blob: e1b4314a28647ff82753a06ad6c07e6dba710e45 [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 Lang57e713e2013-07-31 17:01:58 -04001464 // Checked by Renderer9::applyPrimitiveType
1465 ASSERT(count >= 0);
1466
1467 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001468 {
1469 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1470 return gl::error(GL_OUT_OF_MEMORY);
1471 }
1472
Geoff Lang57e713e2013-07-31 17:01:58 -04001473 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001474 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001475 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001476 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001477 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001478 }
1479
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001480 void* mappedMemory = NULL;
1481 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1482 if (offset == -1 || mappedMemory == NULL)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001483 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001484 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001485 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001486 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001487
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001488 startIndex = static_cast<UINT>(offset) / 4;
1489 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1490
1491 switch (type)
1492 {
1493 case GL_NONE: // Non-indexed draw
1494 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001495 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001496 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001497 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001498 data[count] = 0;
1499 break;
1500 case GL_UNSIGNED_BYTE:
1501 for (int i = 0; i < count; i++)
1502 {
1503 data[i] = static_cast<const GLubyte*>(indices)[i];
1504 }
1505 data[count] = static_cast<const GLubyte*>(indices)[0];
1506 break;
1507 case GL_UNSIGNED_SHORT:
1508 for (int i = 0; i < count; i++)
1509 {
1510 data[i] = static_cast<const GLushort*>(indices)[i];
1511 }
1512 data[count] = static_cast<const GLushort*>(indices)[0];
1513 break;
1514 case GL_UNSIGNED_INT:
1515 for (int i = 0; i < count; i++)
1516 {
1517 data[i] = static_cast<const GLuint*>(indices)[i];
1518 }
1519 data[count] = static_cast<const GLuint*>(indices)[0];
1520 break;
1521 default: UNREACHABLE();
1522 }
1523
1524 if (!mLineLoopIB->unmapBuffer())
1525 {
1526 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001527 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001528 }
1529 }
1530 else
1531 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001532 if (!mLineLoopIB)
1533 {
1534 mLineLoopIB = new StreamingIndexBufferInterface(this);
1535 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
1536 {
1537 delete mLineLoopIB;
1538 mLineLoopIB = NULL;
1539
1540 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001541 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001542 }
1543 }
1544
Geoff Lang57e713e2013-07-31 17:01:58 -04001545 // Checked by Renderer9::applyPrimitiveType
1546 ASSERT(count >= 0);
1547
1548 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned short>::max() / sizeof(unsigned short)))
Geoff Langeadfd572013-07-09 15:55:07 -04001549 {
1550 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1551 return gl::error(GL_OUT_OF_MEMORY);
1552 }
1553
Geoff Lang57e713e2013-07-31 17:01:58 -04001554 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned short);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001555 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001556 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001557 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001558 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001559 }
1560
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001561 void* mappedMemory = NULL;
1562 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1563 if (offset == -1 || mappedMemory == NULL)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001564 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001565 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001566 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001567 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001568
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001569 startIndex = static_cast<UINT>(offset) / 2;
1570 unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory);
1571
1572 switch (type)
1573 {
1574 case GL_NONE: // Non-indexed draw
1575 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001576 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001577 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001578 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001579 data[count] = 0;
1580 break;
1581 case GL_UNSIGNED_BYTE:
1582 for (int i = 0; i < count; i++)
1583 {
1584 data[i] = static_cast<const GLubyte*>(indices)[i];
1585 }
1586 data[count] = static_cast<const GLubyte*>(indices)[0];
1587 break;
1588 case GL_UNSIGNED_SHORT:
1589 for (int i = 0; i < count; i++)
1590 {
1591 data[i] = static_cast<const GLushort*>(indices)[i];
1592 }
1593 data[count] = static_cast<const GLushort*>(indices)[0];
1594 break;
1595 case GL_UNSIGNED_INT:
1596 for (int i = 0; i < count; i++)
1597 {
1598 data[i] = static_cast<const GLuint*>(indices)[i];
1599 }
1600 data[count] = static_cast<const GLuint*>(indices)[0];
1601 break;
1602 default: UNREACHABLE();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001603 }
1604
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001605 if (!mLineLoopIB->unmapBuffer())
1606 {
1607 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001608 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001609 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001610 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001611
1612 if (mAppliedIBSerial != mLineLoopIB->getSerial())
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001613 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001614 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer());
1615
1616 mDevice->SetIndices(indexBuffer->getBuffer());
1617 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001618 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001619
1620 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001621}
1622
shannon.woods@transgaming.come1602ae2013-02-28 23:17:38 +00001623template <typename T>
1624static void drawPoints(IDirect3DDevice9* device, GLsizei count, const GLvoid *indices)
1625{
1626 for (int i = 0; i < count; i++)
1627 {
1628 unsigned int indexValue = static_cast<unsigned int>(static_cast<const T*>(indices)[i]);
1629 device->DrawPrimitive(D3DPT_POINTLIST, indexValue, 1);
1630 }
1631}
1632
1633void Renderer9::drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer)
1634{
1635 // Drawing index point lists is unsupported in d3d9, fall back to a regular DrawPrimitive call
1636 // for each individual point. This call is not expected to happen often.
1637
1638 if (elementArrayBuffer)
1639 {
1640 BufferStorage *storage = elementArrayBuffer->getStorage();
1641 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1642 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
1643 }
1644
1645 switch (type)
1646 {
1647 case GL_UNSIGNED_BYTE: drawPoints<GLubyte>(mDevice, count, indices); break;
1648 case GL_UNSIGNED_SHORT: drawPoints<GLushort>(mDevice, count, indices); break;
1649 case GL_UNSIGNED_INT: drawPoints<GLuint>(mDevice, count, indices); break;
1650 default: UNREACHABLE();
1651 }
1652}
1653
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001654void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1655{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001656 unsigned int programBinarySerial = programBinary->getSerial();
1657 if (programBinarySerial != mAppliedProgramBinarySerial)
1658 {
1659 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1660 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001661
daniel@transgaming.come4991412012-12-20 20:55:34 +00001662 IDirect3DVertexShader9 *vertexShader = NULL;
1663 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001664
daniel@transgaming.come4991412012-12-20 20:55:34 +00001665 IDirect3DPixelShader9 *pixelShader = NULL;
1666 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001667
daniel@transgaming.come4991412012-12-20 20:55:34 +00001668 mDevice->SetPixelShader(pixelShader);
1669 mDevice->SetVertexShader(vertexShader);
1670 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001671 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001672
1673 mAppliedProgramBinarySerial = programBinarySerial;
1674 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001675}
1676
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001677void Renderer9::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001678{
1679 for (std::vector<gl::Uniform*>::const_iterator ub = uniformArray->begin(), ue = uniformArray->end(); ub != ue; ++ub)
1680 {
1681 gl::Uniform *targetUniform = *ub;
1682
1683 if (targetUniform->dirty)
1684 {
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001685 GLfloat *f = (GLfloat*)targetUniform->data;
1686 GLint *i = (GLint*)targetUniform->data;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001687
1688 switch (targetUniform->type)
1689 {
1690 case GL_SAMPLER_2D:
1691 case GL_SAMPLER_CUBE:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001692 break;
1693 case GL_BOOL:
1694 case GL_BOOL_VEC2:
1695 case GL_BOOL_VEC3:
1696 case GL_BOOL_VEC4:
1697 applyUniformnbv(targetUniform, i);
1698 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001699 case GL_FLOAT:
1700 case GL_FLOAT_VEC2:
1701 case GL_FLOAT_VEC3:
1702 case GL_FLOAT_VEC4:
1703 case GL_FLOAT_MAT2:
1704 case GL_FLOAT_MAT3:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001705 case GL_FLOAT_MAT4:
1706 applyUniformnfv(targetUniform, f);
1707 break;
1708 case GL_INT:
1709 case GL_INT_VEC2:
1710 case GL_INT_VEC3:
1711 case GL_INT_VEC4:
1712 applyUniformniv(targetUniform, i);
1713 break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001714 default:
1715 UNREACHABLE();
1716 }
1717
1718 targetUniform->dirty = false;
1719 }
1720 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001721
1722 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001723 if (mDxUniformsDirty)
1724 {
1725 mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4]));
1726 mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4]));
1727 mDxUniformsDirty = false;
1728 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001729}
1730
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001731void Renderer9::applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001732{
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001733 if (targetUniform->isReferencedByFragmentShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001734 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001735 mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001736 }
1737
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001738 if (targetUniform->isReferencedByVertexShader())
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001739 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001740 mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001741 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001742}
1743
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001744void Renderer9::applyUniformniv(gl::Uniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001745{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001746 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1747 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001748
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001749 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001750 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001751 vector[i][0] = (GLfloat)v[4 * i + 0];
1752 vector[i][1] = (GLfloat)v[4 * i + 1];
1753 vector[i][2] = (GLfloat)v[4 * i + 2];
1754 vector[i][3] = (GLfloat)v[4 * i + 3];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001755 }
1756
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001757 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001758}
1759
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001760void Renderer9::applyUniformnbv(gl::Uniform *targetUniform, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001761{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001762 ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
1763 GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001764
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001765 for (unsigned int i = 0; i < targetUniform->registerCount; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001766 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001767 vector[i][0] = (v[4 * i + 0] == GL_FALSE) ? 0.0f : 1.0f;
1768 vector[i][1] = (v[4 * i + 1] == GL_FALSE) ? 0.0f : 1.0f;
1769 vector[i][2] = (v[4 * i + 2] == GL_FALSE) ? 0.0f : 1.0f;
1770 vector[i][3] = (v[4 * i + 3] == GL_FALSE) ? 0.0f : 1.0f;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001771 }
1772
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001773 applyUniformnfv(targetUniform, (GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001774}
1775
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001776void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001777{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001778 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1779 gl::unorm<8>(clearParams.colorClearValue.red),
1780 gl::unorm<8>(clearParams.colorClearValue.green),
1781 gl::unorm<8>(clearParams.colorClearValue.blue));
1782 float depth = gl::clamp01(clearParams.depthClearValue);
1783 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001784
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001785 unsigned int stencilUnmasked = 0x0;
1786 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1787 {
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001788 unsigned int stencilSize = gl::GetStencilBits(frameBuffer->getStencilbuffer()->getActualFormat(),
1789 getCurrentClientVersion());
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001790 stencilUnmasked = (0x1 << stencilSize) - 1;
1791 }
1792
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001793 bool alphaUnmasked = gl::GetAlphaBits(mRenderTargetDesc.format, getCurrentClientVersion()) == 0 ||
1794 clearParams.colorMaskAlpha;
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001795
1796 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1797 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1798 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1799 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1800 clearParams.colorMaskBlue && alphaUnmasked);
1801
1802 if (needMaskedColorClear || needMaskedStencilClear)
1803 {
1804 // State which is altered in all paths from this point to the clear call is saved.
1805 // State which is altered in only some paths will be flagged dirty in the case that
1806 // that path is taken.
1807 HRESULT hr;
1808 if (mMaskedClearSavedState == NULL)
1809 {
1810 hr = mDevice->BeginStateBlock();
1811 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1812
1813 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1814 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1815 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1816 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1817 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1818 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1819 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1820 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1821 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1822 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1823 mDevice->SetPixelShader(NULL);
1824 mDevice->SetVertexShader(NULL);
1825 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1826 mDevice->SetStreamSource(0, NULL, 0, 0);
1827 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1828 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1829 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1830 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1831 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1832 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1833 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1834
1835 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1836 {
1837 mDevice->SetStreamSourceFreq(i, 1);
1838 }
1839
1840 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1841 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1842 }
1843
1844 ASSERT(mMaskedClearSavedState != NULL);
1845
1846 if (mMaskedClearSavedState != NULL)
1847 {
1848 hr = mMaskedClearSavedState->Capture();
1849 ASSERT(SUCCEEDED(hr));
1850 }
1851
1852 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1853 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1854 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1855 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1856 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1857 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1858 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1859 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1860
1861 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1862 {
1863 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1864 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1865 clearParams.colorMaskGreen,
1866 clearParams.colorMaskBlue,
1867 clearParams.colorMaskAlpha));
1868 }
1869 else
1870 {
1871 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1872 }
1873
1874 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1875 {
1876 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1877 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1878 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1879 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1880 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1881 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1882 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1883 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1884 }
1885 else
1886 {
1887 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1888 }
1889
1890 mDevice->SetPixelShader(NULL);
1891 mDevice->SetVertexShader(NULL);
1892 mDevice->SetFVF(D3DFVF_XYZRHW);
1893 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1894 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1895 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1896 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1897 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1898 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1899 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1900
1901 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1902 {
1903 mDevice->SetStreamSourceFreq(i, 1);
1904 }
1905
1906 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1907 quad[0][0] = -0.5f;
1908 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1909 quad[0][2] = 0.0f;
1910 quad[0][3] = 1.0f;
1911
1912 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1913 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1914 quad[1][2] = 0.0f;
1915 quad[1][3] = 1.0f;
1916
1917 quad[2][0] = -0.5f;
1918 quad[2][1] = -0.5f;
1919 quad[2][2] = 0.0f;
1920 quad[2][3] = 1.0f;
1921
1922 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1923 quad[3][1] = -0.5f;
1924 quad[3][2] = 0.0f;
1925 quad[3][3] = 1.0f;
1926
1927 startScene();
1928 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1929
1930 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1931 {
1932 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1933 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1934 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1935 }
1936
1937 if (mMaskedClearSavedState != NULL)
1938 {
1939 mMaskedClearSavedState->Apply();
1940 }
1941 }
1942 else if (clearParams.mask)
1943 {
1944 DWORD dxClearFlags = 0;
1945 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1946 {
1947 dxClearFlags |= D3DCLEAR_TARGET;
1948 }
1949 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1950 {
1951 dxClearFlags |= D3DCLEAR_ZBUFFER;
1952 }
1953 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1954 {
1955 dxClearFlags |= D3DCLEAR_STENCIL;
1956 }
1957
1958 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1959 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001960}
1961
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001962void Renderer9::markAllStateDirty()
1963{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001964 mAppliedRenderTargetSerial = 0;
1965 mAppliedDepthbufferSerial = 0;
1966 mAppliedStencilbufferSerial = 0;
1967 mDepthStencilInitialized = false;
1968 mRenderTargetDescInitialized = false;
1969
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001970 mForceSetDepthStencilState = true;
1971 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001972 mForceSetScissor = true;
1973 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001974 mForceSetBlendState = true;
1975
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001976 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001977 {
1978 mForceSetVertexSamplerStates[i] = true;
1979 mCurVertexTextureSerials[i] = 0;
1980 }
1981 for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1982 {
1983 mForceSetPixelSamplerStates[i] = true;
1984 mCurPixelTextureSerials[i] = 0;
1985 }
1986
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001987 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001988 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001989 mDxUniformsDirty = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001990
1991 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001992}
1993
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001994void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001995{
1996 while (!mEventQueryPool.empty())
1997 {
1998 mEventQueryPool.back()->Release();
1999 mEventQueryPool.pop_back();
2000 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +00002001
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00002002 if (mMaskedClearSavedState)
2003 {
2004 mMaskedClearSavedState->Release();
2005 mMaskedClearSavedState = NULL;
2006 }
2007
daniel@transgaming.come4733d72012-10-31 18:07:01 +00002008 mVertexShaderCache.clear();
2009 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002010
daniel@transgaming.come569fc52012-11-28 20:56:02 +00002011 delete mBlit;
2012 mBlit = NULL;
2013
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00002014 delete mVertexDataManager;
2015 mVertexDataManager = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00002016
2017 delete mIndexDataManager;
2018 mIndexDataManager = NULL;
2019
2020 delete mLineLoopIB;
2021 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00002022
2023 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
2024 {
2025 delete mNullColorbufferCache[i].buffer;
2026 mNullColorbufferCache[i].buffer = NULL;
2027 }
2028
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002029}
2030
2031
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002032void Renderer9::notifyDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002033{
2034 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002035 mDisplay->notifyDeviceLost();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002036}
2037
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002038bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002039{
2040 return mDeviceLost;
2041}
2042
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002043// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002044bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002045{
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002046 HRESULT status = S_OK;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002047
2048 if (mDeviceEx)
2049 {
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002050 status = mDeviceEx->CheckDeviceState(NULL);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002051 }
2052 else if (mDevice)
2053 {
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002054 status = mDevice->TestCooperativeLevel();
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002055 }
2056 else
2057 {
2058 // No device yet, so no reset required
2059 }
2060
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002061 bool isLost = FAILED(status) || d3d9::isDeviceLostError(status);
2062
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002063 if (isLost)
2064 {
2065 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002066 // we'll probably get this done again by notifyDeviceLost
2067 // but best to remember it!
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002068 // Note that we don't want to clear the device loss status here
2069 // -- this needs to be done by resetDevice
2070 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002071 if (notify)
2072 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002073 notifyDeviceLost();
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002074 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002075 }
2076
2077 return isLost;
2078}
2079
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002080bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002081{
2082 HRESULT status = D3D_OK;
2083
2084 if (mDeviceEx)
2085 {
2086 status = mDeviceEx->CheckDeviceState(NULL);
2087 }
2088 else if (mDevice)
2089 {
2090 status = mDevice->TestCooperativeLevel();
2091 }
2092
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002093 // On D3D9Ex, DEVICELOST represents a hung device that needs to be restarted
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002094 // DEVICEREMOVED indicates the device has been stopped and must be recreated
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002095 switch (status)
2096 {
2097 case D3DERR_DEVICENOTRESET:
2098 case D3DERR_DEVICEHUNG:
2099 return true;
shannon.woods@transgaming.coma4ba59c2013-02-28 23:14:38 +00002100 case D3DERR_DEVICELOST:
2101 return (mDeviceEx != NULL);
2102 case D3DERR_DEVICEREMOVED:
2103 UNIMPLEMENTED();
2104 return false;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002105 default:
2106 return false;
2107 }
2108}
2109
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002110bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002111{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002112 releaseDeviceResources();
2113
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002114 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2115
2116 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002117 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002118 int attempts = 3;
2119
2120 while (lost && attempts > 0)
2121 {
2122 if (mDeviceEx)
2123 {
2124 Sleep(500); // Give the graphics driver some CPU time
2125 result = mDeviceEx->ResetEx(&presentParameters, NULL);
2126 }
2127 else
2128 {
2129 result = mDevice->TestCooperativeLevel();
2130 while (result == D3DERR_DEVICELOST)
2131 {
2132 Sleep(100); // Give the graphics driver some CPU time
2133 result = mDevice->TestCooperativeLevel();
2134 }
2135
2136 if (result == D3DERR_DEVICENOTRESET)
2137 {
2138 result = mDevice->Reset(&presentParameters);
2139 }
2140 }
2141
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002142 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002143 attempts --;
2144 }
2145
2146 if (FAILED(result))
2147 {
2148 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
2149 return false;
2150 }
2151
2152 // reset device defaults
2153 initializeDevice();
2154 mDeviceLost = false;
2155
2156 return true;
2157}
2158
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002159DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002160{
2161 return mAdapterIdentifier.VendorId;
2162}
2163
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002164std::string Renderer9::getRendererDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002165{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002166 std::ostringstream rendererString;
2167
2168 rendererString << mAdapterIdentifier.Description;
2169 if (getShareHandleSupport())
2170 {
2171 rendererString << " Direct3D9Ex";
2172 }
2173 else
2174 {
2175 rendererString << " Direct3D9";
2176 }
2177
2178 rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2179 rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2180
2181 return rendererString.str();
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002182}
2183
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002184GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002185{
2186 return mAdapterIdentifier.DeviceIdentifier;
2187}
2188
Geoff Lang61e49a52013-05-29 10:22:58 -04002189Renderer9::MultisampleSupportInfo Renderer9::getMultiSampleSupport(D3DFORMAT format)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002190{
Geoff Lang61e49a52013-05-29 10:22:58 -04002191 MultisampleSupportInfo support = { 0 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002192
Geoff Lang61e49a52013-05-29 10:22:58 -04002193 for (unsigned int multiSampleIndex = 0; multiSampleIndex < ArraySize(support.supportedSamples); multiSampleIndex++)
2194 {
2195 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, TRUE,
2196 (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
2197
2198 if (SUCCEEDED(result))
2199 {
2200 support.supportedSamples[multiSampleIndex] = true;
2201 if (multiSampleIndex != D3DMULTISAMPLE_NONMASKABLE)
2202 {
2203 support.maxSupportedSamples = std::max(support.maxSupportedSamples, multiSampleIndex);
2204 }
2205 }
2206 else
2207 {
2208 support.supportedSamples[multiSampleIndex] = false;
2209 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002210 }
Geoff Lang61e49a52013-05-29 10:22:58 -04002211
2212 return support;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002213}
2214
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002215bool Renderer9::getBGRATextureSupport() const
2216{
2217 // DirectX 9 always supports BGRA
2218 return true;
2219}
2220
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002221bool Renderer9::getDXT1TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002222{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002223 return mDXT1TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002224}
2225
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002226bool Renderer9::getDXT3TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002227{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002228 return mDXT3TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002229}
2230
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002231bool Renderer9::getDXT5TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002232{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002233 return mDXT5TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002234}
2235
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002236bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002237{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002238 return mDepthTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002239}
2240
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002241bool Renderer9::getFloat32TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002242{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002243 return mFloat32TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002244}
2245
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002246bool Renderer9::getFloat32TextureFilteringSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002247{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002248 return mFloat32FilterSupport;
2249}
2250
2251bool Renderer9::getFloat32TextureRenderingSupport() const
2252{
2253 return mFloat32RenderSupport;
2254}
2255
2256bool Renderer9::getFloat16TextureSupport() const
2257{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002258 return mFloat16TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002259}
2260
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002261bool Renderer9::getFloat16TextureFilteringSupport() const
2262{
2263 return mFloat16FilterSupport;
2264}
2265
2266bool Renderer9::getFloat16TextureRenderingSupport() const
2267{
2268 return mFloat16RenderSupport;
2269}
2270
Geoff Langd42cf4e2013-06-05 16:09:17 -04002271bool Renderer9::getRGB565TextureSupport() const
2272{
2273 return mRGB565TextureSupport;
2274}
2275
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002276bool Renderer9::getLuminanceTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002277{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002278 return mLuminanceTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002279}
2280
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002281bool Renderer9::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002282{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002283 return mLuminanceAlphaTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002284}
2285
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002286bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002287{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002288 return mSupportsTextureFilterAnisotropy;
2289}
2290
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002291float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002292{
2293 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002294 {
shannon.woods%transgaming.com@gtempaccount.com6b731912013-04-13 03:35:19 +00002295 return static_cast<float>(mDeviceCaps.MaxAnisotropy);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002296 }
2297 return 1.0f;
2298}
2299
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002300bool Renderer9::getEventQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002301{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002302 return mEventQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002303}
2304
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002305unsigned int Renderer9::getMaxVertexTextureImageUnits() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002306{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002307 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2308 return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002309}
2310
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002311unsigned int Renderer9::getMaxCombinedTextureImageUnits() const
2312{
2313 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2314}
2315
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002316unsigned int Renderer9::getReservedVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002317{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002318 return 2; // dx_ViewAdjust and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002319}
2320
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002321unsigned int Renderer9::getReservedFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002322{
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00002323 return 3; // dx_ViewCoords, dx_DepthFront and dx_DepthRange.
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002324}
2325
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002326unsigned int Renderer9::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002327{
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002328 return MAX_VERTEX_CONSTANT_VECTORS_D3D9 - getReservedVertexUniformVectors();
2329}
2330
2331unsigned int Renderer9::getMaxFragmentUniformVectors() const
2332{
2333 const int maxPixelConstantVectors = (getMajorShaderModel() >= 3) ? MAX_PIXEL_CONSTANT_VECTORS_SM3 : MAX_PIXEL_CONSTANT_VECTORS_SM2;
2334
2335 return maxPixelConstantVectors - getReservedFragmentUniformVectors();
2336}
2337
2338unsigned int Renderer9::getMaxVaryingVectors() const
2339{
2340 return (getMajorShaderModel() >= 3) ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002341}
2342
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002343unsigned int Renderer9::getMaxVertexShaderUniformBuffers() const
2344{
2345 return 0;
2346}
2347
2348unsigned int Renderer9::getMaxFragmentShaderUniformBuffers() const
2349{
2350 return 0;
2351}
2352
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002353unsigned int Renderer9::getReservedVertexUniformBuffers() const
2354{
2355 return 0;
2356}
2357
2358unsigned int Renderer9::getReservedFragmentUniformBuffers() const
2359{
2360 return 0;
2361}
2362
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002363unsigned int Renderer9::getMaxTransformFeedbackBuffers() const
2364{
2365 return 0;
2366}
2367
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002368unsigned int Renderer9::getMaxUniformBufferSize() const
2369{
2370 return 0;
2371}
2372
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002373bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002374{
2375 return mSupportsNonPower2Textures;
2376}
2377
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002378bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002379{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002380 return mOcclusionQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002381}
2382
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002383bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002384{
2385 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2386}
2387
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002388bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002389{
2390 // PIX doesn't seem to support using share handles, so disable them.
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002391 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002392}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002393
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002394bool Renderer9::getDerivativeInstructionSupport() const
2395{
2396 return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0;
2397}
2398
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002399bool Renderer9::getPostSubBufferSupport() const
2400{
2401 return true;
2402}
2403
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002404int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002405{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002406 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002407}
2408
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002409float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002410{
shannon.woods@transgaming.combd8c10c2013-01-25 21:15:03 +00002411 // Point size clamped at 1.0f for SM2
2412 return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f;
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002413}
2414
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002415int Renderer9::getMaxViewportDimension() const
2416{
2417 int maxTextureDimension = std::min(std::min(getMaxTextureWidth(), getMaxTextureHeight()),
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002418 (int)gl::IMPLEMENTATION_MAX_2D_TEXTURE_SIZE);
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002419 return maxTextureDimension;
2420}
2421
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002422int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002423{
2424 return (int)mDeviceCaps.MaxTextureWidth;
2425}
2426
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002427int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002428{
2429 return (int)mDeviceCaps.MaxTextureHeight;
2430}
2431
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002432int Renderer9::getMaxTextureDepth() const
2433{
2434 // 3D textures are not available in the D3D9 backend.
2435 return 1;
2436}
2437
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002438int Renderer9::getMaxTextureArrayLayers() const
2439{
2440 // 2D array textures are not available in the D3D9 backend.
2441 return 1;
2442}
2443
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002444bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002445{
2446 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2447}
2448
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002449DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002450{
2451 return mDeviceCaps.DeclTypes;
2452}
2453
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002454int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002455{
2456 return mMinSwapInterval;
2457}
2458
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002459int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002460{
2461 return mMaxSwapInterval;
2462}
2463
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002464int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002465{
2466 return mMaxSupportedSamples;
2467}
2468
Geoff Lang0e120e32013-05-29 10:23:55 -04002469GLsizei Renderer9::getMaxSupportedFormatSamples(GLint internalFormat) const
2470{
Shannon Woodsddb785c2013-07-08 10:32:13 -04002471 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
Geoff Lang0e120e32013-05-29 10:23:55 -04002472 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
2473 return (itr != mMultiSampleSupport.end()) ? mMaxSupportedSamples : 0;
2474}
2475
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002476GLsizei Renderer9::getNumSampleCounts(GLint internalFormat) const
2477{
2478 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
2479 MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format);
2480
2481 unsigned int numCounts = 0;
2482 if (iter != mMultiSampleSupport.end())
2483 {
2484 const MultisampleSupportInfo& info = iter->second;
2485 for (int i = 0; i < D3DMULTISAMPLE_16_SAMPLES; i++)
2486 {
2487 if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i])
2488 {
2489 numCounts++;
2490 }
2491 }
2492 }
2493
2494 return numCounts;
2495}
2496
2497void Renderer9::getSampleCounts(GLint internalFormat, GLsizei bufSize, GLint *params) const
2498{
2499 D3DFORMAT format = gl_d3d9::GetTextureFormat(internalFormat, this);
2500 MultisampleSupportMap::const_iterator iter = mMultiSampleSupport.find(format);
2501
2502 if (iter != mMultiSampleSupport.end())
2503 {
2504 const MultisampleSupportInfo& info = iter->second;
2505 int bufPos = 0;
2506 for (int i = D3DMULTISAMPLE_16_SAMPLES; i >= 0 && bufPos < bufSize; i--)
2507 {
2508 if (i != D3DMULTISAMPLE_NONMASKABLE && info.supportedSamples[i])
2509 {
2510 params[bufPos++] = i;
2511 }
2512 }
2513 }
2514}
2515
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002516int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002517{
2518 if (requested == 0)
2519 {
2520 return requested;
2521 }
2522
Geoff Lang61e49a52013-05-29 10:22:58 -04002523 MultisampleSupportMap::const_iterator itr = mMultiSampleSupport.find(format);
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002524 if (itr == mMultiSampleSupport.end())
2525 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002526 if (format == D3DFMT_UNKNOWN)
2527 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002528 return -1;
2529 }
2530
Geoff Lang61e49a52013-05-29 10:22:58 -04002531 for (unsigned int i = requested; i < ArraySize(itr->second.supportedSamples); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002532 {
Geoff Lang61e49a52013-05-29 10:22:58 -04002533 if (itr->second.supportedSamples[i] && i != D3DMULTISAMPLE_NONMASKABLE)
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002534 {
2535 return i;
2536 }
2537 }
2538
2539 return -1;
2540}
2541
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002542unsigned int Renderer9::getMaxRenderTargets() const
2543{
2544 // we do not support MRT in d3d9
2545 return 1;
2546}
2547
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002548D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2549{
2550 switch (internalformat)
2551 {
2552 case GL_DEPTH_COMPONENT16:
2553 case GL_DEPTH_COMPONENT32_OES:
2554 case GL_DEPTH24_STENCIL8_OES:
2555 return D3DFMT_INTZ;
2556 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2557 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2558 return D3DFMT_DXT1;
2559 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2560 return D3DFMT_DXT3;
2561 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2562 return D3DFMT_DXT5;
2563 case GL_RGBA32F_EXT:
2564 case GL_RGB32F_EXT:
2565 case GL_ALPHA32F_EXT:
2566 case GL_LUMINANCE32F_EXT:
2567 case GL_LUMINANCE_ALPHA32F_EXT:
2568 return D3DFMT_A32B32G32R32F;
2569 case GL_RGBA16F_EXT:
2570 case GL_RGB16F_EXT:
2571 case GL_ALPHA16F_EXT:
2572 case GL_LUMINANCE16F_EXT:
2573 case GL_LUMINANCE_ALPHA16F_EXT:
2574 return D3DFMT_A16B16G16R16F;
2575 case GL_LUMINANCE8_EXT:
2576 if (getLuminanceTextureSupport())
2577 {
2578 return D3DFMT_L8;
2579 }
2580 break;
2581 case GL_LUMINANCE8_ALPHA8_EXT:
2582 if (getLuminanceAlphaTextureSupport())
2583 {
2584 return D3DFMT_A8L8;
2585 }
2586 break;
2587 case GL_RGB8_OES:
2588 case GL_RGB565:
2589 return D3DFMT_X8R8G8B8;
2590 }
2591
2592 return D3DFMT_A8R8G8B8;
2593}
2594
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002595bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002596{
2597 bool result = false;
2598
2599 if (source && dest)
2600 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002601 TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance());
2602 TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002603
2604 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002605 for (int i = 0; i < levels; ++i)
2606 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002607 IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false);
2608 IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002609
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002610 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002611
2612 if (srcSurf) srcSurf->Release();
2613 if (dstSurf) dstSurf->Release();
2614
2615 if (!result)
2616 return false;
2617 }
2618 }
2619
2620 return result;
2621}
2622
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002623bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002624{
2625 bool result = false;
2626
2627 if (source && dest)
2628 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002629 TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance());
2630 TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002631 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002632 for (int f = 0; f < 6; f++)
2633 {
2634 for (int i = 0; i < levels; i++)
2635 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002636 IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2637 IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002638
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002639 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002640
2641 if (srcSurf) srcSurf->Release();
2642 if (dstSurf) dstSurf->Release();
2643
2644 if (!result)
2645 return false;
2646 }
2647 }
2648 }
2649
2650 return result;
2651}
2652
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002653bool Renderer9::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2654{
2655 // 3D textures are not available in the D3D9 backend.
2656 UNREACHABLE();
2657 return false;
2658}
2659
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002660bool Renderer9::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2661{
2662 // 2D array textures are not supported by the D3D9 backend.
2663 UNREACHABLE();
2664 return false;
2665}
2666
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002667D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002668{
2669 if (mD3d9Ex != NULL)
2670 {
2671 return D3DPOOL_DEFAULT;
2672 }
2673 else
2674 {
2675 if (!(usage & D3DUSAGE_DYNAMIC))
2676 {
2677 return D3DPOOL_MANAGED;
2678 }
2679 }
2680
2681 return D3DPOOL_DEFAULT;
2682}
2683
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002684bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002685 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002686{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002687 RECT rect;
2688 rect.left = sourceRect.x;
2689 rect.top = sourceRect.y;
2690 rect.right = sourceRect.x + sourceRect.width;
2691 rect.bottom = sourceRect.y + sourceRect.height;
2692
2693 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002694}
2695
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002696bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002697 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002698{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002699 RECT rect;
2700 rect.left = sourceRect.x;
2701 rect.top = sourceRect.y;
2702 rect.right = sourceRect.x + sourceRect.width;
2703 rect.bottom = sourceRect.y + sourceRect.height;
2704
2705 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002706}
2707
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002708bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2709 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2710{
2711 // 3D textures are not available in the D3D9 backend.
2712 UNREACHABLE();
2713 return false;
2714}
2715
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002716bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2717 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2718{
2719 // 2D array textures are not available in the D3D9 backend.
2720 UNREACHABLE();
2721 return false;
2722}
2723
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002724bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, const gl::Rectangle &readRect, gl::Framebuffer *drawFramebuffer, const gl::Rectangle &drawRect,
Geoff Lang685806d2013-06-12 11:16:36 -04002725 bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002726{
Geoff Lang758d5b22013-06-11 11:42:50 -04002727 ASSERT(filter == GL_NEAREST);
2728
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002729 endScene();
2730
2731 if (blitRenderTarget)
2732 {
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002733 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer(0);
2734 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002735 RenderTarget9 *readRenderTarget = NULL;
2736 RenderTarget9 *drawRenderTarget = NULL;
2737 IDirect3DSurface9* readSurface = NULL;
2738 IDirect3DSurface9* drawSurface = NULL;
2739
2740 if (readBuffer)
2741 {
2742 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2743 }
2744 if (drawBuffer)
2745 {
2746 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2747 }
2748
2749 if (readRenderTarget)
2750 {
2751 readSurface = readRenderTarget->getSurface();
2752 }
2753 if (drawRenderTarget)
2754 {
2755 drawSurface = drawRenderTarget->getSurface();
2756 }
2757
2758 if (!readSurface || !drawSurface)
2759 {
2760 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002761 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002762 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002763
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002764 RECT srcRect;
2765 srcRect.left = readRect.x;
2766 srcRect.right = readRect.x + readRect.width;
2767 srcRect.top = readRect.y;
2768 srcRect.bottom = readRect.y + readRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002769
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002770 RECT dstRect;
2771 dstRect.left = drawRect.x;
2772 dstRect.right = drawRect.x + drawRect.width;
2773 dstRect.top = drawRect.y;
2774 dstRect.bottom = drawRect.y + drawRect.height;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002775
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002776 HRESULT result = mDevice->StretchRect(readSurface, &srcRect, drawSurface, &dstRect, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002777
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002778 readSurface->Release();
2779 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002780
2781 if (FAILED(result))
2782 {
2783 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2784 return false;
2785 }
2786 }
2787
Geoff Lang685806d2013-06-12 11:16:36 -04002788 if (blitDepth || blitStencil)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002789 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002790 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2791 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2792 RenderTarget9 *readDepthStencil = NULL;
2793 RenderTarget9 *drawDepthStencil = NULL;
2794 IDirect3DSurface9* readSurface = NULL;
2795 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002796
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002797 if (readBuffer)
2798 {
2799 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2800 }
2801 if (drawBuffer)
2802 {
2803 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2804 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002805
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002806 if (readDepthStencil)
2807 {
2808 readSurface = readDepthStencil->getSurface();
2809 }
2810 if (drawDepthStencil)
2811 {
2812 drawSurface = drawDepthStencil->getSurface();
2813 }
2814
2815 if (!readSurface || !drawSurface)
2816 {
2817 ERR("Failed to retrieve the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002818 return gl::error(GL_OUT_OF_MEMORY, false);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002819 }
2820
2821 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2822
2823 readSurface->Release();
2824 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002825
2826 if (FAILED(result))
2827 {
2828 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2829 return false;
2830 }
2831 }
2832
2833 return true;
2834}
2835
2836void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2837 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2838{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002839 RenderTarget9 *renderTarget = NULL;
2840 IDirect3DSurface9 *surface = NULL;
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002841 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(0);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002842
2843 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002844 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002845 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2846 }
2847
2848 if (renderTarget)
2849 {
2850 surface = renderTarget->getSurface();
2851 }
2852
2853 if (!surface)
2854 {
2855 // context must be lost
2856 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002857 }
2858
2859 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002860 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002861
2862 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2863 {
2864 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002865 surface->Release();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002866 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002867 }
2868
2869 HRESULT result;
2870 IDirect3DSurface9 *systemSurface = NULL;
2871 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2872 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2873 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2874 if (directToPixels)
2875 {
2876 // Use the pixels ptr as a shared handle to write directly into client's memory
2877 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2878 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2879 if (FAILED(result))
2880 {
2881 // Try again without the shared handle
2882 directToPixels = false;
2883 }
2884 }
2885
2886 if (!directToPixels)
2887 {
2888 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2889 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2890 if (FAILED(result))
2891 {
2892 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002893 surface->Release();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002894 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002895 }
2896 }
2897
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002898 result = mDevice->GetRenderTargetData(surface, systemSurface);
2899 surface->Release();
2900 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002901
2902 if (FAILED(result))
2903 {
2904 systemSurface->Release();
2905
2906 // It turns out that D3D will sometimes produce more error
2907 // codes than those documented.
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002908 if (d3d9::isDeviceLostError(result))
2909 {
2910 notifyDeviceLost();
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002911 return gl::error(GL_OUT_OF_MEMORY);
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00002912 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002913 else
2914 {
2915 UNREACHABLE();
2916 return;
2917 }
2918
2919 }
2920
2921 if (directToPixels)
2922 {
2923 systemSurface->Release();
2924 return;
2925 }
2926
2927 RECT rect;
2928 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2929 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2930 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2931 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2932
2933 D3DLOCKED_RECT lock;
2934 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2935
2936 if (FAILED(result))
2937 {
2938 UNREACHABLE();
2939 systemSurface->Release();
2940
2941 return; // No sensible error to generate
2942 }
2943
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002944 unsigned char *source;
2945 int inputPitch;
2946 if (packReverseRowOrder)
2947 {
2948 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2949 inputPitch = -lock.Pitch;
2950 }
2951 else
2952 {
2953 source = (unsigned char*)lock.pBits;
2954 inputPitch = lock.Pitch;
2955 }
2956
Geoff Lang697ad3e2013-06-04 10:11:28 -04002957 GLuint clientVersion = getCurrentClientVersion();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002958
Geoff Lang697ad3e2013-06-04 10:11:28 -04002959 GLint sourceInternalFormat = d3d9_gl::GetInternalFormat(desc.Format);
2960 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
2961 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002962
Geoff Lang697ad3e2013-06-04 10:11:28 -04002963 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
2964
2965 if (sourceFormat == format && sourceType == type)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002966 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002967 // Direct copy possible
2968 unsigned char *dest = static_cast<unsigned char*>(pixels);
2969 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002970 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002971 memcpy(dest + y * outputPitch, source + y * inputPitch, (rect.right - rect.left) * sourcePixelSize);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002972 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04002973 }
2974 else
2975 {
2976 GLint destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
2977 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
2978 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
2979
2980 ColorCopyFunction fastCopyFunc = d3d9::GetFastCopyFunction(desc.Format, format, type, getCurrentClientVersion());
2981 if (fastCopyFunc)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002982 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002983 // Fast copy is possible through some special function
2984 for (int y = 0; y < rect.bottom - rect.top; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002985 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002986 for (int x = 0; x < rect.right - rect.left; x++)
2987 {
2988 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
2989 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
2990
2991 fastCopyFunc(src, dest);
2992 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002993 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00002994 }
Geoff Lang697ad3e2013-06-04 10:11:28 -04002995 else
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002996 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04002997 ColorReadFunction readFunc = d3d9::GetColorReadFunction(desc.Format);
2998 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002999
Geoff Lang697ad3e2013-06-04 10:11:28 -04003000 gl::ColorF temp;
3001
3002 for (int y = 0; y < rect.bottom - rect.top; y++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003003 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003004 for (int x = 0; x < rect.right - rect.left; x++)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003005 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003006 void *dest = reinterpret_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3007 void *src = source + y * inputPitch + x * sourcePixelSize;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003008
Geoff Lang697ad3e2013-06-04 10:11:28 -04003009 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3010 // will not allow the copy otherwise.
3011 readFunc(src, &temp);
3012 writeFunc(&temp, dest);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003013 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003014 }
3015 }
3016 }
3017
3018 systemSurface->UnlockRect();
3019
3020 systemSurface->Release();
3021}
3022
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003023RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
3024{
3025 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3026 IDirect3DSurface9 *surface = NULL;
3027 if (depth)
3028 {
3029 surface = swapChain9->getDepthStencil();
3030 }
3031 else
3032 {
3033 surface = swapChain9->getRenderTarget();
3034 }
3035
3036 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
3037
3038 return renderTarget;
3039}
3040
Geoff Langa2d97f12013-06-11 11:44:02 -04003041RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003042{
3043 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
3044 return renderTarget;
3045}
3046
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003047ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00003048{
3049 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com55318902012-11-28 20:58:58 +00003050
3051 switch (type)
3052 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003053 case rx::SHADER_VERTEX:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003054 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003055 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003056 if (vshader)
3057 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003058 executable = new ShaderExecutable9(function, length, vshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003059 }
3060 }
3061 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003062 case rx::SHADER_PIXEL:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003063 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003064 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003065 if (pshader)
3066 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003067 executable = new ShaderExecutable9(function, length, pshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003068 }
3069 }
3070 break;
3071 default:
3072 UNREACHABLE();
3073 break;
3074 }
3075
3076 return executable;
3077}
3078
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003079ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003080{
3081 const char *profile = NULL;
3082
3083 switch (type)
3084 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003085 case rx::SHADER_VERTEX:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003086 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
3087 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003088 case rx::SHADER_PIXEL:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003089 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
3090 break;
3091 default:
3092 UNREACHABLE();
3093 return NULL;
3094 }
3095
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00003096 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, ANGLE_COMPILE_OPTIMIZATION_LEVEL, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003097 if (!binary)
3098 return NULL;
3099
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003100 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com536dd6e2012-11-28 21:00:18 +00003101 binary->Release();
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003102
3103 return executable;
3104}
3105
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00003106bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
3107{
3108 return mBlit->boxFilter(source, dest);
3109}
3110
daniel@transgaming.com2507f412012-10-31 18:46:48 +00003111D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003112{
3113 if (mD3d9Ex != NULL)
3114 {
3115 return D3DPOOL_DEFAULT;
3116 }
3117 else
3118 {
3119 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
3120 {
3121 return D3DPOOL_MANAGED;
3122 }
3123 }
3124
3125 return D3DPOOL_DEFAULT;
3126}
3127
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003128bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
3129{
3130 if (source && dest)
3131 {
3132 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003133
3134 if (fromManaged)
3135 {
3136 D3DSURFACE_DESC desc;
3137 source->GetDesc(&desc);
3138
3139 IDirect3DSurface9 *surf = 0;
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003140 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003141
3142 if (SUCCEEDED(result))
3143 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003144 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003145 result = mDevice->UpdateSurface(surf, NULL, dest, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003146 surf->Release();
3147 }
3148 }
3149 else
3150 {
3151 endScene();
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003152 result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003153 }
3154
3155 if (FAILED(result))
3156 {
3157 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
3158 return false;
3159 }
3160 }
3161
3162 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00003163}
3164
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003165Image *Renderer9::createImage()
3166{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003167 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003168}
3169
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003170void Renderer9::generateMipmap(Image *dest, Image *src)
3171{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003172 Image9 *src9 = Image9::makeImage9(src);
3173 Image9 *dst9 = Image9::makeImage9(dest);
3174 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003175}
3176
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003177TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain)
3178{
3179 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3180 return new TextureStorage9_2D(this, swapChain9);
3181}
3182
3183TextureStorage *Renderer9::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3184{
3185 return new TextureStorage9_2D(this, levels, internalformat, usage, forceRenderable, width, height);
3186}
3187
3188TextureStorage *Renderer9::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3189{
3190 return new TextureStorage9_Cube(this, levels, internalformat, usage, forceRenderable, size);
3191}
3192
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003193TextureStorage *Renderer9::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3194{
3195 // 3D textures are not supported by the D3D9 backend.
3196 UNREACHABLE();
3197
3198 return NULL;
3199}
3200
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003201TextureStorage *Renderer9::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3202{
3203 // 2D array textures are not supported by the D3D9 backend.
3204 UNREACHABLE();
3205
3206 return NULL;
3207}
3208
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003209bool Renderer9::getLUID(LUID *adapterLuid) const
3210{
3211 adapterLuid->HighPart = 0;
3212 adapterLuid->LowPart = 0;
3213
3214 if (mD3d9Ex)
3215 {
3216 mD3d9Ex->GetAdapterLUID(mAdapter, adapterLuid);
3217 return true;
3218 }
3219
3220 return false;
3221}
3222
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003223}