blob: 7b3b21ad3bd4248ba61c4b8a4b4376a083dcc339 [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
142 mDevice->Release();
143 mDevice = NULL;
144 }
145
146 if (mDeviceEx)
147 {
148 mDeviceEx->Release();
149 mDeviceEx = NULL;
150 }
151
152 if (mD3d9)
153 {
154 mD3d9->Release();
155 mD3d9 = NULL;
156 }
157
158 if (mDeviceWindow)
159 {
160 DestroyWindow(mDeviceWindow);
161 mDeviceWindow = NULL;
162 }
163
164 if (mD3d9Ex)
165 {
166 mD3d9Ex->Release();
167 mD3d9Ex = NULL;
168 }
169
170 if (mD3d9Module)
171 {
172 mD3d9Module = NULL;
173 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000174}
175
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000176Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
177{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000178 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer9*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000179 return static_cast<rx::Renderer9*>(renderer);
180}
181
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000182EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000183{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000184 if (!initializeCompiler())
185 {
186 return EGL_NOT_INITIALIZED;
187 }
188
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000189 if (mSoftwareDevice)
190 {
191 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
192 }
193 else
194 {
195 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
196 }
197
198 if (mD3d9Module == NULL)
199 {
200 ERR("No D3D9 module found - aborting!\n");
201 return EGL_NOT_INITIALIZED;
202 }
203
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000204 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
205 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
206
207 // Use Direct3D9Ex if available. Among other things, this version is less
208 // inclined to report a lost context, for example when the user switches
209 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
210 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
211 {
212 ASSERT(mD3d9Ex);
213 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
214 ASSERT(mD3d9);
215 }
216 else
217 {
218 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
219 }
220
221 if (!mD3d9)
222 {
223 ERR("Could not create D3D9 device - aborting!\n");
224 return EGL_NOT_INITIALIZED;
225 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000226
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000227 if (mDc != NULL)
228 {
229 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
230 }
231
232 HRESULT result;
233
234 // Give up on getting device caps after about one second.
235 for (int i = 0; i < 10; ++i)
236 {
237 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
238 if (SUCCEEDED(result))
239 {
240 break;
241 }
242 else if (result == D3DERR_NOTAVAILABLE)
243 {
244 Sleep(100); // Give the driver some time to initialize/recover
245 }
246 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
247 {
248 ERR("failed to get device caps (0x%x)\n", result);
249 return EGL_NOT_INITIALIZED;
250 }
251 }
252
253 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
254 {
255 ERR("Renderer does not support PS 2.0. aborting!\n");
256 return EGL_NOT_INITIALIZED;
257 }
258
259 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
260 // This is required by Texture2D::convertToRenderTarget.
261 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
262 {
263 ERR("Renderer does not support stretctrect from textures!\n");
264 return EGL_NOT_INITIALIZED;
265 }
266
267 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
268
269 // ATI cards on XP have problems with non-power-of-two textures.
270 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
271 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
272 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
273 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
274
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000275 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
276 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
277
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000278 mMinSwapInterval = 4;
279 mMaxSwapInterval = 0;
280
281 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
282 {
283 mMinSwapInterval = std::min(mMinSwapInterval, 0);
284 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
285 }
286 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
287 {
288 mMinSwapInterval = std::min(mMinSwapInterval, 1);
289 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
290 }
291 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
292 {
293 mMinSwapInterval = std::min(mMinSwapInterval, 2);
294 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
295 }
296 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
297 {
298 mMinSwapInterval = std::min(mMinSwapInterval, 3);
299 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
300 }
301 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
302 {
303 mMinSwapInterval = std::min(mMinSwapInterval, 4);
304 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
305 }
306
Geoff Lang61e49a52013-05-29 10:22:58 -0400307 mMaxSupportedSamples = 0;
308
309 const d3d9::D3DFormatSet &d3d9Formats = d3d9::GetAllUsedD3DFormats();
310 for (d3d9::D3DFormatSet::const_iterator i = d3d9Formats.begin(); i != d3d9Formats.end(); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000311 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400312 MultisampleSupportInfo support = getMultiSampleSupport(*i);
313 mMultiSampleSupport[*i] = support;
314 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
daniel@transgaming.com92955622012-10-31 18:38:41 +0000315 }
316
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000317 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
318 static const TCHAR className[] = TEXT("STATIC");
319
320 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
321
322 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
323 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
324
325 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
326 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
327 {
328 return EGL_BAD_ALLOC;
329 }
330
331 if (FAILED(result))
332 {
333 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
334
335 if (FAILED(result))
336 {
337 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
338 return EGL_BAD_ALLOC;
339 }
340 }
341
342 if (mD3d9Ex)
343 {
344 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
345 ASSERT(SUCCEEDED(result));
346 }
347
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000348 mVertexShaderCache.initialize(mDevice);
349 mPixelShaderCache.initialize(mDevice);
350
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000351 // Check occlusion query support
352 IDirect3DQuery9 *occlusionQuery = NULL;
353 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery)) && occlusionQuery)
354 {
355 occlusionQuery->Release();
356 mOcclusionQuerySupport = true;
357 }
358 else
359 {
360 mOcclusionQuerySupport = false;
361 }
362
363 // Check event query support
364 IDirect3DQuery9 *eventQuery = NULL;
365 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery)) && eventQuery)
366 {
367 eventQuery->Release();
368 mEventQuerySupport = true;
369 }
370 else
371 {
372 mEventQuerySupport = false;
373 }
374
375 D3DDISPLAYMODE currentDisplayMode;
376 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
377
378 // Check vertex texture support
379 // Only Direct3D 10 ready devices support all the necessary vertex texture formats.
380 // We test this using D3D9 by checking support for the R16F format.
381 mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) &&
382 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
383 D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F));
384
Geoff Langd42cf4e2013-06-05 16:09:17 -0400385 // Check RGB565 texture support
386 mRGB565TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
387 D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_R5G6B5));
388
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000389 // Check depth texture support
390 // we use INTZ for depth textures in Direct3D9
391 // we also want NULL texture support to ensure the we can make depth-only FBOs
392 // see http://aras-p.info/texts/D3D9GPUHacks.html
393 mDepthTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
394 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)) &&
395 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
396 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
397
398 // Check 32 bit floating point texture support
399 mFloat32FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
400 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
401 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
402 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
403
404 mFloat32RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
405 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
406 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
407 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
408
409 if (!mFloat32FilterSupport && !mFloat32RenderSupport)
410 {
411 mFloat32TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
412 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
413 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
414 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
415 }
416 else
417 {
418 mFloat32TextureSupport = true;
419 }
420
421 // Check 16 bit floating point texture support
422 mFloat16FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
423 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
424 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
425 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
426
427 mFloat16RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
428 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
429 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
430 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
431
432 if (!mFloat16FilterSupport && !mFloat16RenderSupport)
433 {
434 mFloat16TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
435 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
436 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
437 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
438 }
439 else
440 {
441 mFloat16TextureSupport = true;
442 }
443
444 // Check DXT texture support
445 mDXT1TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
446 mDXT3TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
447 mDXT5TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
448
449 // Check luminance[alpha] texture support
450 mLuminanceTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
451 mLuminanceAlphaTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
452
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000453 initializeDevice();
454
455 return EGL_SUCCESS;
456}
457
458// do any one-time device initialization
459// NOTE: this is also needed after a device lost/reset
460// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000461void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000462{
463 // Permanent non-default states
464 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
465 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
466
467 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
468 {
469 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
470 }
471 else
472 {
473 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
474 }
475
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000476 markAllStateDirty();
477
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000478 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000479
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000480 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
Geoff Langdce735c2013-06-04 10:21:18 -0400481 mBlit = new Blit9(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000482 mVertexDataManager = new rx::VertexDataManager(this);
483 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000484}
485
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000486D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000487{
488 D3DPRESENT_PARAMETERS presentParameters = {0};
489
490 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
491 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
492 presentParameters.BackBufferCount = 1;
493 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
494 presentParameters.BackBufferWidth = 1;
495 presentParameters.BackBufferHeight = 1;
496 presentParameters.EnableAutoDepthStencil = FALSE;
497 presentParameters.Flags = 0;
498 presentParameters.hDeviceWindow = mDeviceWindow;
499 presentParameters.MultiSampleQuality = 0;
500 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
501 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
502 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
503 presentParameters.Windowed = TRUE;
504
505 return presentParameters;
506}
507
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000508int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000509{
510 D3DDISPLAYMODE currentDisplayMode;
511 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
512
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000513 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
514 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000515 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
516 int numConfigs = 0;
517
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000518 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000519 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000520 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000521
522 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
523
524 if (SUCCEEDED(result))
525 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000526 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000527 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000528 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000529 HRESULT result = D3D_OK;
530
531 if(depthStencilFormat != D3DFMT_UNKNOWN)
532 {
533 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
534 }
535
536 if (SUCCEEDED(result))
537 {
538 if(depthStencilFormat != D3DFMT_UNKNOWN)
539 {
540 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
541 }
542
543 if (SUCCEEDED(result))
544 {
545 ConfigDesc newConfig;
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +0000546 newConfig.renderTargetFormat = d3d9_gl::GetInternalFormat(renderTargetFormat);
547 newConfig.depthStencilFormat = d3d9_gl::GetInternalFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000548 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
549 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000550 newConfig.es3Capable = false;
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000551
552 (*configDescList)[numConfigs++] = newConfig;
553 }
554 }
555 }
556 }
557 }
558
559 return numConfigs;
560}
561
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000562void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000563{
564 delete [] (configDescList);
565}
566
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000567void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000568{
569 if (!mSceneStarted)
570 {
571 long result = mDevice->BeginScene();
572 if (SUCCEEDED(result)) {
573 // This is defensive checking against the device being
574 // lost at unexpected times.
575 mSceneStarted = true;
576 }
577 }
578}
579
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000580void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000581{
582 if (mSceneStarted)
583 {
584 // EndScene can fail if the device was lost, for example due
585 // to a TDR during a draw call.
586 mDevice->EndScene();
587 mSceneStarted = false;
588 }
589}
590
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000591void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000592{
593 HRESULT result;
594
595 IDirect3DQuery9* query = allocateEventQuery();
596 if (!query)
597 {
598 return;
599 }
600
601 result = query->Issue(D3DISSUE_END);
602 ASSERT(SUCCEEDED(result));
603
604 do
605 {
606 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
607
608 if(block && result == S_FALSE)
609 {
610 // Keep polling, but allow other threads to do something useful first
611 Sleep(0);
612 // explicitly check for device loss
613 // some drivers seem to return S_FALSE even if the device is lost
614 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000615 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000616 {
617 result = D3DERR_DEVICELOST;
618 }
619 }
620 }
621 while(block && result == S_FALSE);
622
623 freeEventQuery(query);
624
shannon.woods@transgaming.comf5f59492013-02-28 23:04:40 +0000625 if (d3d9::isDeviceLostError(result))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000626 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +0000627 notifyDeviceLost();
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000628 }
629}
630
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000631SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
632{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000633 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000634}
635
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000636IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000637{
638 IDirect3DQuery9 *query = NULL;
639
640 if (mEventQueryPool.empty())
641 {
642 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
643 ASSERT(SUCCEEDED(result));
644 }
645 else
646 {
647 query = mEventQueryPool.back();
648 mEventQueryPool.pop_back();
649 }
650
651 return query;
652}
653
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000654void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000655{
656 if (mEventQueryPool.size() > 1000)
657 {
658 query->Release();
659 }
660 else
661 {
662 mEventQueryPool.push_back(query);
663 }
664}
665
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000666IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000667{
668 return mVertexShaderCache.create(function, length);
669}
670
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000671IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000672{
673 return mPixelShaderCache.create(function, length);
674}
675
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000676HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
677{
678 D3DPOOL Pool = getBufferPool(Usage);
679 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
680}
681
daniel@transgaming.com3f255b42012-12-20 21:07:35 +0000682VertexBuffer *Renderer9::createVertexBuffer()
683{
684 return new VertexBuffer9(this);
685}
686
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000687HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
688{
689 D3DPOOL Pool = getBufferPool(Usage);
690 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
691}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000692
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +0000693IndexBuffer *Renderer9::createIndexBuffer()
694{
695 return new IndexBuffer9(this);
696}
697
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +0000698BufferStorage *Renderer9::createBufferStorage()
699{
700 return new BufferStorage9();
701}
702
shannon.woods@transgaming.com50df6c52013-02-28 23:02:49 +0000703QueryImpl *Renderer9::createQuery(GLenum type)
704{
705 return new Query9(this, type);
706}
707
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +0000708FenceImpl *Renderer9::createFence()
709{
710 return new Fence9(this);
711}
712
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000713void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000714{
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000715 bool *forceSetSamplers = (type == gl::SAMPLER_PIXEL) ? mForceSetPixelSamplerStates : mForceSetVertexSamplerStates;
716 gl::SamplerState *appliedSamplers = (type == gl::SAMPLER_PIXEL) ? mCurPixelSamplerStates: mCurVertexSamplerStates;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000717
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000718 if (forceSetSamplers[index] || memcmp(&samplerState, &appliedSamplers[index], sizeof(gl::SamplerState)) != 0)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000719 {
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000720 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
721 int d3dSampler = index + d3dSamplerOffset;
722
723 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
724 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
725
726 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
727 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
728 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
729 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
730 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
731 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
732 if (mSupportsTextureFilterAnisotropy)
733 {
734 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
735 }
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000736 }
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000737
738 forceSetSamplers[index] = false;
739 appliedSamplers[index] = samplerState;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000740}
741
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000742void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000743{
744 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
745 int d3dSampler = index + d3dSamplerOffset;
746 IDirect3DBaseTexture9 *d3dTexture = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000747 unsigned int serial = 0;
748 bool forceSetTexture = false;
749
750 unsigned int *appliedSerials = (type == gl::SAMPLER_PIXEL) ? mCurPixelTextureSerials : mCurVertexTextureSerials;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000751
752 if (texture)
753 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000754 TextureStorageInterface *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000755 if (texStorage)
756 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000757 TextureStorage9 *storage9 = TextureStorage9::makeTextureStorage9(texStorage->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +0000758 d3dTexture = storage9->getBaseTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000759 }
760 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000761 // in the texture class and we're unexpectedly missing the d3d texture
762 ASSERT(d3dTexture != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000763
764 serial = texture->getTextureSerial();
765 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000766 }
767
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000768 if (forceSetTexture || appliedSerials[index] != serial)
769 {
770 mDevice->SetTexture(d3dSampler, d3dTexture);
771 }
772
773 appliedSerials[index] = serial;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000774}
775
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000776bool Renderer9::setUniformBuffers(const gl::Buffer* /*vertexUniformBuffers*/[], const gl::Buffer* /*fragmentUniformBuffers*/[])
777{
778 // No effect in ES2/D3D9
779 return true;
780}
781
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000782void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000783{
784 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000785
786 if (rasterStateChanged)
787 {
788 // Set the cull mode
789 if (rasterState.cullFace)
790 {
791 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
792 }
793 else
794 {
795 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
796 }
797
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000798 if (rasterState.polygonOffsetFill)
799 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000800 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000801 {
802 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
803
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000804 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000805 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
806 }
807 }
808 else
809 {
810 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
811 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
812 }
813
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000814 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000815 }
816
817 mForceSetRasterState = false;
818}
819
Geoff Lang2a64ee42013-05-31 11:22:40 -0400820void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor, unsigned int sampleMask)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000821{
822 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
Geoff Lang2a64ee42013-05-31 11:22:40 -0400823 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000824 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
825
826 if (blendStateChanged || blendColorChanged)
827 {
828 if (blendState.blend)
829 {
830 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
831
832 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
833 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
834 {
835 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
836 }
837 else
838 {
839 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
840 gl::unorm<8>(blendColor.alpha),
841 gl::unorm<8>(blendColor.alpha),
842 gl::unorm<8>(blendColor.alpha)));
843 }
844
845 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
846 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
847 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
848
849 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
850 blendState.destBlendRGB != blendState.destBlendAlpha ||
851 blendState.blendEquationRGB != blendState.blendEquationAlpha)
852 {
853 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
854
855 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
856 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
857 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
858 }
859 else
860 {
861 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
862 }
863 }
864 else
865 {
866 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
867 }
868
869 if (blendState.sampleAlphaToCoverage)
870 {
871 FIXME("Sample alpha to coverage is unimplemented.");
872 }
873
874 // Set the color mask
875 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
876 // Apparently some ATI cards have a bug where a draw with a zero color
877 // write mask can cause later draws to have incorrect results. Instead,
878 // set a nonzero color write mask but modify the blend state so that no
879 // drawing is done.
880 // http://code.google.com/p/angleproject/issues/detail?id=169
881
882 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
883 blendState.colorMaskBlue, blendState.colorMaskAlpha);
884 if (colorMask == 0 && !zeroColorMaskAllowed)
885 {
886 // Enable green channel, but set blending so nothing will be drawn.
887 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
888 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
889
890 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
891 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
892 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
893 }
894 else
895 {
896 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
897 }
898
899 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
900
901 mCurBlendState = blendState;
902 mCurBlendColor = blendColor;
903 }
904
905 if (sampleMaskChanged)
906 {
907 // Set the multisample mask
908 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
909 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
910
911 mCurSampleMask = sampleMask;
912 }
913
914 mForceSetBlendState = false;
915}
916
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000917void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000918 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000919{
920 bool depthStencilStateChanged = mForceSetDepthStencilState ||
921 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000922 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
923 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000924 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000925
926 if (depthStencilStateChanged)
927 {
928 if (depthStencilState.depthTest)
929 {
930 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
931 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
932 }
933 else
934 {
935 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
936 }
937
938 mCurDepthStencilState = depthStencilState;
939 }
940
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000941 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000942 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000943 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000944 {
945 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
946 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
947
948 // FIXME: Unsupported by D3D9
949 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
950 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
951 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
952 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000953 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000954 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
955 {
956 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 +0000957 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000958 }
959
960 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000961 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000962
963 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
964 depthStencilState.stencilWritemask);
965 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
966 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
967
968 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000969 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000970 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
971 depthStencilState.stencilMask);
972
973 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
974 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
975 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
976 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
977 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
978 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
979
980 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
981 depthStencilState.stencilBackWritemask);
982 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
983 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
984
985 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000986 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000987 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
988 depthStencilState.stencilBackMask);
989
990 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
991 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
992 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
993 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
994 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
995 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
996 }
997 else
998 {
999 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1000 }
1001
1002 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
1003
daniel@transgaming.com08c331d2012-11-28 19:38:39 +00001004 mCurStencilRef = stencilRef;
1005 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001006 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001007 }
1008
1009 mForceSetDepthStencilState = false;
1010}
1011
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001012void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001013{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001014 bool scissorChanged = mForceSetScissor ||
1015 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
1016 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001017
daniel@transgaming.com04f1b332012-11-28 21:00:40 +00001018 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001019 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001020 if (enabled)
1021 {
1022 RECT rect;
1023 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
1024 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
1025 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
1026 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
1027 mDevice->SetScissorRect(&rect);
1028 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001029
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001030 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
1031
1032 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001033 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001034 }
1035
1036 mForceSetScissor = false;
1037}
1038
daniel@transgaming.com12985182012-12-20 20:56:31 +00001039bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +00001040 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001041{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001042 gl::Rectangle actualViewport = viewport;
1043 float actualZNear = gl::clamp01(zNear);
1044 float actualZFar = gl::clamp01(zFar);
1045 if (ignoreViewport)
1046 {
1047 actualViewport.x = 0;
1048 actualViewport.y = 0;
1049 actualViewport.width = mRenderTargetDesc.width;
1050 actualViewport.height = mRenderTargetDesc.height;
1051 actualZNear = 0.0f;
1052 actualZFar = 1.0f;
1053 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001054
1055 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001056 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
1057 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
1058 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
1059 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
1060 dxViewport.MinZ = actualZNear;
1061 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001062
1063 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
1064 {
1065 return false; // Nothing to render
1066 }
1067
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001068 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
1069 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001070 if (viewportChanged)
1071 {
1072 mDevice->SetViewport(&dxViewport);
1073
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001074 mCurViewport = actualViewport;
1075 mCurNear = actualZNear;
1076 mCurFar = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001077
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001078 dx_VertexConstants vc = {0};
1079 dx_PixelConstants pc = {0};
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001080
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00001081 vc.viewAdjust[0] = (float)((actualViewport.width - (int)dxViewport.Width) + 2 * (actualViewport.x - (int)dxViewport.X) - 1) / dxViewport.Width;
1082 vc.viewAdjust[1] = (float)((actualViewport.height - (int)dxViewport.Height) + 2 * (actualViewport.y - (int)dxViewport.Y) - 1) / dxViewport.Height;
1083 vc.viewAdjust[2] = (float)actualViewport.width / dxViewport.Width;
1084 vc.viewAdjust[3] = (float)actualViewport.height / dxViewport.Height;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001085
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001086 pc.viewCoords[0] = actualViewport.width * 0.5f;
1087 pc.viewCoords[1] = actualViewport.height * 0.5f;
1088 pc.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
1089 pc.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001090
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001091 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
1092 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
1093 pc.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);;
1094
1095 vc.depthRange[0] = actualZNear;
1096 vc.depthRange[1] = actualZFar;
1097 vc.depthRange[2] = actualZFar - actualZNear;
1098
1099 pc.depthRange[0] = actualZNear;
1100 pc.depthRange[1] = actualZFar;
1101 pc.depthRange[2] = actualZFar - actualZNear;
1102
1103 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
1104 {
1105 mVertexConstants = vc;
1106 mDxUniformsDirty = true;
1107 }
1108
1109 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
1110 {
1111 mPixelConstants = pc;
1112 mDxUniformsDirty = true;
1113 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001114 }
1115
1116 mForceSetViewport = false;
1117 return true;
1118}
1119
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001120bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
1121{
1122 switch (mode)
1123 {
1124 case GL_POINTS:
1125 mPrimitiveType = D3DPT_POINTLIST;
1126 mPrimitiveCount = count;
1127 break;
1128 case GL_LINES:
1129 mPrimitiveType = D3DPT_LINELIST;
1130 mPrimitiveCount = count / 2;
1131 break;
1132 case GL_LINE_LOOP:
1133 mPrimitiveType = D3DPT_LINESTRIP;
1134 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
1135 break;
1136 case GL_LINE_STRIP:
1137 mPrimitiveType = D3DPT_LINESTRIP;
1138 mPrimitiveCount = count - 1;
1139 break;
1140 case GL_TRIANGLES:
1141 mPrimitiveType = D3DPT_TRIANGLELIST;
1142 mPrimitiveCount = count / 3;
1143 break;
1144 case GL_TRIANGLE_STRIP:
1145 mPrimitiveType = D3DPT_TRIANGLESTRIP;
1146 mPrimitiveCount = count - 2;
1147 break;
1148 case GL_TRIANGLE_FAN:
1149 mPrimitiveType = D3DPT_TRIANGLEFAN;
1150 mPrimitiveCount = count - 2;
1151 break;
1152 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001153 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001154 }
1155
1156 return mPrimitiveCount > 0;
1157}
1158
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001159
1160gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
1161{
1162 if (!depthbuffer)
1163 {
1164 ERR("Unexpected null depthbuffer for depth-only FBO.");
1165 return NULL;
1166 }
1167
1168 GLsizei width = depthbuffer->getWidth();
1169 GLsizei height = depthbuffer->getHeight();
1170
1171 // search cached nullcolorbuffers
1172 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1173 {
1174 if (mNullColorbufferCache[i].buffer != NULL &&
1175 mNullColorbufferCache[i].width == width &&
1176 mNullColorbufferCache[i].height == height)
1177 {
1178 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1179 return mNullColorbufferCache[i].buffer;
1180 }
1181 }
1182
1183 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1184
1185 // add nullbuffer to the cache
1186 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1187 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1188 {
1189 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1190 {
1191 oldest = &mNullColorbufferCache[i];
1192 }
1193 }
1194
1195 delete oldest->buffer;
1196 oldest->buffer = nullbuffer;
1197 oldest->lruCount = ++mMaxNullColorbufferLRU;
1198 oldest->width = width;
1199 oldest->height = height;
1200
1201 return nullbuffer;
1202}
1203
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001204bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001205{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001206 // if there is no color attachment we must synthesize a NULL colorattachment
1207 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1208 gl::Renderbuffer *renderbufferObject = NULL;
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00001209 if (framebuffer->getColorbufferType(0) != GL_NONE)
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001210 {
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00001211 renderbufferObject = framebuffer->getColorbuffer(0);
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001212 }
1213 else
1214 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001215 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001216 }
1217 if (!renderbufferObject)
1218 {
1219 ERR("unable to locate renderbuffer for FBO.");
1220 return false;
1221 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001222
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001223 bool renderTargetChanged = false;
1224 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1225 if (renderTargetSerial != mAppliedRenderTargetSerial)
1226 {
1227 // Apply the render target on the device
1228 IDirect3DSurface9 *renderTargetSurface = NULL;
1229
1230 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1231 if (renderTarget)
1232 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001233 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001234 }
1235
1236 if (!renderTargetSurface)
1237 {
1238 ERR("render target pointer unexpectedly null.");
1239 return false; // Context must be lost
1240 }
1241
1242 mDevice->SetRenderTarget(0, renderTargetSurface);
1243 renderTargetSurface->Release();
1244
1245 mAppliedRenderTargetSerial = renderTargetSerial;
1246 renderTargetChanged = true;
1247 }
1248
1249 gl::Renderbuffer *depthStencil = NULL;
1250 unsigned int depthbufferSerial = 0;
1251 unsigned int stencilbufferSerial = 0;
1252 if (framebuffer->getDepthbufferType() != GL_NONE)
1253 {
1254 depthStencil = framebuffer->getDepthbuffer();
1255 if (!depthStencil)
1256 {
1257 ERR("Depth stencil pointer unexpectedly null.");
1258 return false;
1259 }
1260
1261 depthbufferSerial = depthStencil->getSerial();
1262 }
1263 else if (framebuffer->getStencilbufferType() != GL_NONE)
1264 {
1265 depthStencil = framebuffer->getStencilbuffer();
1266 if (!depthStencil)
1267 {
1268 ERR("Depth stencil pointer unexpectedly null.");
1269 return false;
1270 }
1271
1272 stencilbufferSerial = depthStencil->getSerial();
1273 }
1274
1275 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1276 stencilbufferSerial != mAppliedStencilbufferSerial ||
1277 !mDepthStencilInitialized)
1278 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001279 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001280 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001281
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001282 // Apply the depth stencil on the device
1283 if (depthStencil)
1284 {
1285 IDirect3DSurface9 *depthStencilSurface = NULL;
1286 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1287
1288 if (depthStencilRenderTarget)
1289 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001290 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001291 }
1292
1293 if (!depthStencilSurface)
1294 {
1295 ERR("depth stencil pointer unexpectedly null.");
1296 return false; // Context must be lost
1297 }
1298
1299 mDevice->SetDepthStencilSurface(depthStencilSurface);
1300 depthStencilSurface->Release();
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001301
1302 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001303 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001304 }
1305 else
1306 {
1307 mDevice->SetDepthStencilSurface(NULL);
1308 }
1309
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001310 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1311 {
1312 mCurDepthSize = depthSize;
1313 mForceSetRasterState = true;
1314 }
1315
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001316 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1317 {
1318 mCurStencilSize = stencilSize;
1319 mForceSetDepthStencilState = true;
1320 }
1321
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001322 mAppliedDepthbufferSerial = depthbufferSerial;
1323 mAppliedStencilbufferSerial = stencilbufferSerial;
1324 mDepthStencilInitialized = true;
1325 }
1326
1327 if (renderTargetChanged || !mRenderTargetDescInitialized)
1328 {
1329 mForceSetScissor = true;
1330 mForceSetViewport = true;
1331
1332 mRenderTargetDesc.width = renderbufferObject->getWidth();
1333 mRenderTargetDesc.height = renderbufferObject->getHeight();
1334 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1335 mRenderTargetDescInitialized = true;
1336 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001337
1338 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001339}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001340
Jamie Madill57a89722013-07-02 11:57:03 -04001341GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001342 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001343{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001344 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001345 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001346 if (err != GL_NO_ERROR)
1347 {
1348 return err;
1349 }
1350
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001351 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1352}
1353
1354// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001355GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001356{
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001357 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001358
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001359 if (err == GL_NO_ERROR)
1360 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001361 // Directly binding the storage buffer is not supported for d3d9
1362 ASSERT(indexInfo->storage == NULL);
1363
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001364 if (indexInfo->serial != mAppliedIBSerial)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001365 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001366 IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer);
1367
1368 mDevice->SetIndices(indexBuffer->getBuffer());
1369 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001370 }
1371 }
1372
1373 return err;
1374}
1375
1376void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1377{
1378 startScene();
1379
1380 if (mode == GL_LINE_LOOP)
1381 {
1382 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1383 }
1384 else if (instances > 0)
1385 {
daniel@transgaming.com50cc7252012-12-20 21:09:23 +00001386 StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001387 if (countingIB)
1388 {
1389 if (mAppliedIBSerial != countingIB->getSerial())
1390 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001391 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer());
1392
1393 mDevice->SetIndices(indexBuffer->getBuffer());
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001394 mAppliedIBSerial = countingIB->getSerial();
1395 }
1396
1397 for (int i = 0; i < mRepeatDraw; i++)
1398 {
1399 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1400 }
1401 }
1402 else
1403 {
1404 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001405 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001406 }
1407 }
1408 else // Regular case
1409 {
1410 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1411 }
1412}
1413
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001414void 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 +00001415{
1416 startScene();
1417
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001418 if (mode == GL_POINTS)
1419 {
1420 drawIndexedPoints(count, type, indices, elementArrayBuffer);
1421 }
1422 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001423 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001424 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001425 }
1426 else
1427 {
1428 for (int i = 0; i < mRepeatDraw; i++)
1429 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001430 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1431 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001432 }
1433 }
1434}
1435
1436void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1437{
1438 // Get the raw indices for an indexed draw
1439 if (type != GL_NONE && elementArrayBuffer)
1440 {
1441 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001442 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001443 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001444 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001445 }
1446
1447 UINT startIndex = 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001448
1449 if (get32BitIndexSupport())
1450 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001451 if (!mLineLoopIB)
1452 {
1453 mLineLoopIB = new StreamingIndexBufferInterface(this);
1454 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1455 {
1456 delete mLineLoopIB;
1457 mLineLoopIB = NULL;
1458
1459 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001460 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001461 }
1462 }
1463
Geoff Langeadfd572013-07-09 15:55:07 -04001464 if (static_cast<unsigned int>(count + 1) > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
1465 {
1466 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1467 return gl::error(GL_OUT_OF_MEMORY);
1468 }
1469
1470 const unsigned int spaceNeeded = (count + 1) * sizeof(unsigned int);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001471 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001472 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001473 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001474 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001475 }
1476
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001477 void* mappedMemory = NULL;
1478 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1479 if (offset == -1 || mappedMemory == NULL)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001480 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001481 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001482 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001483 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001484
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001485 startIndex = static_cast<UINT>(offset) / 4;
1486 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1487
1488 switch (type)
1489 {
1490 case GL_NONE: // Non-indexed draw
1491 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001492 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001493 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001494 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001495 data[count] = 0;
1496 break;
1497 case GL_UNSIGNED_BYTE:
1498 for (int i = 0; i < count; i++)
1499 {
1500 data[i] = static_cast<const GLubyte*>(indices)[i];
1501 }
1502 data[count] = static_cast<const GLubyte*>(indices)[0];
1503 break;
1504 case GL_UNSIGNED_SHORT:
1505 for (int i = 0; i < count; i++)
1506 {
1507 data[i] = static_cast<const GLushort*>(indices)[i];
1508 }
1509 data[count] = static_cast<const GLushort*>(indices)[0];
1510 break;
1511 case GL_UNSIGNED_INT:
1512 for (int i = 0; i < count; i++)
1513 {
1514 data[i] = static_cast<const GLuint*>(indices)[i];
1515 }
1516 data[count] = static_cast<const GLuint*>(indices)[0];
1517 break;
1518 default: UNREACHABLE();
1519 }
1520
1521 if (!mLineLoopIB->unmapBuffer())
1522 {
1523 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001524 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001525 }
1526 }
1527 else
1528 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001529 if (!mLineLoopIB)
1530 {
1531 mLineLoopIB = new StreamingIndexBufferInterface(this);
1532 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
1533 {
1534 delete mLineLoopIB;
1535 mLineLoopIB = NULL;
1536
1537 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001538 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001539 }
1540 }
1541
Geoff Langeadfd572013-07-09 15:55:07 -04001542 if (static_cast<unsigned int>(count + 1) > (std::numeric_limits<unsigned short>::max() / sizeof(unsigned short)))
1543 {
1544 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1545 return gl::error(GL_OUT_OF_MEMORY);
1546 }
1547
1548 const unsigned int spaceNeeded = (count + 1) * sizeof(unsigned short);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001549 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001550 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001551 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001552 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001553 }
1554
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001555 void* mappedMemory = NULL;
1556 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1557 if (offset == -1 || mappedMemory == NULL)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001558 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001559 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001560 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001561 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001562
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001563 startIndex = static_cast<UINT>(offset) / 2;
1564 unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory);
1565
1566 switch (type)
1567 {
1568 case GL_NONE: // Non-indexed draw
1569 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001570 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001571 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001572 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001573 data[count] = 0;
1574 break;
1575 case GL_UNSIGNED_BYTE:
1576 for (int i = 0; i < count; i++)
1577 {
1578 data[i] = static_cast<const GLubyte*>(indices)[i];
1579 }
1580 data[count] = static_cast<const GLubyte*>(indices)[0];
1581 break;
1582 case GL_UNSIGNED_SHORT:
1583 for (int i = 0; i < count; i++)
1584 {
1585 data[i] = static_cast<const GLushort*>(indices)[i];
1586 }
1587 data[count] = static_cast<const GLushort*>(indices)[0];
1588 break;
1589 case GL_UNSIGNED_INT:
1590 for (int i = 0; i < count; i++)
1591 {
1592 data[i] = static_cast<const GLuint*>(indices)[i];
1593 }
1594 data[count] = static_cast<const GLuint*>(indices)[0];
1595 break;
1596 default: UNREACHABLE();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001597 }
1598
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001599 if (!mLineLoopIB->unmapBuffer())
1600 {
1601 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001602 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001603 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001604 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001605
1606 if (mAppliedIBSerial != mLineLoopIB->getSerial())
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001607 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001608 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer());
1609
1610 mDevice->SetIndices(indexBuffer->getBuffer());
1611 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001612 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001613
1614 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001615}
1616
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001617template <typename T>
1618static void drawPoints(IDirect3DDevice9* device, GLsizei count, const GLvoid *indices)
1619{
1620 for (int i = 0; i < count; i++)
1621 {
1622 unsigned int indexValue = static_cast<unsigned int>(static_cast<const T*>(indices)[i]);
1623 device->DrawPrimitive(D3DPT_POINTLIST, indexValue, 1);
1624 }
1625}
1626
1627void Renderer9::drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer)
1628{
1629 // Drawing index point lists is unsupported in d3d9, fall back to a regular DrawPrimitive call
1630 // for each individual point. This call is not expected to happen often.
1631
1632 if (elementArrayBuffer)
1633 {
1634 BufferStorage *storage = elementArrayBuffer->getStorage();
1635 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1636 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
1637 }
1638
1639 switch (type)
1640 {
1641 case GL_UNSIGNED_BYTE: drawPoints<GLubyte>(mDevice, count, indices); break;
1642 case GL_UNSIGNED_SHORT: drawPoints<GLushort>(mDevice, count, indices); break;
1643 case GL_UNSIGNED_INT: drawPoints<GLuint>(mDevice, count, indices); break;
1644 default: UNREACHABLE();
1645 }
1646}
1647
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001648void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1649{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001650 unsigned int programBinarySerial = programBinary->getSerial();
1651 if (programBinarySerial != mAppliedProgramBinarySerial)
1652 {
1653 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1654 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001655
daniel@transgaming.come4991412012-12-20 20:55:34 +00001656 IDirect3DVertexShader9 *vertexShader = NULL;
1657 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001658
daniel@transgaming.come4991412012-12-20 20:55:34 +00001659 IDirect3DPixelShader9 *pixelShader = NULL;
1660 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001661
daniel@transgaming.come4991412012-12-20 20:55:34 +00001662 mDevice->SetPixelShader(pixelShader);
1663 mDevice->SetVertexShader(vertexShader);
1664 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001665 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001666
1667 mAppliedProgramBinarySerial = programBinarySerial;
1668 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001669}
1670
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001671void Renderer9::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001672{
1673 for (std::vector<gl::Uniform*>::const_iterator ub = uniformArray->begin(), ue = uniformArray->end(); ub != ue; ++ub)
1674 {
1675 gl::Uniform *targetUniform = *ub;
1676
1677 if (targetUniform->dirty)
1678 {
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001679 GLfloat *f = (GLfloat*)targetUniform->data;
1680 GLint *i = (GLint*)targetUniform->data;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001681
1682 switch (targetUniform->type)
1683 {
1684 case GL_SAMPLER_2D:
1685 case GL_SAMPLER_CUBE:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001686 break;
1687 case GL_BOOL:
1688 case GL_BOOL_VEC2:
1689 case GL_BOOL_VEC3:
1690 case GL_BOOL_VEC4:
1691 applyUniformnbv(targetUniform, i);
1692 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001693 case GL_FLOAT:
1694 case GL_FLOAT_VEC2:
1695 case GL_FLOAT_VEC3:
1696 case GL_FLOAT_VEC4:
1697 case GL_FLOAT_MAT2:
1698 case GL_FLOAT_MAT3:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001699 case GL_FLOAT_MAT4:
1700 applyUniformnfv(targetUniform, f);
1701 break;
1702 case GL_INT:
1703 case GL_INT_VEC2:
1704 case GL_INT_VEC3:
1705 case GL_INT_VEC4:
1706 applyUniformniv(targetUniform, i);
1707 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001708 default:
1709 UNREACHABLE();
1710 }
1711
1712 targetUniform->dirty = false;
1713 }
1714 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001715
1716 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001717 if (mDxUniformsDirty)
1718 {
1719 mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4]));
1720 mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4]));
1721 mDxUniformsDirty = false;
1722 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001723}
1724
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001725void Renderer9::applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001726{
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001727 if (targetUniform->isReferencedByFragmentShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001728 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001729 mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001730 }
1731
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001732 if (targetUniform->isReferencedByVertexShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001733 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001734 mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001735 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001736}
1737
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001738void Renderer9::applyUniformniv(gl::Uniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001739{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001740 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1741 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001742
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001743 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001744 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001745 vector[i][0] = (GLfloat)v[4 * i + 0];
1746 vector[i][1] = (GLfloat)v[4 * i + 1];
1747 vector[i][2] = (GLfloat)v[4 * i + 2];
1748 vector[i][3] = (GLfloat)v[4 * i + 3];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001749 }
1750
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001751 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001752}
1753
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001754void Renderer9::applyUniformnbv(gl::Uniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001755{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001756 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1757 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001758
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001759 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001760 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001761 vector[i][0] = (v[4 * i + 0] == GL_FALSE) ? 0.0f : 1.0f;
1762 vector[i][1] = (v[4 * i + 1] == GL_FALSE) ? 0.0f : 1.0f;
1763 vector[i][2] = (v[4 * i + 2] == GL_FALSE) ? 0.0f : 1.0f;
1764 vector[i][3] = (v[4 * i + 3] == GL_FALSE) ? 0.0f : 1.0f;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001765 }
1766
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001767 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001768}
1769
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001770void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001771{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001772 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1773 gl::unorm<8>(clearParams.colorClearValue.red),
1774 gl::unorm<8>(clearParams.colorClearValue.green),
1775 gl::unorm<8>(clearParams.colorClearValue.blue));
1776 float depth = gl::clamp01(clearParams.depthClearValue);
1777 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001778
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001779 unsigned int stencilUnmasked = 0x0;
1780 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1781 {
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001782 unsigned int stencilSize = gl::GetStencilBits(frameBuffer->getStencilbuffer()->getActualFormat(),
1783 getCurrentClientVersion());
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001784 stencilUnmasked = (0x1 << stencilSize) - 1;
1785 }
1786
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001787 bool alphaUnmasked = gl::GetAlphaBits(mRenderTargetDesc.format, getCurrentClientVersion()) == 0 ||
1788 clearParams.colorMaskAlpha;
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001789
1790 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1791 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1792 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1793 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1794 clearParams.colorMaskBlue && alphaUnmasked);
1795
1796 if (needMaskedColorClear || needMaskedStencilClear)
1797 {
1798 // State which is altered in all paths from this point to the clear call is saved.
1799 // State which is altered in only some paths will be flagged dirty in the case that
1800 // that path is taken.
1801 HRESULT hr;
1802 if (mMaskedClearSavedState == NULL)
1803 {
1804 hr = mDevice->BeginStateBlock();
1805 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1806
1807 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1808 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1809 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1810 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1811 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1812 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1813 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1814 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1815 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1816 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1817 mDevice->SetPixelShader(NULL);
1818 mDevice->SetVertexShader(NULL);
1819 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1820 mDevice->SetStreamSource(0, NULL, 0, 0);
1821 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1822 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1823 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1824 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1825 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1826 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1827 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1828
1829 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1830 {
1831 mDevice->SetStreamSourceFreq(i, 1);
1832 }
1833
1834 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1835 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1836 }
1837
1838 ASSERT(mMaskedClearSavedState != NULL);
1839
1840 if (mMaskedClearSavedState != NULL)
1841 {
1842 hr = mMaskedClearSavedState->Capture();
1843 ASSERT(SUCCEEDED(hr));
1844 }
1845
1846 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1847 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1848 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1849 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1850 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1851 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1852 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1853 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1854
1855 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1856 {
1857 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1858 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1859 clearParams.colorMaskGreen,
1860 clearParams.colorMaskBlue,
1861 clearParams.colorMaskAlpha));
1862 }
1863 else
1864 {
1865 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1866 }
1867
1868 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1869 {
1870 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1871 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1872 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1873 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1874 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1875 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1876 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1877 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1878 }
1879 else
1880 {
1881 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1882 }
1883
1884 mDevice->SetPixelShader(NULL);
1885 mDevice->SetVertexShader(NULL);
1886 mDevice->SetFVF(D3DFVF_XYZRHW);
1887 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1888 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1889 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1890 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1891 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1892 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1893 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1894
1895 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1896 {
1897 mDevice->SetStreamSourceFreq(i, 1);
1898 }
1899
1900 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1901 quad[0][0] = -0.5f;
1902 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1903 quad[0][2] = 0.0f;
1904 quad[0][3] = 1.0f;
1905
1906 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1907 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1908 quad[1][2] = 0.0f;
1909 quad[1][3] = 1.0f;
1910
1911 quad[2][0] = -0.5f;
1912 quad[2][1] = -0.5f;
1913 quad[2][2] = 0.0f;
1914 quad[2][3] = 1.0f;
1915
1916 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1917 quad[3][1] = -0.5f;
1918 quad[3][2] = 0.0f;
1919 quad[3][3] = 1.0f;
1920
1921 startScene();
1922 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1923
1924 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1925 {
1926 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1927 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1928 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1929 }
1930
1931 if (mMaskedClearSavedState != NULL)
1932 {
1933 mMaskedClearSavedState->Apply();
1934 }
1935 }
1936 else if (clearParams.mask)
1937 {
1938 DWORD dxClearFlags = 0;
1939 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1940 {
1941 dxClearFlags |= D3DCLEAR_TARGET;
1942 }
1943 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1944 {
1945 dxClearFlags |= D3DCLEAR_ZBUFFER;
1946 }
1947 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1948 {
1949 dxClearFlags |= D3DCLEAR_STENCIL;
1950 }
1951
1952 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1953 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001954}
1955
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001956void Renderer9::markAllStateDirty()
1957{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001958 mAppliedRenderTargetSerial = 0;
1959 mAppliedDepthbufferSerial = 0;
1960 mAppliedStencilbufferSerial = 0;
1961 mDepthStencilInitialized = false;
1962 mRenderTargetDescInitialized = false;
1963
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001964 mForceSetDepthStencilState = true;
1965 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001966 mForceSetScissor = true;
1967 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001968 mForceSetBlendState = true;
1969
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001970 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001971 {
1972 mForceSetVertexSamplerStates[i] = true;
1973 mCurVertexTextureSerials[i] = 0;
1974 }
1975 for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1976 {
1977 mForceSetPixelSamplerStates[i] = true;
1978 mCurPixelTextureSerials[i] = 0;
1979 }
1980
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001981 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001982 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001983 mDxUniformsDirty = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001984
1985 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001986}
1987
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001988void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001989{
1990 while (!mEventQueryPool.empty())
1991 {
1992 mEventQueryPool.back()->Release();
1993 mEventQueryPool.pop_back();
1994 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001995
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001996 if (mMaskedClearSavedState)
1997 {
1998 mMaskedClearSavedState->Release();
1999 mMaskedClearSavedState = NULL;
2000 }
2001
daniel@transgaming.come4733d72012-10-31 18:07:01 +00002002 mVertexShaderCache.clear();
2003 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002004
daniel@transgaming.come569fc52012-11-28 20:56:02 +00002005 delete mBlit;
2006 mBlit = NULL;
2007
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002008 delete mVertexDataManager;
2009 mVertexDataManager = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00002010
2011 delete mIndexDataManager;
2012 mIndexDataManager = NULL;
2013
2014 delete mLineLoopIB;
2015 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00002016
2017 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
2018 {
2019 delete mNullColorbufferCache[i].buffer;
2020 mNullColorbufferCache[i].buffer = NULL;
2021 }
2022
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002023}
2024
2025
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002026void Renderer9::notifyDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002027{
2028 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002029 mDisplay->notifyDeviceLost();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002030}
2031
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002032bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002033{
2034 return mDeviceLost;
2035}
2036
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002037// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002038bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002039{
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002040 HRESULT status = S_OK;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002041
2042 if (mDeviceEx)
2043 {
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002044 status = mDeviceEx->CheckDeviceState(NULL);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002045 }
2046 else if (mDevice)
2047 {
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002048 status = mDevice->TestCooperativeLevel();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002049 }
2050 else
2051 {
2052 // No device yet, so no reset required
2053 }
2054
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002055 bool isLost = FAILED(status) || d3d9::isDeviceLostError(status);
2056
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002057 if (isLost)
2058 {
2059 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002060 // we'll probably get this done again by notifyDeviceLost
2061 // but best to remember it!
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002062 // Note that we don't want to clear the device loss status here
2063 // -- this needs to be done by resetDevice
2064 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002065 if (notify)
2066 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002067 notifyDeviceLost();
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002068 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002069 }
2070
2071 return isLost;
2072}
2073
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002074bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002075{
2076 HRESULT status = D3D_OK;
2077
2078 if (mDeviceEx)
2079 {
2080 status = mDeviceEx->CheckDeviceState(NULL);
2081 }
2082 else if (mDevice)
2083 {
2084 status = mDevice->TestCooperativeLevel();
2085 }
2086
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002087 // On D3D9Ex, DEVICELOST represents a hung device that needs to be restarted
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002088 // DEVICEREMOVED indicates the device has been stopped and must be recreated
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002089 switch (status)
2090 {
2091 case D3DERR_DEVICENOTRESET:
2092 case D3DERR_DEVICEHUNG:
2093 return true;
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002094 case D3DERR_DEVICELOST:
2095 return (mDeviceEx != NULL);
2096 case D3DERR_DEVICEREMOVED:
2097 UNIMPLEMENTED();
2098 return false;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002099 default:
2100 return false;
2101 }
2102}
2103
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002104bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002105{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002106 releaseDeviceResources();
2107
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002108 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2109
2110 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002111 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002112 int attempts = 3;
2113
2114 while (lost && attempts > 0)
2115 {
2116 if (mDeviceEx)
2117 {
2118 Sleep(500); // Give the graphics driver some CPU time
2119 result = mDeviceEx->ResetEx(&presentParameters, NULL);
2120 }
2121 else
2122 {
2123 result = mDevice->TestCooperativeLevel();
2124 while (result == D3DERR_DEVICELOST)
2125 {
2126 Sleep(100); // Give the graphics driver some CPU time
2127 result = mDevice->TestCooperativeLevel();
2128 }
2129
2130 if (result == D3DERR_DEVICENOTRESET)
2131 {
2132 result = mDevice->Reset(&presentParameters);
2133 }
2134 }
2135
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002136 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002137 attempts --;
2138 }
2139
2140 if (FAILED(result))
2141 {
2142 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
2143 return false;
2144 }
2145
2146 // reset device defaults
2147 initializeDevice();
2148 mDeviceLost = false;
2149
2150 return true;
2151}
2152
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002153DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002154{
2155 return mAdapterIdentifier.VendorId;
2156}
2157
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002158std::string Renderer9::getRendererDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002159{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002160 std::ostringstream rendererString;
2161
2162 rendererString << mAdapterIdentifier.Description;
2163 if (getShareHandleSupport())
2164 {
2165 rendererString << " Direct3D9Ex";
2166 }
2167 else
2168 {
2169 rendererString << " Direct3D9";
2170 }
2171
2172 rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2173 rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2174
2175 return rendererString.str();
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002176}
2177
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002178GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002179{
2180 return mAdapterIdentifier.DeviceIdentifier;
2181}
2182
Geoff Lang61e49a52013-05-29 10:22:58 -04002183Renderer9::MultisampleSupportInfo Renderer9::getMultiSampleSupport(D3DFORMAT format)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002184{
Geoff Lang61e49a52013-05-29 10:22:58 -04002185 MultisampleSupportInfo support = { 0 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002186
Geoff Lang61e49a52013-05-29 10:22:58 -04002187 for (unsigned int multiSampleIndex = 0; multiSampleIndex < ArraySize(support.supportedSamples); multiSampleIndex++)
2188 {
2189 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, TRUE,
2190 (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
2191
2192 if (SUCCEEDED(result))
2193 {
2194 support.supportedSamples[multiSampleIndex] = true;
2195 if (multiSampleIndex != D3DMULTISAMPLE_NONMASKABLE)
2196 {
2197 support.maxSupportedSamples = std::max(support.maxSupportedSamples, multiSampleIndex);
2198 }
2199 }
2200 else
2201 {
2202 support.supportedSamples[multiSampleIndex] = false;
2203 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002204 }
Geoff Lang61e49a52013-05-29 10:22:58 -04002205
2206 return support;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002207}
2208
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002209bool Renderer9::getBGRATextureSupport() const
2210{
2211 // DirectX 9 always supports BGRA
2212 return true;
2213}
2214
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002215bool Renderer9::getDXT1TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002216{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002217 return mDXT1TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002218}
2219
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002220bool Renderer9::getDXT3TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002221{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002222 return mDXT3TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002223}
2224
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002225bool Renderer9::getDXT5TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002226{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002227 return mDXT5TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002228}
2229
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002230bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002231{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002232 return mDepthTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002233}
2234
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002235bool Renderer9::getFloat32TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002236{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002237 return mFloat32TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002238}
2239
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002240bool Renderer9::getFloat32TextureFilteringSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002241{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002242 return mFloat32FilterSupport;
2243}
2244
2245bool Renderer9::getFloat32TextureRenderingSupport() const
2246{
2247 return mFloat32RenderSupport;
2248}
2249
2250bool Renderer9::getFloat16TextureSupport() const
2251{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002252 return mFloat16TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002253}
2254
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002255bool Renderer9::getFloat16TextureFilteringSupport() const
2256{
2257 return mFloat16FilterSupport;
2258}
2259
2260bool Renderer9::getFloat16TextureRenderingSupport() const
2261{
2262 return mFloat16RenderSupport;
2263}
2264
Geoff Langd42cf4e2013-06-05 16:09:17 -04002265bool Renderer9::getRGB565TextureSupport() const
2266{
2267 return mRGB565TextureSupport;
2268}
2269
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002270bool Renderer9::getLuminanceTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002271{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002272 return mLuminanceTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002273}
2274
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002275bool Renderer9::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002276{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002277 return mLuminanceAlphaTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002278}
2279
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002280bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002281{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002282 return mSupportsTextureFilterAnisotropy;
2283}
2284
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002285float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002286{
2287 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002288 {
shannon.woods%transgaming.com@gtempaccount.com6b731912013-04-13 03:35:19 +00002289 return static_cast<float>(mDeviceCaps.MaxAnisotropy);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002290 }
2291 return 1.0f;
2292}
2293
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002294bool Renderer9::getEventQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002295{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002296 return mEventQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002297}
2298
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002299unsigned int Renderer9::getMaxVertexTextureImageUnits() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002300{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002301 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2302 return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002303}
2304
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002305unsigned int Renderer9::getMaxCombinedTextureImageUnits() const
2306{
2307 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2308}
2309
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002310unsigned int Renderer9::getReservedVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002311{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002312 return 2; // dx_ViewAdjust 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::getReservedFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002316{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002317 return 3; // dx_ViewCoords, dx_DepthFront and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002318}
2319
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002320unsigned int Renderer9::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002321{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002322 return MAX_VERTEX_CONSTANT_VECTORS_D3D9 - getReservedVertexUniformVectors();
2323}
2324
2325unsigned int Renderer9::getMaxFragmentUniformVectors() const
2326{
2327 const int maxPixelConstantVectors = (getMajorShaderModel() >= 3) ? MAX_PIXEL_CONSTANT_VECTORS_SM3 : MAX_PIXEL_CONSTANT_VECTORS_SM2;
2328
2329 return maxPixelConstantVectors - getReservedFragmentUniformVectors();
2330}
2331
2332unsigned int Renderer9::getMaxVaryingVectors() const
2333{
2334 return (getMajorShaderModel() >= 3) ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002335}
2336
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002337unsigned int Renderer9::getMaxVertexShaderUniformBuffers() const
2338{
2339 return 0;
2340}
2341
2342unsigned int Renderer9::getMaxFragmentShaderUniformBuffers() const
2343{
2344 return 0;
2345}
2346
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002347unsigned int Renderer9::getReservedVertexUniformBuffers() const
2348{
2349 return 0;
2350}
2351
2352unsigned int Renderer9::getReservedFragmentUniformBuffers() const
2353{
2354 return 0;
2355}
2356
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002357unsigned int Renderer9::getMaxTransformFeedbackBuffers() const
2358{
2359 return 0;
2360}
2361
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002362unsigned int Renderer9::getMaxUniformBufferSize() const
2363{
2364 return 0;
2365}
2366
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002367bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002368{
2369 return mSupportsNonPower2Textures;
2370}
2371
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002372bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002373{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002374 return mOcclusionQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002375}
2376
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002377bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002378{
2379 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2380}
2381
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002382bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002383{
2384 // PIX doesn't seem to support using share handles, so disable them.
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002385 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002386}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002387
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002388bool Renderer9::getDerivativeInstructionSupport() const
2389{
2390 return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0;
2391}
2392
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002393bool Renderer9::getPostSubBufferSupport() const
2394{
2395 return true;
2396}
2397
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002398int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002399{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002400 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002401}
2402
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002403float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002404{
shannon.woods@transgaming.combd8c10c2013-01-25 21:15:03 +00002405 // Point size clamped at 1.0f for SM2
2406 return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f;
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002407}
2408
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002409int Renderer9::getMaxViewportDimension() const
2410{
2411 int maxTextureDimension = std::min(std::min(getMaxTextureWidth(), getMaxTextureHeight()),
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002412 (int)gl::IMPLEMENTATION_MAX_2D_TEXTURE_SIZE);
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002413 return maxTextureDimension;
2414}
2415
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002416int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002417{
2418 return (int)mDeviceCaps.MaxTextureWidth;
2419}
2420
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002421int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002422{
2423 return (int)mDeviceCaps.MaxTextureHeight;
2424}
2425
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002426int Renderer9::getMaxTextureDepth() const
2427{
2428 // 3D textures are not available in the D3D9 backend.
2429 return 1;
2430}
2431
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002432int Renderer9::getMaxTextureArrayLayers() const
2433{
2434 // 2D array textures are not available in the D3D9 backend.
2435 return 1;
2436}
2437
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002438bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002439{
2440 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2441}
2442
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002443DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002444{
2445 return mDeviceCaps.DeclTypes;
2446}
2447
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002448int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002449{
2450 return mMinSwapInterval;
2451}
2452
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002453int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002454{
2455 return mMaxSwapInterval;
2456}
2457
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002458int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002459{
2460 return mMaxSupportedSamples;
2461}
2462
Geoff Lang0e120e32013-05-29 10:23:55 -04002463GLsizei Renderer9::getMaxSupportedFormatSamples(GLint internalFormat) const
2464{
Shannon Woodsddb785c2013-07-08 10:32:13 -04002465 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
Geoff Lang0e120e32013-05-29 10:23:55 -04002466 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
2467 return (itr != mMultiSampleSupport.end()) ? mMaxSupportedSamples : 0;
2468}
2469
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002470GLsizei Renderer9::getNumSampleCounts(GLint internalFormat) const
2471{
2472 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
2473 MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format);
2474
2475 unsigned int numCounts = 0;
2476 if (iter != mMultiSampleSupport.end())
2477 {
2478 const MultisampleSupportInfo& info = iter->second;
2479 for (int i = 0; i < D3DMULTISAMPLE_16_SAMPLES; i++)
2480 {
2481 if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i])
2482 {
2483 numCounts++;
2484 }
2485 }
2486 }
2487
2488 return numCounts;
2489}
2490
2491void Renderer9::getSampleCounts(GLint internalFormat, GLsizei bufSize, GLint *params) const
2492{
2493 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
2494 MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format);
2495
2496 if (iter != mMultiSampleSupport.end())
2497 {
2498 const MultisampleSupportInfo& info = iter->second;
2499 int bufPos = 0;
2500 for (int i = D3DMULTISAMPLE_16_SAMPLES; i >= 0 && bufPos < bufSize; i--)
2501 {
2502 if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i])
2503 {
2504 params[bufPos++] = i;
2505 }
2506 }
2507 }
2508}
2509
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002510int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002511{
2512 if (requested == 0)
2513 {
2514 return requested;
2515 }
2516
Geoff Lang61e49a52013-05-29 10:22:58 -04002517 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002518 if (itr == mMultiSampleSupport.end())
2519 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002520 if (format == D3DFMT_UNKNOWN)
2521 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002522 return -1;
2523 }
2524
Geoff Lang61e49a52013-05-29 10:22:58 -04002525 for (unsigned int i = requested; i < ArraySize(itr->second.supportedSamples); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002526 {
Geoff Lang61e49a52013-05-29 10:22:58 -04002527 if (itr->second.supportedSamples[i] && i != D3DMULTISAMPLE_NONMASKABLE)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002528 {
2529 return i;
2530 }
2531 }
2532
2533 return -1;
2534}
2535
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002536unsigned int Renderer9::getMaxRenderTargets() const
2537{
2538 // we do not support MRT in d3d9
2539 return 1;
2540}
2541
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002542D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2543{
2544 switch (internalformat)
2545 {
2546 case GL_DEPTH_COMPONENT16:
2547 case GL_DEPTH_COMPONENT32_OES:
2548 case GL_DEPTH24_STENCIL8_OES:
2549 return D3DFMT_INTZ;
2550 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2551 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2552 return D3DFMT_DXT1;
2553 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2554 return D3DFMT_DXT3;
2555 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2556 return D3DFMT_DXT5;
2557 case GL_RGBA32F_EXT:
2558 case GL_RGB32F_EXT:
2559 case GL_ALPHA32F_EXT:
2560 case GL_LUMINANCE32F_EXT:
2561 case GL_LUMINANCE_ALPHA32F_EXT:
2562 return D3DFMT_A32B32G32R32F;
2563 case GL_RGBA16F_EXT:
2564 case GL_RGB16F_EXT:
2565 case GL_ALPHA16F_EXT:
2566 case GL_LUMINANCE16F_EXT:
2567 case GL_LUMINANCE_ALPHA16F_EXT:
2568 return D3DFMT_A16B16G16R16F;
2569 case GL_LUMINANCE8_EXT:
2570 if (getLuminanceTextureSupport())
2571 {
2572 return D3DFMT_L8;
2573 }
2574 break;
2575 case GL_LUMINANCE8_ALPHA8_EXT:
2576 if (getLuminanceAlphaTextureSupport())
2577 {
2578 return D3DFMT_A8L8;
2579 }
2580 break;
2581 case GL_RGB8_OES:
2582 case GL_RGB565:
2583 return D3DFMT_X8R8G8B8;
2584 }
2585
2586 return D3DFMT_A8R8G8B8;
2587}
2588
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002589bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002590{
2591 bool result = false;
2592
2593 if (source && dest)
2594 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002595 TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance());
2596 TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002597
2598 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002599 for (int i = 0; i < levels; ++i)
2600 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002601 IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false);
2602 IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002603
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002604 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002605
2606 if (srcSurf) srcSurf->Release();
2607 if (dstSurf) dstSurf->Release();
2608
2609 if (!result)
2610 return false;
2611 }
2612 }
2613
2614 return result;
2615}
2616
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002617bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002618{
2619 bool result = false;
2620
2621 if (source && dest)
2622 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002623 TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance());
2624 TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002625 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002626 for (int f = 0; f < 6; f++)
2627 {
2628 for (int i = 0; i < levels; i++)
2629 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002630 IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2631 IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002632
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002633 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002634
2635 if (srcSurf) srcSurf->Release();
2636 if (dstSurf) dstSurf->Release();
2637
2638 if (!result)
2639 return false;
2640 }
2641 }
2642 }
2643
2644 return result;
2645}
2646
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002647bool Renderer9::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2648{
2649 // 3D textures are not available in the D3D9 backend.
2650 UNREACHABLE();
2651 return false;
2652}
2653
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002654bool Renderer9::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2655{
2656 // 2D array textures are not supported by the D3D9 backend.
2657 UNREACHABLE();
2658 return false;
2659}
2660
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002661D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002662{
2663 if (mD3d9Ex != NULL)
2664 {
2665 return D3DPOOL_DEFAULT;
2666 }
2667 else
2668 {
2669 if (!(usage & D3DUSAGE_DYNAMIC))
2670 {
2671 return D3DPOOL_MANAGED;
2672 }
2673 }
2674
2675 return D3DPOOL_DEFAULT;
2676}
2677
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002678bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002679 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002680{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002681 RECT rect;
2682 rect.left = sourceRect.x;
2683 rect.top = sourceRect.y;
2684 rect.right = sourceRect.x + sourceRect.width;
2685 rect.bottom = sourceRect.y + sourceRect.height;
2686
2687 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002688}
2689
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002690bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002691 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002692{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002693 RECT rect;
2694 rect.left = sourceRect.x;
2695 rect.top = sourceRect.y;
2696 rect.right = sourceRect.x + sourceRect.width;
2697 rect.bottom = sourceRect.y + sourceRect.height;
2698
2699 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002700}
2701
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002702bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2703 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2704{
2705 // 3D textures are not available in the D3D9 backend.
2706 UNREACHABLE();
2707 return false;
2708}
2709
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002710bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2711 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2712{
2713 // 2D array textures are not available in the D3D9 backend.
2714 UNREACHABLE();
2715 return false;
2716}
2717
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002718bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, const gl::Rectangle &readRect, gl::Framebuffer *drawFramebuffer, const gl::Rectangle &drawRect,
Geoff Lang685806d2013-06-12 11:16:36 -04002719 bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002720{
Geoff Lang758d5b22013-06-11 11:42:50 -04002721 ASSERT(filter == GL_NEAREST);
2722
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002723 endScene();
2724
2725 if (blitRenderTarget)
2726 {
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002727 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer(0);
2728 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002729 RenderTarget9 *readRenderTarget = NULL;
2730 RenderTarget9 *drawRenderTarget = NULL;
2731 IDirect3DSurface9* readSurface = NULL;
2732 IDirect3DSurface9* drawSurface = NULL;
2733
2734 if (readBuffer)
2735 {
2736 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2737 }
2738 if (drawBuffer)
2739 {
2740 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2741 }
2742
2743 if (readRenderTarget)
2744 {
2745 readSurface = readRenderTarget->getSurface();
2746 }
2747 if (drawRenderTarget)
2748 {
2749 drawSurface = drawRenderTarget->getSurface();
2750 }
2751
2752 if (!readSurface || !drawSurface)
2753 {
2754 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002755 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002756 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002757
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002758 RECT srcRect;
2759 srcRect.left = readRect.x;
2760 srcRect.right = readRect.x + readRect.width;
2761 srcRect.top = readRect.y;
2762 srcRect.bottom = readRect.y + readRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002763
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002764 RECT dstRect;
2765 dstRect.left = drawRect.x;
2766 dstRect.right = drawRect.x + drawRect.width;
2767 dstRect.top = drawRect.y;
2768 dstRect.bottom = drawRect.y + drawRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002769
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002770 HRESULT result = mDevice->StretchRect(readSurface, &srcRect, drawSurface, &dstRect, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002771
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002772 readSurface->Release();
2773 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002774
2775 if (FAILED(result))
2776 {
2777 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2778 return false;
2779 }
2780 }
2781
Geoff Lang685806d2013-06-12 11:16:36 -04002782 if (blitDepth || blitStencil)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002783 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002784 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2785 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2786 RenderTarget9 *readDepthStencil = NULL;
2787 RenderTarget9 *drawDepthStencil = NULL;
2788 IDirect3DSurface9* readSurface = NULL;
2789 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002790
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002791 if (readBuffer)
2792 {
2793 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2794 }
2795 if (drawBuffer)
2796 {
2797 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2798 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002799
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002800 if (readDepthStencil)
2801 {
2802 readSurface = readDepthStencil->getSurface();
2803 }
2804 if (drawDepthStencil)
2805 {
2806 drawSurface = drawDepthStencil->getSurface();
2807 }
2808
2809 if (!readSurface || !drawSurface)
2810 {
2811 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002812 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002813 }
2814
2815 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2816
2817 readSurface->Release();
2818 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002819
2820 if (FAILED(result))
2821 {
2822 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2823 return false;
2824 }
2825 }
2826
2827 return true;
2828}
2829
2830void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2831 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2832{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002833 RenderTarget9 *renderTarget = NULL;
2834 IDirect3DSurface9 *surface = NULL;
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002835 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002836
2837 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002838 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002839 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2840 }
2841
2842 if (renderTarget)
2843 {
2844 surface = renderTarget->getSurface();
2845 }
2846
2847 if (!surface)
2848 {
2849 // context must be lost
2850 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002851 }
2852
2853 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002854 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002855
2856 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2857 {
2858 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002859 surface->Release();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002860 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002861 }
2862
2863 HRESULT result;
2864 IDirect3DSurface9 *systemSurface = NULL;
2865 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2866 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2867 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2868 if (directToPixels)
2869 {
2870 // Use the pixels ptr as a shared handle to write directly into client's memory
2871 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2872 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2873 if (FAILED(result))
2874 {
2875 // Try again without the shared handle
2876 directToPixels = false;
2877 }
2878 }
2879
2880 if (!directToPixels)
2881 {
2882 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2883 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2884 if (FAILED(result))
2885 {
2886 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002887 surface->Release();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002888 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002889 }
2890 }
2891
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002892 result = mDevice->GetRenderTargetData(surface, systemSurface);
2893 surface->Release();
2894 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002895
2896 if (FAILED(result))
2897 {
2898 systemSurface->Release();
2899
2900 // It turns out that D3D will sometimes produce more error
2901 // codes than those documented.
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002902 if (d3d9::isDeviceLostError(result))
2903 {
2904 notifyDeviceLost();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002905 return gl::error(GL_OUT_OF_MEMORY);
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002906 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002907 else
2908 {
2909 UNREACHABLE();
2910 return;
2911 }
2912
2913 }
2914
2915 if (directToPixels)
2916 {
2917 systemSurface->Release();
2918 return;
2919 }
2920
2921 RECT rect;
2922 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2923 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2924 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2925 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2926
2927 D3DLOCKED_RECT lock;
2928 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2929
2930 if (FAILED(result))
2931 {
2932 UNREACHABLE();
2933 systemSurface->Release();
2934
2935 return; // No sensible error to generate
2936 }
2937
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002938 unsigned char *source;
2939 int inputPitch;
2940 if (packReverseRowOrder)
2941 {
2942 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2943 inputPitch = -lock.Pitch;
2944 }
2945 else
2946 {
2947 source = (unsigned char*)lock.pBits;
2948 inputPitch = lock.Pitch;
2949 }
2950
Geoff Lang697ad3e2013-06-04 10:11:28 -04002951 GLuint clientVersion = getCurrentClientVersion();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002952
Geoff Lang697ad3e2013-06-04 10:11:28 -04002953 GLint sourceInternalFormat = d3d9_gl::GetInternalFormat(desc.Format);
2954 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
2955 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002956
Geoff Lang697ad3e2013-06-04 10:11:28 -04002957 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
2958
2959 if (sourceFormat == format && sourceType == type)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002960 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002961 // Direct copy possible
2962 unsigned char *dest = static_cast<unsigned char*>(pixels);
2963 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002964 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002965 memcpy(dest + y * outputPitch, source + y * inputPitch, (rect.right - rect.left) * sourcePixelSize);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002966 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04002967 }
2968 else
2969 {
2970 GLint destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
2971 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
2972 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
2973
2974 ColorCopyFunction fastCopyFunc = d3d9::GetFastCopyFunction(desc.Format, format, type, getCurrentClientVersion());
2975 if (fastCopyFunc)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002976 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002977 // Fast copy is possible through some special function
2978 for (int y = 0; y < rect.bottom - rect.top; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002979 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002980 for (int x = 0; x < rect.right - rect.left; x++)
2981 {
2982 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
2983 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
2984
2985 fastCopyFunc(src, dest);
2986 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002987 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002988 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04002989 else
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002990 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002991 ColorReadFunction readFunc = d3d9::GetColorReadFunction(desc.Format);
2992 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002993
Geoff Lang697ad3e2013-06-04 10:11:28 -04002994 gl::ColorF temp;
2995
2996 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002997 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002998 for (int x = 0; x < rect.right - rect.left; x++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002999 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003000 void *dest = reinterpret_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3001 void *src = source + y * inputPitch + x * sourcePixelSize;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003002
Geoff Lang697ad3e2013-06-04 10:11:28 -04003003 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3004 // will not allow the copy otherwise.
3005 readFunc(src, &temp);
3006 writeFunc(&temp, dest);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003007 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003008 }
3009 }
3010 }
3011
3012 systemSurface->UnlockRect();
3013
3014 systemSurface->Release();
3015}
3016
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003017RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
3018{
3019 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3020 IDirect3DSurface9 *surface = NULL;
3021 if (depth)
3022 {
3023 surface = swapChain9->getDepthStencil();
3024 }
3025 else
3026 {
3027 surface = swapChain9->getRenderTarget();
3028 }
3029
3030 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
3031
3032 return renderTarget;
3033}
3034
Geoff Langa2d97f12013-06-11 11:44:02 -04003035RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003036{
3037 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
3038 return renderTarget;
3039}
3040
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003041ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00003042{
3043 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com55318902012-11-28 20:58:58 +00003044
3045 switch (type)
3046 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003047 case rx::SHADER_VERTEX:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003048 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003049 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003050 if (vshader)
3051 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003052 executable = new ShaderExecutable9(function, length, vshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003053 }
3054 }
3055 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003056 case rx::SHADER_PIXEL:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003057 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003058 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003059 if (pshader)
3060 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003061 executable = new ShaderExecutable9(function, length, pshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003062 }
3063 }
3064 break;
3065 default:
3066 UNREACHABLE();
3067 break;
3068 }
3069
3070 return executable;
3071}
3072
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003073ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003074{
3075 const char *profile = NULL;
3076
3077 switch (type)
3078 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003079 case rx::SHADER_VERTEX:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003080 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
3081 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003082 case rx::SHADER_PIXEL:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003083 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
3084 break;
3085 default:
3086 UNREACHABLE();
3087 return NULL;
3088 }
3089
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00003090 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, ANGLE_COMPILE_OPTIMIZATION_LEVEL, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003091 if (!binary)
3092 return NULL;
3093
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003094 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com536dd6e2012-11-28 21:00:18 +00003095 binary->Release();
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003096
3097 return executable;
3098}
3099
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00003100bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
3101{
3102 return mBlit->boxFilter(source, dest);
3103}
3104
daniel@transgaming.com2507f412012-10-31 18:46:48 +00003105D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003106{
3107 if (mD3d9Ex != NULL)
3108 {
3109 return D3DPOOL_DEFAULT;
3110 }
3111 else
3112 {
3113 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
3114 {
3115 return D3DPOOL_MANAGED;
3116 }
3117 }
3118
3119 return D3DPOOL_DEFAULT;
3120}
3121
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003122bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
3123{
3124 if (source && dest)
3125 {
3126 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003127
3128 if (fromManaged)
3129 {
3130 D3DSURFACE_DESC desc;
3131 source->GetDesc(&desc);
3132
3133 IDirect3DSurface9 *surf = 0;
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003134 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003135
3136 if (SUCCEEDED(result))
3137 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003138 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003139 result = mDevice->UpdateSurface(surf, NULL, dest, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003140 surf->Release();
3141 }
3142 }
3143 else
3144 {
3145 endScene();
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003146 result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003147 }
3148
3149 if (FAILED(result))
3150 {
3151 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
3152 return false;
3153 }
3154 }
3155
3156 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00003157}
3158
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003159Image *Renderer9::createImage()
3160{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003161 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003162}
3163
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003164void Renderer9::generateMipmap(Image *dest, Image *src)
3165{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003166 Image9 *src9 = Image9::makeImage9(src);
3167 Image9 *dst9 = Image9::makeImage9(dest);
3168 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003169}
3170
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003171TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain)
3172{
3173 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3174 return new TextureStorage9_2D(this, swapChain9);
3175}
3176
3177TextureStorage *Renderer9::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3178{
3179 return new TextureStorage9_2D(this, levels, internalformat, usage, forceRenderable, width, height);
3180}
3181
3182TextureStorage *Renderer9::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3183{
3184 return new TextureStorage9_Cube(this, levels, internalformat, usage, forceRenderable, size);
3185}
3186
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003187TextureStorage *Renderer9::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3188{
3189 // 3D textures are not supported by the D3D9 backend.
3190 UNREACHABLE();
3191
3192 return NULL;
3193}
3194
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003195TextureStorage *Renderer9::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3196{
3197 // 2D array textures are not supported by the D3D9 backend.
3198 UNREACHABLE();
3199
3200 return NULL;
3201}
3202
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003203bool Renderer9::getLUID(LUID *adapterLuid) const
3204{
3205 adapterLuid->HighPart = 0;
3206 adapterLuid->LowPart = 0;
3207
3208 if (mD3d9Ex)
3209 {
3210 mD3d9Ex->GetAdapterLUID(mAdapter, adapterLuid);
3211 return true;
3212 }
3213
3214 return false;
3215}
3216
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003217}