blob: de7c63a43d813b50b03de64de0e5a19b732cce4a [file] [log] [blame]
daniel@transgaming.com621ce052012-10-31 17:52:29 +00001//
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00007// Renderer9.cpp: Implements a back-end specific class for the D3D9 renderer.
daniel@transgaming.com621ce052012-10-31 17:52:29 +00008
daniel@transgaming.com621ce052012-10-31 17:52:29 +00009#include "common/debug.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000010#include "libGLESv2/main.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000011#include "libGLESv2/utilities.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000012#include "libGLESv2/mathutil.h"
daniel@transgaming.com91207b72012-11-28 20:56:43 +000013#include "libGLESv2/Buffer.h"
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000014#include "libGLESv2/Framebuffer.h"
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +000015#include "libGLESv2/Program.h"
16#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com50aadb02012-11-28 21:06:11 +000017#include "libGLESv2/renderer/IndexDataManager.h"
18#include "libGLESv2/renderer/VertexDataManager.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"
daniel@transgaming.coma9c71422012-11-28 20:58:45 +000021#include "libGLESv2/renderer/ShaderExecutable9.h"
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +000022#include "libGLESv2/renderer/SwapChain9.h"
daniel@transgaming.com34da3972012-12-20 21:10:29 +000023#include "libGLESv2/renderer/TextureStorage9.h"
daniel@transgaming.com4ba24062012-12-20 20:54:24 +000024#include "libGLESv2/renderer/Image9.h"
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000025#include "libGLESv2/renderer/Blit.h"
daniel@transgaming.comd186dc72012-11-28 19:40:16 +000026#include "libGLESv2/renderer/RenderTarget9.h"
daniel@transgaming.com3f255b42012-12-20 21:07:35 +000027#include "libGLESv2/renderer/VertexBuffer9.h"
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +000028#include "libGLESv2/renderer/IndexBuffer9.h"
shannon.woods@transgaming.com50df6c52013-02-28 23:02:49 +000029#include "libGLESv2/renderer/Query9.h"
daniel@transgaming.com621ce052012-10-31 17:52:29 +000030
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +000031#include <sstream>
32
daniel@transgaming.com621ce052012-10-31 17:52:29 +000033// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
34#define REF_RAST 0
35
36// The "Debug This Pixel..." feature in PIX often fails when using the
37// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
38// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
39#if !defined(ANGLE_ENABLE_D3D9EX)
40// Enables use of the IDirect3D9Ex interface, when available
41#define ANGLE_ENABLE_D3D9EX 1
42#endif // !defined(ANGLE_ENABLE_D3D9EX)
43
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000044namespace rx
daniel@transgaming.com621ce052012-10-31 17:52:29 +000045{
daniel@transgaming.com222ee082012-11-28 19:31:49 +000046static const D3DFORMAT RenderTargetFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000047 {
48 D3DFMT_A1R5G5B5,
49 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
50 D3DFMT_A8R8G8B8,
51 D3DFMT_R5G6B5,
52 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
53 D3DFMT_X8R8G8B8
54 };
55
daniel@transgaming.com222ee082012-11-28 19:31:49 +000056static const D3DFORMAT DepthStencilFormats[] =
daniel@transgaming.com92955622012-10-31 18:38:41 +000057 {
58 D3DFMT_UNKNOWN,
59 // D3DFMT_D16_LOCKABLE,
60 D3DFMT_D32,
61 // D3DFMT_D15S1,
62 D3DFMT_D24S8,
63 D3DFMT_D24X8,
64 // D3DFMT_D24X4S4,
65 D3DFMT_D16,
66 // D3DFMT_D32F_LOCKABLE,
67 // D3DFMT_D24FS8
68 };
daniel@transgaming.com621ce052012-10-31 17:52:29 +000069
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000070enum
71{
72 MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 = 4
73};
74
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000075Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice)
daniel@transgaming.com621ce052012-10-31 17:52:29 +000076{
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000077 mD3d9Module = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000078
79 mD3d9 = NULL;
80 mD3d9Ex = NULL;
81 mDevice = NULL;
82 mDeviceEx = NULL;
83 mDeviceWindow = NULL;
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +000084 mBlit = NULL;
daniel@transgaming.com621ce052012-10-31 17:52:29 +000085
86 mAdapter = D3DADAPTER_DEFAULT;
87
88 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
89 mDeviceType = D3DDEVTYPE_REF;
90 #else
91 mDeviceType = D3DDEVTYPE_HAL;
92 #endif
93
94 mDeviceLost = false;
daniel@transgaming.comb7833982012-10-31 18:31:46 +000095
96 mMaxSupportedSamples = 0;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000097
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +000098 mMaskedClearSavedState = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +000099
100 mVertexDataManager = NULL;
101 mIndexDataManager = NULL;
102 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +0000103
104 mMaxNullColorbufferLRU = 0;
105 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
106 {
107 mNullColorbufferCache[i].lruCount = 0;
108 mNullColorbufferCache[i].width = 0;
109 mNullColorbufferCache[i].height = 0;
110 mNullColorbufferCache[i].buffer = NULL;
111 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000112}
113
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000114Renderer9::~Renderer9()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000115{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000116 releaseDeviceResources();
117
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000118 if (mDevice)
119 {
120 // 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 +0000121 if (testDeviceLost(false))
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000122 {
123 resetDevice();
124 }
125
126 mDevice->Release();
127 mDevice = NULL;
128 }
129
130 if (mDeviceEx)
131 {
132 mDeviceEx->Release();
133 mDeviceEx = NULL;
134 }
135
136 if (mD3d9)
137 {
138 mD3d9->Release();
139 mD3d9 = NULL;
140 }
141
142 if (mDeviceWindow)
143 {
144 DestroyWindow(mDeviceWindow);
145 mDeviceWindow = NULL;
146 }
147
148 if (mD3d9Ex)
149 {
150 mD3d9Ex->Release();
151 mD3d9Ex = NULL;
152 }
153
154 if (mD3d9Module)
155 {
156 mD3d9Module = NULL;
157 }
158
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000159 while (!mMultiSampleSupport.empty())
160 {
161 delete [] mMultiSampleSupport.begin()->second;
162 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
163 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000164}
165
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000166Renderer9 *Renderer9::makeRenderer9(Renderer *renderer)
167{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000168 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer9*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000169 return static_cast<rx::Renderer9*>(renderer);
170}
171
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000172EGLint Renderer9::initialize()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000173{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000174 if (!initializeCompiler())
175 {
176 return EGL_NOT_INITIALIZED;
177 }
178
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000179 if (mSoftwareDevice)
180 {
181 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
182 }
183 else
184 {
185 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
186 }
187
188 if (mD3d9Module == NULL)
189 {
190 ERR("No D3D9 module found - aborting!\n");
191 return EGL_NOT_INITIALIZED;
192 }
193
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000194 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
195 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
196
197 // Use Direct3D9Ex if available. Among other things, this version is less
198 // inclined to report a lost context, for example when the user switches
199 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
200 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
201 {
202 ASSERT(mD3d9Ex);
203 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
204 ASSERT(mD3d9);
205 }
206 else
207 {
208 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
209 }
210
211 if (!mD3d9)
212 {
213 ERR("Could not create D3D9 device - aborting!\n");
214 return EGL_NOT_INITIALIZED;
215 }
daniel@transgaming.com7d738a22012-11-28 19:43:08 +0000216
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000217 if (mDc != NULL)
218 {
219 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
220 }
221
222 HRESULT result;
223
224 // Give up on getting device caps after about one second.
225 for (int i = 0; i < 10; ++i)
226 {
227 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
228 if (SUCCEEDED(result))
229 {
230 break;
231 }
232 else if (result == D3DERR_NOTAVAILABLE)
233 {
234 Sleep(100); // Give the driver some time to initialize/recover
235 }
236 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
237 {
238 ERR("failed to get device caps (0x%x)\n", result);
239 return EGL_NOT_INITIALIZED;
240 }
241 }
242
243 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
244 {
245 ERR("Renderer does not support PS 2.0. aborting!\n");
246 return EGL_NOT_INITIALIZED;
247 }
248
249 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
250 // This is required by Texture2D::convertToRenderTarget.
251 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
252 {
253 ERR("Renderer does not support stretctrect from textures!\n");
254 return EGL_NOT_INITIALIZED;
255 }
256
257 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
258
259 // ATI cards on XP have problems with non-power-of-two textures.
260 mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
261 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
262 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) &&
263 !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD);
264
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000265 // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec
266 mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2));
267
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +0000268 mMinSwapInterval = 4;
269 mMaxSwapInterval = 0;
270
271 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
272 {
273 mMinSwapInterval = std::min(mMinSwapInterval, 0);
274 mMaxSwapInterval = std::max(mMaxSwapInterval, 0);
275 }
276 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
277 {
278 mMinSwapInterval = std::min(mMinSwapInterval, 1);
279 mMaxSwapInterval = std::max(mMaxSwapInterval, 1);
280 }
281 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
282 {
283 mMinSwapInterval = std::min(mMinSwapInterval, 2);
284 mMaxSwapInterval = std::max(mMaxSwapInterval, 2);
285 }
286 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
287 {
288 mMinSwapInterval = std::min(mMinSwapInterval, 3);
289 mMaxSwapInterval = std::max(mMaxSwapInterval, 3);
290 }
291 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
292 {
293 mMinSwapInterval = std::min(mMinSwapInterval, 4);
294 mMaxSwapInterval = std::max(mMaxSwapInterval, 4);
295 }
296
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000297 int max = 0;
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000298 for (int i = 0; i < sizeof(RenderTargetFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000299 {
300 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000301 getMultiSampleSupport(RenderTargetFormats[i], multisampleArray);
302 mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray;
daniel@transgaming.com92955622012-10-31 18:38:41 +0000303
304 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
305 {
306 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
307 {
308 max = j;
309 }
310 }
311 }
312
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000313 for (int i = 0; i < sizeof(DepthStencilFormats) / sizeof(D3DFORMAT); ++i)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000314 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000315 if (DepthStencilFormats[i] == D3DFMT_UNKNOWN)
daniel@transgaming.com92955622012-10-31 18:38:41 +0000316 continue;
317
318 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000319 getMultiSampleSupport(DepthStencilFormats[i], multisampleArray);
320 mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray;
daniel@transgaming.comb7833982012-10-31 18:31:46 +0000321
322 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
323 {
324 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
325 {
326 max = j;
327 }
328 }
329 }
330
331 mMaxSupportedSamples = max;
332
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000333 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
334 static const TCHAR className[] = TEXT("STATIC");
335
336 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
337
338 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
339 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
340
341 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
342 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
343 {
344 return EGL_BAD_ALLOC;
345 }
346
347 if (FAILED(result))
348 {
349 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
350
351 if (FAILED(result))
352 {
353 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
354 return EGL_BAD_ALLOC;
355 }
356 }
357
358 if (mD3d9Ex)
359 {
360 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
361 ASSERT(SUCCEEDED(result));
362 }
363
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000364 mVertexShaderCache.initialize(mDevice);
365 mPixelShaderCache.initialize(mDevice);
366
daniel@transgaming.com669c9952013-01-11 04:08:16 +0000367 // Check occlusion query support
368 IDirect3DQuery9 *occlusionQuery = NULL;
369 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery)) && occlusionQuery)
370 {
371 occlusionQuery->Release();
372 mOcclusionQuerySupport = true;
373 }
374 else
375 {
376 mOcclusionQuerySupport = false;
377 }
378
379 // Check event query support
380 IDirect3DQuery9 *eventQuery = NULL;
381 if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery)) && eventQuery)
382 {
383 eventQuery->Release();
384 mEventQuerySupport = true;
385 }
386 else
387 {
388 mEventQuerySupport = false;
389 }
390
391 D3DDISPLAYMODE currentDisplayMode;
392 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
393
394 // Check vertex texture support
395 // Only Direct3D 10 ready devices support all the necessary vertex texture formats.
396 // We test this using D3D9 by checking support for the R16F format.
397 mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) &&
398 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
399 D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F));
400
401 // Check depth texture support
402 // we use INTZ for depth textures in Direct3D9
403 // we also want NULL texture support to ensure the we can make depth-only FBOs
404 // see http://aras-p.info/texts/D3D9GPUHacks.html
405 mDepthTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
406 D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)) &&
407 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format,
408 D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL));
409
410 // Check 32 bit floating point texture support
411 mFloat32FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
412 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
413 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
414 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
415
416 mFloat32RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
417 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
418 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
419 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
420
421 if (!mFloat32FilterSupport && !mFloat32RenderSupport)
422 {
423 mFloat32TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
424 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
425 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
426 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
427 }
428 else
429 {
430 mFloat32TextureSupport = true;
431 }
432
433 // Check 16 bit floating point texture support
434 mFloat16FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
435 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
436 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
437 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
438
439 mFloat16RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
440 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
441 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
442 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
443
444 if (!mFloat16FilterSupport && !mFloat16RenderSupport)
445 {
446 mFloat16TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
447 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
448 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
449 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
450 }
451 else
452 {
453 mFloat16TextureSupport = true;
454 }
455
456 // Check DXT texture support
457 mDXT1TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
458 mDXT3TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
459 mDXT5TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
460
461 // Check luminance[alpha] texture support
462 mLuminanceTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
463 mLuminanceAlphaTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
464
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000465 initializeDevice();
466
467 return EGL_SUCCESS;
468}
469
470// do any one-time device initialization
471// NOTE: this is also needed after a device lost/reset
472// to reset the scene status and ensure the default states are reset.
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000473void Renderer9::initializeDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000474{
475 // Permanent non-default states
476 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
477 mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
478
479 if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
480 {
481 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize);
482 }
483 else
484 {
485 mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
486 }
487
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000488 markAllStateDirty();
489
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000490 mSceneStarted = false;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000491
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000492 ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager);
daniel@transgaming.come569fc52012-11-28 20:56:02 +0000493 mBlit = new Blit(this);
daniel@transgaming.com31240482012-11-28 21:06:41 +0000494 mVertexDataManager = new rx::VertexDataManager(this);
495 mIndexDataManager = new rx::IndexDataManager(this);
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000496}
497
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000498D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000499{
500 D3DPRESENT_PARAMETERS presentParameters = {0};
501
502 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
503 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
504 presentParameters.BackBufferCount = 1;
505 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
506 presentParameters.BackBufferWidth = 1;
507 presentParameters.BackBufferHeight = 1;
508 presentParameters.EnableAutoDepthStencil = FALSE;
509 presentParameters.Flags = 0;
510 presentParameters.hDeviceWindow = mDeviceWindow;
511 presentParameters.MultiSampleQuality = 0;
512 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
513 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
514 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
515 presentParameters.Windowed = TRUE;
516
517 return presentParameters;
518}
519
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000520int Renderer9::generateConfigs(ConfigDesc **configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000521{
522 D3DDISPLAYMODE currentDisplayMode;
523 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
524
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000525 unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]);
526 unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000527 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
528 int numConfigs = 0;
529
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000530 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000531 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000532 D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000533
534 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
535
536 if (SUCCEEDED(result))
537 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000538 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000539 {
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000540 D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000541 HRESULT result = D3D_OK;
542
543 if(depthStencilFormat != D3DFMT_UNKNOWN)
544 {
545 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
546 }
547
548 if (SUCCEEDED(result))
549 {
550 if(depthStencilFormat != D3DFMT_UNKNOWN)
551 {
552 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
553 }
554
555 if (SUCCEEDED(result))
556 {
557 ConfigDesc newConfig;
daniel@transgaming.com682a37c2012-11-28 19:34:44 +0000558 newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat);
559 newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat);
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000560 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
561 newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat);
562
563 (*configDescList)[numConfigs++] = newConfig;
564 }
565 }
566 }
567 }
568 }
569
570 return numConfigs;
571}
572
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000573void Renderer9::deleteConfigs(ConfigDesc *configDescList)
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000574{
575 delete [] (configDescList);
576}
577
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000578void Renderer9::startScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000579{
580 if (!mSceneStarted)
581 {
582 long result = mDevice->BeginScene();
583 if (SUCCEEDED(result)) {
584 // This is defensive checking against the device being
585 // lost at unexpected times.
586 mSceneStarted = true;
587 }
588 }
589}
590
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000591void Renderer9::endScene()
daniel@transgaming.com621ce052012-10-31 17:52:29 +0000592{
593 if (mSceneStarted)
594 {
595 // EndScene can fail if the device was lost, for example due
596 // to a TDR during a draw call.
597 mDevice->EndScene();
598 mSceneStarted = false;
599 }
600}
601
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000602void Renderer9::sync(bool block)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000603{
604 HRESULT result;
605
606 IDirect3DQuery9* query = allocateEventQuery();
607 if (!query)
608 {
609 return;
610 }
611
612 result = query->Issue(D3DISSUE_END);
613 ASSERT(SUCCEEDED(result));
614
615 do
616 {
617 result = query->GetData(NULL, 0, D3DGETDATA_FLUSH);
618
619 if(block && result == S_FALSE)
620 {
621 // Keep polling, but allow other threads to do something useful first
622 Sleep(0);
623 // explicitly check for device loss
624 // some drivers seem to return S_FALSE even if the device is lost
625 // instead of D3DERR_DEVICELOST like they should
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +0000626 if (testDeviceLost(false))
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000627 {
628 result = D3DERR_DEVICELOST;
629 }
630 }
631 }
632 while(block && result == S_FALSE);
633
634 freeEventQuery(query);
635
636 if (isDeviceLostError(result))
637 {
638 mDisplay->notifyDeviceLost();
639 }
640}
641
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000642SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
643{
daniel@transgaming.coma27e05b2012-11-28 19:39:42 +0000644 return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000645}
646
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000647IDirect3DQuery9* Renderer9::allocateEventQuery()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000648{
649 IDirect3DQuery9 *query = NULL;
650
651 if (mEventQueryPool.empty())
652 {
653 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
654 ASSERT(SUCCEEDED(result));
655 }
656 else
657 {
658 query = mEventQueryPool.back();
659 mEventQueryPool.pop_back();
660 }
661
662 return query;
663}
664
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000665void Renderer9::freeEventQuery(IDirect3DQuery9* query)
daniel@transgaming.comef21ab22012-10-31 17:52:47 +0000666{
667 if (mEventQueryPool.size() > 1000)
668 {
669 query->Release();
670 }
671 else
672 {
673 mEventQueryPool.push_back(query);
674 }
675}
676
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000677IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000678{
679 return mVertexShaderCache.create(function, length);
680}
681
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000682IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length)
daniel@transgaming.come4733d72012-10-31 18:07:01 +0000683{
684 return mPixelShaderCache.create(function, length);
685}
686
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000687HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer)
688{
689 D3DPOOL Pool = getBufferPool(Usage);
690 return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL);
691}
692
daniel@transgaming.com3f255b42012-12-20 21:07:35 +0000693VertexBuffer *Renderer9::createVertexBuffer()
694{
695 return new VertexBuffer9(this);
696}
697
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000698HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer)
699{
700 D3DPOOL Pool = getBufferPool(Usage);
701 return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL);
702}
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000703
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +0000704IndexBuffer *Renderer9::createIndexBuffer()
705{
706 return new IndexBuffer9(this);
707}
708
shannon.woods@transgaming.com50df6c52013-02-28 23:02:49 +0000709QueryImpl *Renderer9::createQuery(GLenum type)
710{
711 return new Query9(this, type);
712}
713
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000714void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000715{
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000716 bool *forceSetSamplers = (type == gl::SAMPLER_PIXEL) ? mForceSetPixelSamplerStates : mForceSetVertexSamplerStates;
717 gl::SamplerState *appliedSamplers = (type == gl::SAMPLER_PIXEL) ? mCurPixelSamplerStates: mCurVertexSamplerStates;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000718
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000719 if (forceSetSamplers[index] || memcmp(&samplerState, &appliedSamplers[index], sizeof(gl::SamplerState)) != 0)
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000720 {
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000721 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
722 int d3dSampler = index + d3dSamplerOffset;
723
724 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS));
725 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT));
726
727 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy));
728 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
729 gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy);
730 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
731 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
732 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset);
733 if (mSupportsTextureFilterAnisotropy)
734 {
735 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy);
736 }
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000737 }
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000738
739 forceSetSamplers[index] = false;
740 appliedSamplers[index] = samplerState;
daniel@transgaming.comba0570e2012-10-31 18:07:39 +0000741}
742
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000743void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000744{
745 int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
746 int d3dSampler = index + d3dSamplerOffset;
747 IDirect3DBaseTexture9 *d3dTexture = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000748 unsigned int serial = 0;
749 bool forceSetTexture = false;
750
751 unsigned int *appliedSerials = (type == gl::SAMPLER_PIXEL) ? mCurPixelTextureSerials : mCurVertexTextureSerials;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000752
753 if (texture)
754 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000755 TextureStorageInterface *texStorage = texture->getNativeTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000756 if (texStorage)
757 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000758 TextureStorage9 *storage9 = TextureStorage9::makeTextureStorage9(texStorage->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +0000759 d3dTexture = storage9->getBaseTexture();
daniel@transgaming.com9d4346f2012-10-31 19:52:04 +0000760 }
761 // If we get NULL back from getBaseTexture here, something went wrong
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000762 // in the texture class and we're unexpectedly missing the d3d texture
763 ASSERT(d3dTexture != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000764
765 serial = texture->getTextureSerial();
766 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000767 }
768
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000769 if (forceSetTexture || appliedSerials[index] != serial)
770 {
771 mDevice->SetTexture(d3dSampler, d3dTexture);
772 }
773
774 appliedSerials[index] = serial;
daniel@transgaming.coma734f272012-10-31 18:07:48 +0000775}
776
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000777void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000778{
779 bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000780
781 if (rasterStateChanged)
782 {
783 // Set the cull mode
784 if (rasterState.cullFace)
785 {
786 mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace));
787 }
788 else
789 {
790 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
791 }
792
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000793 if (rasterState.polygonOffsetFill)
794 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000795 if (mCurDepthSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000796 {
797 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor);
798
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000799 float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize));
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000800 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias);
801 }
802 }
803 else
804 {
805 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
806 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
807 }
808
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000809 mCurRasterState = rasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000810 }
811
812 mForceSetRasterState = false;
813}
814
815void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask)
816{
817 bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0;
818 bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0;
819 bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask;
820
821 if (blendStateChanged || blendColorChanged)
822 {
823 if (blendState.blend)
824 {
825 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
826
827 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
828 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
829 {
830 mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor));
831 }
832 else
833 {
834 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha),
835 gl::unorm<8>(blendColor.alpha),
836 gl::unorm<8>(blendColor.alpha),
837 gl::unorm<8>(blendColor.alpha)));
838 }
839
840 mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB));
841 mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB));
842 mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB));
843
844 if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha ||
845 blendState.destBlendRGB != blendState.destBlendAlpha ||
846 blendState.blendEquationRGB != blendState.blendEquationAlpha)
847 {
848 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
849
850 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha));
851 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha));
852 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha));
853 }
854 else
855 {
856 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
857 }
858 }
859 else
860 {
861 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
862 }
863
864 if (blendState.sampleAlphaToCoverage)
865 {
866 FIXME("Sample alpha to coverage is unimplemented.");
867 }
868
869 // Set the color mask
870 bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD;
871 // Apparently some ATI cards have a bug where a draw with a zero color
872 // write mask can cause later draws to have incorrect results. Instead,
873 // set a nonzero color write mask but modify the blend state so that no
874 // drawing is done.
875 // http://code.google.com/p/angleproject/issues/detail?id=169
876
877 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen,
878 blendState.colorMaskBlue, blendState.colorMaskAlpha);
879 if (colorMask == 0 && !zeroColorMaskAllowed)
880 {
881 // Enable green channel, but set blending so nothing will be drawn.
882 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
883 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
884
885 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
886 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
887 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
888 }
889 else
890 {
891 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
892 }
893
894 mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE);
895
896 mCurBlendState = blendState;
897 mCurBlendColor = blendColor;
898 }
899
900 if (sampleMaskChanged)
901 {
902 // Set the multisample mask
903 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
904 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask));
905
906 mCurSampleMask = sampleMask;
907 }
908
909 mForceSetBlendState = false;
910}
911
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000912void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000913 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000914{
915 bool depthStencilStateChanged = mForceSetDepthStencilState ||
916 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000917 bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef ||
918 stencilBackRef != mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000919 bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000920
921 if (depthStencilStateChanged)
922 {
923 if (depthStencilState.depthTest)
924 {
925 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
926 mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc));
927 }
928 else
929 {
930 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
931 }
932
933 mCurDepthStencilState = depthStencilState;
934 }
935
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000936 if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000937 {
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000938 if (depthStencilState.stencilTest && mCurStencilSize > 0)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000939 {
940 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
941 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
942
943 // FIXME: Unsupported by D3D9
944 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
945 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
946 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
947 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000948 stencilRef != stencilBackRef ||
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000949 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
950 {
951 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
952 return error(GL_INVALID_OPERATION);
953 }
954
955 // get the maximum size of the stencil ref
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000956 unsigned int maxStencil = (1 << mCurStencilSize) - 1;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000957
958 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
959 depthStencilState.stencilWritemask);
960 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
961 gl_d3d9::ConvertComparison(depthStencilState.stencilFunc));
962
963 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000964 (stencilRef < (int)maxStencil) ? stencilRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000965 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
966 depthStencilState.stencilMask);
967
968 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
969 gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail));
970 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
971 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail));
972 mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
973 gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass));
974
975 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK,
976 depthStencilState.stencilBackWritemask);
977 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
978 gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc));
979
980 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF,
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000981 (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000982 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK,
983 depthStencilState.stencilBackMask);
984
985 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
986 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail));
987 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
988 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail));
989 mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
990 gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass));
991 }
992 else
993 {
994 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
995 }
996
997 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE);
998
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000999 mCurStencilRef = stencilRef;
1000 mCurStencilBackRef = stencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001001 mCurFrontFaceCCW = frontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001002 }
1003
1004 mForceSetDepthStencilState = false;
1005}
1006
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001007void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001008{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001009 bool scissorChanged = mForceSetScissor ||
1010 memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
1011 enabled != mScissorEnabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001012
daniel@transgaming.com04f1b332012-11-28 21:00:40 +00001013 if (scissorChanged)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001014 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001015 if (enabled)
1016 {
1017 RECT rect;
1018 rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width));
1019 rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height));
1020 rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width));
1021 rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height));
1022 mDevice->SetScissorRect(&rect);
1023 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001024
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +00001025 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE);
1026
1027 mScissorEnabled = enabled;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001028 mCurScissor = scissor;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001029 }
1030
1031 mForceSetScissor = false;
1032}
1033
daniel@transgaming.com12985182012-12-20 20:56:31 +00001034bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +00001035 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001036{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001037 gl::Rectangle actualViewport = viewport;
1038 float actualZNear = gl::clamp01(zNear);
1039 float actualZFar = gl::clamp01(zFar);
1040 if (ignoreViewport)
1041 {
1042 actualViewport.x = 0;
1043 actualViewport.y = 0;
1044 actualViewport.width = mRenderTargetDesc.width;
1045 actualViewport.height = mRenderTargetDesc.height;
1046 actualZNear = 0.0f;
1047 actualZFar = 1.0f;
1048 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001049
1050 D3DVIEWPORT9 dxViewport;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001051 dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width));
1052 dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height));
1053 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X));
1054 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y));
1055 dxViewport.MinZ = actualZNear;
1056 dxViewport.MaxZ = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001057
1058 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
1059 {
1060 return false; // Nothing to render
1061 }
1062
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001063 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
1064 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001065 if (viewportChanged)
1066 {
1067 mDevice->SetViewport(&dxViewport);
1068
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +00001069 mCurViewport = actualViewport;
1070 mCurNear = actualZNear;
1071 mCurFar = actualZFar;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001072
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001073 dx_VertexConstants vc = {0};
1074 dx_PixelConstants pc = {0};
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001075
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001076 vc.halfPixelSize[0] = 1.0f / dxViewport.Width;
1077 vc.halfPixelSize[1] = -1.0f / dxViewport.Height;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001078
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001079 pc.coord[0] = actualViewport.width * 0.5f;
1080 pc.coord[1] = actualViewport.height * 0.5f;
1081 pc.coord[2] = actualViewport.x + (actualViewport.width * 0.5f);
1082 pc.coord[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001083
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001084 pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
1085 pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
1086 pc.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);;
1087
1088 vc.depthRange[0] = actualZNear;
1089 vc.depthRange[1] = actualZFar;
1090 vc.depthRange[2] = actualZFar - actualZNear;
1091
1092 pc.depthRange[0] = actualZNear;
1093 pc.depthRange[1] = actualZFar;
1094 pc.depthRange[2] = actualZFar - actualZNear;
1095
1096 if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0)
1097 {
1098 mVertexConstants = vc;
1099 mDxUniformsDirty = true;
1100 }
1101
1102 if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0)
1103 {
1104 mPixelConstants = pc;
1105 mDxUniformsDirty = true;
1106 }
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +00001107 }
1108
1109 mForceSetViewport = false;
1110 return true;
1111}
1112
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001113bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count)
1114{
1115 switch (mode)
1116 {
1117 case GL_POINTS:
1118 mPrimitiveType = D3DPT_POINTLIST;
1119 mPrimitiveCount = count;
1120 break;
1121 case GL_LINES:
1122 mPrimitiveType = D3DPT_LINELIST;
1123 mPrimitiveCount = count / 2;
1124 break;
1125 case GL_LINE_LOOP:
1126 mPrimitiveType = D3DPT_LINESTRIP;
1127 mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately
1128 break;
1129 case GL_LINE_STRIP:
1130 mPrimitiveType = D3DPT_LINESTRIP;
1131 mPrimitiveCount = count - 1;
1132 break;
1133 case GL_TRIANGLES:
1134 mPrimitiveType = D3DPT_TRIANGLELIST;
1135 mPrimitiveCount = count / 3;
1136 break;
1137 case GL_TRIANGLE_STRIP:
1138 mPrimitiveType = D3DPT_TRIANGLESTRIP;
1139 mPrimitiveCount = count - 2;
1140 break;
1141 case GL_TRIANGLE_FAN:
1142 mPrimitiveType = D3DPT_TRIANGLEFAN;
1143 mPrimitiveCount = count - 2;
1144 break;
1145 default:
1146 return error(GL_INVALID_ENUM, false);
1147 }
1148
1149 return mPrimitiveCount > 0;
1150}
1151
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001152
1153gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer)
1154{
1155 if (!depthbuffer)
1156 {
1157 ERR("Unexpected null depthbuffer for depth-only FBO.");
1158 return NULL;
1159 }
1160
1161 GLsizei width = depthbuffer->getWidth();
1162 GLsizei height = depthbuffer->getHeight();
1163
1164 // search cached nullcolorbuffers
1165 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1166 {
1167 if (mNullColorbufferCache[i].buffer != NULL &&
1168 mNullColorbufferCache[i].width == width &&
1169 mNullColorbufferCache[i].height == height)
1170 {
1171 mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU;
1172 return mNullColorbufferCache[i].buffer;
1173 }
1174 }
1175
1176 gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0));
1177
1178 // add nullbuffer to the cache
1179 NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0];
1180 for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
1181 {
1182 if (mNullColorbufferCache[i].lruCount < oldest->lruCount)
1183 {
1184 oldest = &mNullColorbufferCache[i];
1185 }
1186 }
1187
1188 delete oldest->buffer;
1189 oldest->buffer = nullbuffer;
1190 oldest->lruCount = ++mMaxNullColorbufferLRU;
1191 oldest->width = width;
1192 oldest->height = height;
1193
1194 return nullbuffer;
1195}
1196
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001197bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001198{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001199 // if there is no color attachment we must synthesize a NULL colorattachment
1200 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1201 gl::Renderbuffer *renderbufferObject = NULL;
1202 if (framebuffer->getColorbufferType() != GL_NONE)
1203 {
1204 renderbufferObject = framebuffer->getColorbuffer();
1205 }
1206 else
1207 {
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00001208 renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer());
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001209 }
1210 if (!renderbufferObject)
1211 {
1212 ERR("unable to locate renderbuffer for FBO.");
1213 return false;
1214 }
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001215
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001216 bool renderTargetChanged = false;
1217 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1218 if (renderTargetSerial != mAppliedRenderTargetSerial)
1219 {
1220 // Apply the render target on the device
1221 IDirect3DSurface9 *renderTargetSurface = NULL;
1222
1223 RenderTarget *renderTarget = renderbufferObject->getRenderTarget();
1224 if (renderTarget)
1225 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001226 renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001227 }
1228
1229 if (!renderTargetSurface)
1230 {
1231 ERR("render target pointer unexpectedly null.");
1232 return false; // Context must be lost
1233 }
1234
1235 mDevice->SetRenderTarget(0, renderTargetSurface);
1236 renderTargetSurface->Release();
1237
1238 mAppliedRenderTargetSerial = renderTargetSerial;
1239 renderTargetChanged = true;
1240 }
1241
1242 gl::Renderbuffer *depthStencil = NULL;
1243 unsigned int depthbufferSerial = 0;
1244 unsigned int stencilbufferSerial = 0;
1245 if (framebuffer->getDepthbufferType() != GL_NONE)
1246 {
1247 depthStencil = framebuffer->getDepthbuffer();
1248 if (!depthStencil)
1249 {
1250 ERR("Depth stencil pointer unexpectedly null.");
1251 return false;
1252 }
1253
1254 depthbufferSerial = depthStencil->getSerial();
1255 }
1256 else if (framebuffer->getStencilbufferType() != GL_NONE)
1257 {
1258 depthStencil = framebuffer->getStencilbuffer();
1259 if (!depthStencil)
1260 {
1261 ERR("Depth stencil pointer unexpectedly null.");
1262 return false;
1263 }
1264
1265 stencilbufferSerial = depthStencil->getSerial();
1266 }
1267
1268 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1269 stencilbufferSerial != mAppliedStencilbufferSerial ||
1270 !mDepthStencilInitialized)
1271 {
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001272 unsigned int depthSize = 0;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001273 unsigned int stencilSize = 0;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001274
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001275 // Apply the depth stencil on the device
1276 if (depthStencil)
1277 {
1278 IDirect3DSurface9 *depthStencilSurface = NULL;
1279 RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil();
1280
1281 if (depthStencilRenderTarget)
1282 {
daniel@transgaming.com965bcd22012-11-28 20:54:14 +00001283 depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001284 }
1285
1286 if (!depthStencilSurface)
1287 {
1288 ERR("depth stencil pointer unexpectedly null.");
1289 return false; // Context must be lost
1290 }
1291
1292 mDevice->SetDepthStencilSurface(depthStencilSurface);
1293 depthStencilSurface->Release();
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001294
1295 depthSize = depthStencil->getDepthSize();
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001296 stencilSize = depthStencil->getStencilSize();
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001297 }
1298 else
1299 {
1300 mDevice->SetDepthStencilSurface(NULL);
1301 }
1302
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +00001303 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1304 {
1305 mCurDepthSize = depthSize;
1306 mForceSetRasterState = true;
1307 }
1308
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +00001309 if (!mDepthStencilInitialized || stencilSize != mCurStencilSize)
1310 {
1311 mCurStencilSize = stencilSize;
1312 mForceSetDepthStencilState = true;
1313 }
1314
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001315 mAppliedDepthbufferSerial = depthbufferSerial;
1316 mAppliedStencilbufferSerial = stencilbufferSerial;
1317 mDepthStencilInitialized = true;
1318 }
1319
1320 if (renderTargetChanged || !mRenderTargetDescInitialized)
1321 {
1322 mForceSetScissor = true;
1323 mForceSetViewport = true;
1324
1325 mRenderTargetDesc.width = renderbufferObject->getWidth();
1326 mRenderTargetDesc.height = renderbufferObject->getHeight();
1327 mRenderTargetDesc.format = renderbufferObject->getActualFormat();
1328 mRenderTargetDescInitialized = true;
1329 }
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001330
1331 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001332}
daniel@transgaming.coma734f272012-10-31 18:07:48 +00001333
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001334GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001335{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001336 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001337 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
1338 if (err != GL_NO_ERROR)
1339 {
1340 return err;
1341 }
1342
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001343 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw);
1344}
1345
1346// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com31240482012-11-28 21:06:41 +00001347GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001348{
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001349 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001350
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001351 if (err == GL_NO_ERROR)
1352 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001353 if (indexInfo->serial != mAppliedIBSerial)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001354 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001355 IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer);
1356
1357 mDevice->SetIndices(indexBuffer->getBuffer());
1358 mAppliedIBSerial = indexInfo->serial;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001359 }
1360 }
1361
1362 return err;
1363}
1364
1365void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1366{
1367 startScene();
1368
1369 if (mode == GL_LINE_LOOP)
1370 {
1371 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1372 }
1373 else if (instances > 0)
1374 {
daniel@transgaming.com50cc7252012-12-20 21:09:23 +00001375 StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001376 if (countingIB)
1377 {
1378 if (mAppliedIBSerial != countingIB->getSerial())
1379 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001380 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer());
1381
1382 mDevice->SetIndices(indexBuffer->getBuffer());
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001383 mAppliedIBSerial = countingIB->getSerial();
1384 }
1385
1386 for (int i = 0; i < mRepeatDraw; i++)
1387 {
1388 mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
1389 }
1390 }
1391 else
1392 {
1393 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
1394 return error(GL_OUT_OF_MEMORY);
1395 }
1396 }
1397 else // Regular case
1398 {
1399 mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
1400 }
1401}
1402
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001403void 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 +00001404{
1405 startScene();
1406
1407 if (mode == GL_LINE_LOOP)
1408 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001409 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001410 }
1411 else
1412 {
1413 for (int i = 0; i < mRepeatDraw; i++)
1414 {
daniel@transgaming.com97400dd2012-11-28 20:57:00 +00001415 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
1416 mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001417 }
1418 }
1419}
1420
1421void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1422{
1423 // Get the raw indices for an indexed draw
1424 if (type != GL_NONE && elementArrayBuffer)
1425 {
1426 gl::Buffer *indexBuffer = elementArrayBuffer;
1427 intptr_t offset = reinterpret_cast<intptr_t>(indices);
1428 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
1429 }
1430
1431 UINT startIndex = 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001432
1433 if (get32BitIndexSupport())
1434 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001435 if (!mLineLoopIB)
1436 {
1437 mLineLoopIB = new StreamingIndexBufferInterface(this);
1438 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1439 {
1440 delete mLineLoopIB;
1441 mLineLoopIB = NULL;
1442
1443 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
1444 return error(GL_OUT_OF_MEMORY);
1445 }
1446 }
1447
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001448 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001449 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001450 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001451 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
1452 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001453 }
1454
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001455 void* mappedMemory = NULL;
1456 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1457 if (offset == -1 || mappedMemory == NULL)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001458 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001459 ERR("Could not map index buffer for GL_LINE_LOOP.");
1460 return error(GL_OUT_OF_MEMORY);
1461 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001462
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001463 startIndex = static_cast<UINT>(offset) / 4;
1464 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1465
1466 switch (type)
1467 {
1468 case GL_NONE: // Non-indexed draw
1469 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001470 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001471 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001472 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001473 data[count] = 0;
1474 break;
1475 case GL_UNSIGNED_BYTE:
1476 for (int i = 0; i < count; i++)
1477 {
1478 data[i] = static_cast<const GLubyte*>(indices)[i];
1479 }
1480 data[count] = static_cast<const GLubyte*>(indices)[0];
1481 break;
1482 case GL_UNSIGNED_SHORT:
1483 for (int i = 0; i < count; i++)
1484 {
1485 data[i] = static_cast<const GLushort*>(indices)[i];
1486 }
1487 data[count] = static_cast<const GLushort*>(indices)[0];
1488 break;
1489 case GL_UNSIGNED_INT:
1490 for (int i = 0; i < count; i++)
1491 {
1492 data[i] = static_cast<const GLuint*>(indices)[i];
1493 }
1494 data[count] = static_cast<const GLuint*>(indices)[0];
1495 break;
1496 default: UNREACHABLE();
1497 }
1498
1499 if (!mLineLoopIB->unmapBuffer())
1500 {
1501 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
1502 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001503 }
1504 }
1505 else
1506 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001507 if (!mLineLoopIB)
1508 {
1509 mLineLoopIB = new StreamingIndexBufferInterface(this);
1510 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT))
1511 {
1512 delete mLineLoopIB;
1513 mLineLoopIB = NULL;
1514
1515 ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP.");
1516 return error(GL_OUT_OF_MEMORY);
1517 }
1518 }
1519
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001520 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001521 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT))
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001522 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001523 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
1524 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001525 }
1526
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001527 void* mappedMemory = NULL;
1528 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1529 if (offset == -1 || mappedMemory == NULL)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001530 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001531 ERR("Could not map index buffer for GL_LINE_LOOP.");
1532 return error(GL_OUT_OF_MEMORY);
1533 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001534
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001535 startIndex = static_cast<UINT>(offset) / 2;
1536 unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory);
1537
1538 switch (type)
1539 {
1540 case GL_NONE: // Non-indexed draw
1541 for (int i = 0; i < count; i++)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001542 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001543 data[i] = i;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001544 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001545 data[count] = 0;
1546 break;
1547 case GL_UNSIGNED_BYTE:
1548 for (int i = 0; i < count; i++)
1549 {
1550 data[i] = static_cast<const GLubyte*>(indices)[i];
1551 }
1552 data[count] = static_cast<const GLubyte*>(indices)[0];
1553 break;
1554 case GL_UNSIGNED_SHORT:
1555 for (int i = 0; i < count; i++)
1556 {
1557 data[i] = static_cast<const GLushort*>(indices)[i];
1558 }
1559 data[count] = static_cast<const GLushort*>(indices)[0];
1560 break;
1561 case GL_UNSIGNED_INT:
1562 for (int i = 0; i < count; i++)
1563 {
1564 data[i] = static_cast<const GLuint*>(indices)[i];
1565 }
1566 data[count] = static_cast<const GLuint*>(indices)[0];
1567 break;
1568 default: UNREACHABLE();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001569 }
1570
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001571 if (!mLineLoopIB->unmapBuffer())
1572 {
1573 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
1574 return error(GL_OUT_OF_MEMORY);
1575 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001576 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001577
1578 if (mAppliedIBSerial != mLineLoopIB->getSerial())
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001579 {
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001580 IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer());
1581
1582 mDevice->SetIndices(indexBuffer->getBuffer());
1583 mAppliedIBSerial = mLineLoopIB->getSerial();
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001584 }
daniel@transgaming.com1e3a8042012-12-20 21:09:55 +00001585
1586 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001587}
1588
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001589void Renderer9::applyShaders(gl::ProgramBinary *programBinary)
1590{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001591 unsigned int programBinarySerial = programBinary->getSerial();
1592 if (programBinarySerial != mAppliedProgramBinarySerial)
1593 {
1594 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1595 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001596
daniel@transgaming.come4991412012-12-20 20:55:34 +00001597 IDirect3DVertexShader9 *vertexShader = NULL;
1598 if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader();
daniel@transgaming.com95892412012-11-28 20:59:09 +00001599
daniel@transgaming.come4991412012-12-20 20:55:34 +00001600 IDirect3DPixelShader9 *pixelShader = NULL;
1601 if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader();
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001602
daniel@transgaming.come4991412012-12-20 20:55:34 +00001603 mDevice->SetPixelShader(pixelShader);
1604 mDevice->SetVertexShader(vertexShader);
1605 programBinary->dirtyAllUniforms();
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001606 mDxUniformsDirty = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001607
1608 mAppliedProgramBinarySerial = programBinarySerial;
1609 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001610}
1611
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001612void Renderer9::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001613{
1614 for (std::vector<gl::Uniform*>::const_iterator ub = uniformArray->begin(), ue = uniformArray->end(); ub != ue; ++ub)
1615 {
1616 gl::Uniform *targetUniform = *ub;
1617
1618 if (targetUniform->dirty)
1619 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001620 int count = targetUniform->elementCount();
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001621 GLfloat *f = (GLfloat*)targetUniform->data;
1622 GLint *i = (GLint*)targetUniform->data;
1623 GLboolean *b = (GLboolean*)targetUniform->data;
1624
1625 switch (targetUniform->type)
1626 {
1627 case GL_SAMPLER_2D:
1628 case GL_SAMPLER_CUBE:
1629 break;
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001630 case GL_BOOL: applyUniformnbv(targetUniform, count, 1, b); break;
1631 case GL_BOOL_VEC2: applyUniformnbv(targetUniform, count, 2, b); break;
1632 case GL_BOOL_VEC3: applyUniformnbv(targetUniform, count, 3, b); break;
1633 case GL_BOOL_VEC4: applyUniformnbv(targetUniform, count, 4, b); break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001634 case GL_FLOAT:
1635 case GL_FLOAT_VEC2:
1636 case GL_FLOAT_VEC3:
1637 case GL_FLOAT_VEC4:
1638 case GL_FLOAT_MAT2:
1639 case GL_FLOAT_MAT3:
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001640 case GL_FLOAT_MAT4: applyUniformnfv(targetUniform, f); break;
1641 case GL_INT: applyUniform1iv(targetUniform, count, i); break;
1642 case GL_INT_VEC2: applyUniform2iv(targetUniform, count, i); break;
1643 case GL_INT_VEC3: applyUniform3iv(targetUniform, count, i); break;
1644 case GL_INT_VEC4: applyUniform4iv(targetUniform, count, i); break;
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001645 default:
1646 UNREACHABLE();
1647 }
1648
1649 targetUniform->dirty = false;
1650 }
1651 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001652
1653 // Driver uniforms
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001654 if (mDxUniformsDirty)
1655 {
1656 mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4]));
1657 mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4]));
1658 mDxUniformsDirty = false;
1659 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001660}
1661
1662void Renderer9::applyUniformnbv(gl::Uniform *targetUniform, GLsizei count, int width, const GLboolean *v)
1663{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001664 float vector[D3D9_MAX_FLOAT_CONSTANTS * 4];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001665
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001666 if (targetUniform->psRegisterIndex >= 0 || targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001667 {
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001668 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001669 for (int i = 0; i < count; i++)
1670 {
1671 for (int j = 0; j < 4; j++)
1672 {
1673 if (j < width)
1674 {
1675 vector[i * 4 + j] = (v[i * width + j] == GL_FALSE) ? 0.0f : 1.0f;
1676 }
1677 else
1678 {
1679 vector[i * 4 + j] = 0.0f;
1680 }
1681 }
1682 }
1683 }
1684
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001685 applyUniformnfv(targetUniform, vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001686}
1687
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001688void Renderer9::applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001689{
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001690 if (targetUniform->psRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001691 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001692 mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001693 }
1694
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001695 if (targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001696 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001697 mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001698 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001699}
1700
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001701void Renderer9::applyUniform1iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001702{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001703 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
1704 gl::Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001705
1706 for (int i = 0; i < count; i++)
1707 {
1708 vector[i] = gl::Vector4((float)v[i], 0, 0, 0);
1709 }
1710
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001711 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001712}
1713
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001714void Renderer9::applyUniform2iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001715{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001716 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
1717 gl::Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001718
1719 for (int i = 0; i < count; i++)
1720 {
1721 vector[i] = gl::Vector4((float)v[0], (float)v[1], 0, 0);
1722
1723 v += 2;
1724 }
1725
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001726 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001727}
1728
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001729void Renderer9::applyUniform3iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001730{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001731 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
1732 gl::Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001733
1734 for (int i = 0; i < count; i++)
1735 {
1736 vector[i] = gl::Vector4((float)v[0], (float)v[1], (float)v[2], 0);
1737
1738 v += 3;
1739 }
1740
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001741 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001742}
1743
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001744void Renderer9::applyUniform4iv(gl::Uniform *targetUniform, GLsizei count, const GLint *v)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001745{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001746 ASSERT(count <= D3D9_MAX_FLOAT_CONSTANTS);
1747 gl::Vector4 vector[D3D9_MAX_FLOAT_CONSTANTS];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001748
1749 for (int i = 0; i < count; i++)
1750 {
1751 vector[i] = gl::Vector4((float)v[0], (float)v[1], (float)v[2], (float)v[3]);
1752
1753 v += 4;
1754 }
1755
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001756 applyUniformnfv(targetUniform, (const GLfloat*)vector);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001757}
1758
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001759void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001760{
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001761 D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha),
1762 gl::unorm<8>(clearParams.colorClearValue.red),
1763 gl::unorm<8>(clearParams.colorClearValue.green),
1764 gl::unorm<8>(clearParams.colorClearValue.blue));
1765 float depth = gl::clamp01(clearParams.depthClearValue);
1766 int stencil = clearParams.stencilClearValue & 0x000000FF;
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001767
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001768 unsigned int stencilUnmasked = 0x0;
1769 if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil())
1770 {
1771 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1772 stencilUnmasked = (0x1 << stencilSize) - 1;
1773 }
1774
1775 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1776
1777 const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1778 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
1779 const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1780 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1781 clearParams.colorMaskBlue && alphaUnmasked);
1782
1783 if (needMaskedColorClear || needMaskedStencilClear)
1784 {
1785 // State which is altered in all paths from this point to the clear call is saved.
1786 // State which is altered in only some paths will be flagged dirty in the case that
1787 // that path is taken.
1788 HRESULT hr;
1789 if (mMaskedClearSavedState == NULL)
1790 {
1791 hr = mDevice->BeginStateBlock();
1792 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1793
1794 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1795 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1796 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1797 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1798 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1799 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1800 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1801 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1802 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1803 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1804 mDevice->SetPixelShader(NULL);
1805 mDevice->SetVertexShader(NULL);
1806 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1807 mDevice->SetStreamSource(0, NULL, 0, 0);
1808 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1809 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1810 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1811 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1812 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1813 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1814 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1815
1816 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1817 {
1818 mDevice->SetStreamSourceFreq(i, 1);
1819 }
1820
1821 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
1822 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
1823 }
1824
1825 ASSERT(mMaskedClearSavedState != NULL);
1826
1827 if (mMaskedClearSavedState != NULL)
1828 {
1829 hr = mMaskedClearSavedState->Capture();
1830 ASSERT(SUCCEEDED(hr));
1831 }
1832
1833 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1834 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1835 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
1836 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1837 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1838 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1839 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1840 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1841
1842 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1843 {
1844 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE,
1845 gl_d3d9::ConvertColorMask(clearParams.colorMaskRed,
1846 clearParams.colorMaskGreen,
1847 clearParams.colorMaskBlue,
1848 clearParams.colorMaskAlpha));
1849 }
1850 else
1851 {
1852 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1853 }
1854
1855 if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT))
1856 {
1857 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1858 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1859 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1860 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
1861 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
1862 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
1863 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1864 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1865 }
1866 else
1867 {
1868 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1869 }
1870
1871 mDevice->SetPixelShader(NULL);
1872 mDevice->SetVertexShader(NULL);
1873 mDevice->SetFVF(D3DFVF_XYZRHW);
1874 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1875 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1876 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
1877 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
1878 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
1879 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
1880 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1881
1882 for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
1883 {
1884 mDevice->SetStreamSourceFreq(i, 1);
1885 }
1886
1887 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
1888 quad[0][0] = -0.5f;
1889 quad[0][1] = mRenderTargetDesc.height - 0.5f;
1890 quad[0][2] = 0.0f;
1891 quad[0][3] = 1.0f;
1892
1893 quad[1][0] = mRenderTargetDesc.width - 0.5f;
1894 quad[1][1] = mRenderTargetDesc.height - 0.5f;
1895 quad[1][2] = 0.0f;
1896 quad[1][3] = 1.0f;
1897
1898 quad[2][0] = -0.5f;
1899 quad[2][1] = -0.5f;
1900 quad[2][2] = 0.0f;
1901 quad[2][3] = 1.0f;
1902
1903 quad[3][0] = mRenderTargetDesc.width - 0.5f;
1904 quad[3][1] = -0.5f;
1905 quad[3][2] = 0.0f;
1906 quad[3][3] = 1.0f;
1907
1908 startScene();
1909 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
1910
1911 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1912 {
1913 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
1914 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1915 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1916 }
1917
1918 if (mMaskedClearSavedState != NULL)
1919 {
1920 mMaskedClearSavedState->Apply();
1921 }
1922 }
1923 else if (clearParams.mask)
1924 {
1925 DWORD dxClearFlags = 0;
1926 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1927 {
1928 dxClearFlags |= D3DCLEAR_TARGET;
1929 }
1930 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1931 {
1932 dxClearFlags |= D3DCLEAR_ZBUFFER;
1933 }
1934 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1935 {
1936 dxClearFlags |= D3DCLEAR_STENCIL;
1937 }
1938
1939 mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil);
1940 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001941}
1942
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001943void Renderer9::markAllStateDirty()
1944{
daniel@transgaming.com220e79a2012-11-28 19:42:11 +00001945 mAppliedRenderTargetSerial = 0;
1946 mAppliedDepthbufferSerial = 0;
1947 mAppliedStencilbufferSerial = 0;
1948 mDepthStencilInitialized = false;
1949 mRenderTargetDescInitialized = false;
1950
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001951 mForceSetDepthStencilState = true;
1952 mForceSetRasterState = true;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001953 mForceSetScissor = true;
1954 mForceSetViewport = true;
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001955 mForceSetBlendState = true;
1956
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001957 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001958 {
1959 mForceSetVertexSamplerStates[i] = true;
1960 mCurVertexTextureSerials[i] = 0;
1961 }
1962 for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1963 {
1964 mForceSetPixelSamplerStates[i] = true;
1965 mCurPixelTextureSerials[i] = 0;
1966 }
1967
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001968 mAppliedIBSerial = 0;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001969 mAppliedProgramBinarySerial = 0;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +00001970 mDxUniformsDirty = true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001971
1972 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001973}
1974
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001975void Renderer9::releaseDeviceResources()
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00001976{
1977 while (!mEventQueryPool.empty())
1978 {
1979 mEventQueryPool.back()->Release();
1980 mEventQueryPool.pop_back();
1981 }
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001982
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +00001983 if (mMaskedClearSavedState)
1984 {
1985 mMaskedClearSavedState->Release();
1986 mMaskedClearSavedState = NULL;
1987 }
1988
daniel@transgaming.come4733d72012-10-31 18:07:01 +00001989 mVertexShaderCache.clear();
1990 mPixelShaderCache.clear();
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001991
daniel@transgaming.come569fc52012-11-28 20:56:02 +00001992 delete mBlit;
1993 mBlit = NULL;
1994
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00001995 delete mVertexDataManager;
1996 mVertexDataManager = NULL;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001997
1998 delete mIndexDataManager;
1999 mIndexDataManager = NULL;
2000
2001 delete mLineLoopIB;
2002 mLineLoopIB = NULL;
daniel@transgaming.come4e1a332012-12-20 20:52:09 +00002003
2004 for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
2005 {
2006 delete mNullColorbufferCache[i].buffer;
2007 mNullColorbufferCache[i].buffer = NULL;
2008 }
2009
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002010}
2011
2012
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002013void Renderer9::markDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002014{
2015 mDeviceLost = true;
2016}
2017
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002018bool Renderer9::isDeviceLost()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002019{
2020 return mDeviceLost;
2021}
2022
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002023// set notify to true to broadcast a message to all contexts of the device loss
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002024bool Renderer9::testDeviceLost(bool notify)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002025{
2026 bool isLost = false;
2027
2028 if (mDeviceEx)
2029 {
2030 isLost = FAILED(mDeviceEx->CheckDeviceState(NULL));
2031 }
2032 else if (mDevice)
2033 {
2034 isLost = FAILED(mDevice->TestCooperativeLevel());
2035 }
2036 else
2037 {
2038 // No device yet, so no reset required
2039 }
2040
2041 if (isLost)
2042 {
2043 // ensure we note the device loss --
2044 // we'll probably get this done again by markDeviceLost
2045 // but best to remember it!
2046 // Note that we don't want to clear the device loss status here
2047 // -- this needs to be done by resetDevice
2048 mDeviceLost = true;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002049 if (notify)
2050 {
2051 mDisplay->notifyDeviceLost();
2052 }
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002053 }
2054
2055 return isLost;
2056}
2057
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002058bool Renderer9::testDeviceResettable()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002059{
2060 HRESULT status = D3D_OK;
2061
2062 if (mDeviceEx)
2063 {
2064 status = mDeviceEx->CheckDeviceState(NULL);
2065 }
2066 else if (mDevice)
2067 {
2068 status = mDevice->TestCooperativeLevel();
2069 }
2070
2071 switch (status)
2072 {
2073 case D3DERR_DEVICENOTRESET:
2074 case D3DERR_DEVICEHUNG:
2075 return true;
2076 default:
2077 return false;
2078 }
2079}
2080
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002081bool Renderer9::resetDevice()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002082{
daniel@transgaming.comef21ab22012-10-31 17:52:47 +00002083 releaseDeviceResources();
2084
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002085 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
2086
2087 HRESULT result = D3D_OK;
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002088 bool lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002089 int attempts = 3;
2090
2091 while (lost && attempts > 0)
2092 {
2093 if (mDeviceEx)
2094 {
2095 Sleep(500); // Give the graphics driver some CPU time
2096 result = mDeviceEx->ResetEx(&presentParameters, NULL);
2097 }
2098 else
2099 {
2100 result = mDevice->TestCooperativeLevel();
2101 while (result == D3DERR_DEVICELOST)
2102 {
2103 Sleep(100); // Give the graphics driver some CPU time
2104 result = mDevice->TestCooperativeLevel();
2105 }
2106
2107 if (result == D3DERR_DEVICENOTRESET)
2108 {
2109 result = mDevice->Reset(&presentParameters);
2110 }
2111 }
2112
daniel@transgaming.comf688c0d2012-10-31 17:52:57 +00002113 lost = testDeviceLost(false);
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002114 attempts --;
2115 }
2116
2117 if (FAILED(result))
2118 {
2119 ERR("Reset/ResetEx failed multiple times: 0x%08X", result);
2120 return false;
2121 }
2122
2123 // reset device defaults
2124 initializeDevice();
2125 mDeviceLost = false;
2126
2127 return true;
2128}
2129
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002130DWORD Renderer9::getAdapterVendor() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002131{
2132 return mAdapterIdentifier.VendorId;
2133}
2134
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002135std::string Renderer9::getRendererDescription() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002136{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002137 std::ostringstream rendererString;
2138
2139 rendererString << mAdapterIdentifier.Description;
2140 if (getShareHandleSupport())
2141 {
2142 rendererString << " Direct3D9Ex";
2143 }
2144 else
2145 {
2146 rendererString << " Direct3D9";
2147 }
2148
2149 rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
2150 rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
2151
2152 return rendererString.str();
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002153}
2154
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002155GUID Renderer9::getAdapterIdentifier() const
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00002156{
2157 return mAdapterIdentifier.DeviceIdentifier;
2158}
2159
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002160void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002161{
2162 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
2163 {
2164 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
2165 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
2166
2167 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
2168 }
2169}
2170
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002171bool Renderer9::getBGRATextureSupport() const
2172{
2173 // DirectX 9 always supports BGRA
2174 return true;
2175}
2176
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002177bool Renderer9::getDXT1TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002178{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002179 return mDXT1TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002180}
2181
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002182bool Renderer9::getDXT3TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002183{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002184 return mDXT3TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002185}
2186
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002187bool Renderer9::getDXT5TextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002188{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002189 return mDXT5TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002190}
2191
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002192bool Renderer9::getDepthTextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002193{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002194 return mDepthTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002195}
2196
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002197bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002198{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002199 *filtering = mFloat32FilterSupport;
2200 *renderable = mFloat32RenderSupport;
2201 return mFloat32TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002202}
2203
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002204bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002205{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002206 *filtering = mFloat16FilterSupport;
2207 *renderable = mFloat16RenderSupport;
2208 return mFloat16TextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002209}
2210
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002211bool Renderer9::getLuminanceTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002212{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002213 return mLuminanceTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002214}
2215
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002216bool Renderer9::getLuminanceAlphaTextureSupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002217{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002218 return mLuminanceAlphaTextureSupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002219}
2220
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002221bool Renderer9::getTextureFilterAnisotropySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002222{
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002223 return mSupportsTextureFilterAnisotropy;
2224}
2225
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002226float Renderer9::getTextureMaxAnisotropy() const
daniel@transgaming.comba0570e2012-10-31 18:07:39 +00002227{
2228 if (mSupportsTextureFilterAnisotropy)
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002229 {
2230 return mDeviceCaps.MaxAnisotropy;
2231 }
2232 return 1.0f;
2233}
2234
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002235bool Renderer9::getEventQuerySupport()
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002236{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002237 return mEventQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002238}
2239
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002240unsigned int Renderer9::getMaxVertexTextureImageUnits() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002241{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002242 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2243 return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002244}
2245
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002246unsigned int Renderer9::getMaxCombinedTextureImageUnits() const
2247{
2248 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2249}
2250
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002251int Renderer9::getMaxVertexUniformVectors() const
2252{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002253 return MAX_VERTEX_UNIFORM_VECTORS;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002254}
2255
2256int Renderer9::getMaxFragmentUniformVectors() const
2257{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002258 return getMajorShaderModel() >= 3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002259}
2260
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002261int Renderer9::getMaxVaryingVectors() const
2262{
2263 return getMajorShaderModel() >= 3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2264}
2265
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002266bool Renderer9::getNonPower2TextureSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002267{
2268 return mSupportsNonPower2Textures;
2269}
2270
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002271bool Renderer9::getOcclusionQuerySupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002272{
daniel@transgaming.com669c9952013-01-11 04:08:16 +00002273 return mOcclusionQuerySupport;
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002274}
2275
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002276bool Renderer9::getInstancingSupport() const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002277{
2278 return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
2279}
2280
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002281bool Renderer9::getShareHandleSupport() const
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002282{
2283 // PIX doesn't seem to support using share handles, so disable them.
daniel@transgaming.com7cb796e2012-10-31 18:46:44 +00002284 return (mD3d9Ex != NULL) && !gl::perfActive();
daniel@transgaming.com313e3922012-10-31 17:52:39 +00002285}
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002286
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002287bool Renderer9::getDerivativeInstructionSupport() const
2288{
2289 return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0;
2290}
2291
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002292int Renderer9::getMajorShaderModel() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002293{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002294 return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002295}
2296
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002297float Renderer9::getMaxPointSize() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002298{
shannon.woods@transgaming.combd8c10c2013-01-25 21:15:03 +00002299 // Point size clamped at 1.0f for SM2
2300 return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f;
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002301}
2302
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002303int Renderer9::getMaxTextureWidth() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002304{
2305 return (int)mDeviceCaps.MaxTextureWidth;
2306}
2307
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002308int Renderer9::getMaxTextureHeight() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002309{
2310 return (int)mDeviceCaps.MaxTextureHeight;
2311}
2312
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002313bool Renderer9::get32BitIndexSupport() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002314{
2315 return mDeviceCaps.MaxVertexIndex >= (1 << 16);
2316}
2317
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002318DWORD Renderer9::getCapsDeclTypes() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002319{
2320 return mDeviceCaps.DeclTypes;
2321}
2322
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002323int Renderer9::getMinSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002324{
2325 return mMinSwapInterval;
2326}
2327
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002328int Renderer9::getMaxSwapInterval() const
daniel@transgaming.com5f4c1362012-10-31 18:29:00 +00002329{
2330 return mMaxSwapInterval;
2331}
2332
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002333int Renderer9::getMaxSupportedSamples() const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002334{
2335 return mMaxSupportedSamples;
2336}
2337
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002338int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002339{
2340 if (requested == 0)
2341 {
2342 return requested;
2343 }
2344
2345 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2346 if (itr == mMultiSampleSupport.end())
2347 {
daniel@transgaming.com92955622012-10-31 18:38:41 +00002348 if (format == D3DFMT_UNKNOWN)
2349 return 0;
daniel@transgaming.comb7833982012-10-31 18:31:46 +00002350 return -1;
2351 }
2352
2353 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2354 {
2355 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2356 {
2357 return i;
2358 }
2359 }
2360
2361 return -1;
2362}
2363
daniel@transgaming.coma9571682012-11-28 19:33:08 +00002364D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat)
2365{
2366 switch (internalformat)
2367 {
2368 case GL_DEPTH_COMPONENT16:
2369 case GL_DEPTH_COMPONENT32_OES:
2370 case GL_DEPTH24_STENCIL8_OES:
2371 return D3DFMT_INTZ;
2372 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
2373 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
2374 return D3DFMT_DXT1;
2375 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE:
2376 return D3DFMT_DXT3;
2377 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE:
2378 return D3DFMT_DXT5;
2379 case GL_RGBA32F_EXT:
2380 case GL_RGB32F_EXT:
2381 case GL_ALPHA32F_EXT:
2382 case GL_LUMINANCE32F_EXT:
2383 case GL_LUMINANCE_ALPHA32F_EXT:
2384 return D3DFMT_A32B32G32R32F;
2385 case GL_RGBA16F_EXT:
2386 case GL_RGB16F_EXT:
2387 case GL_ALPHA16F_EXT:
2388 case GL_LUMINANCE16F_EXT:
2389 case GL_LUMINANCE_ALPHA16F_EXT:
2390 return D3DFMT_A16B16G16R16F;
2391 case GL_LUMINANCE8_EXT:
2392 if (getLuminanceTextureSupport())
2393 {
2394 return D3DFMT_L8;
2395 }
2396 break;
2397 case GL_LUMINANCE8_ALPHA8_EXT:
2398 if (getLuminanceAlphaTextureSupport())
2399 {
2400 return D3DFMT_A8L8;
2401 }
2402 break;
2403 case GL_RGB8_OES:
2404 case GL_RGB565:
2405 return D3DFMT_X8R8G8B8;
2406 }
2407
2408 return D3DFMT_A8R8G8B8;
2409}
2410
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002411bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002412{
2413 bool result = false;
2414
2415 if (source && dest)
2416 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002417 TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance());
2418 TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002419
2420 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002421 for (int i = 0; i < levels; ++i)
2422 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002423 IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false);
2424 IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002425
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002426 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002427
2428 if (srcSurf) srcSurf->Release();
2429 if (dstSurf) dstSurf->Release();
2430
2431 if (!result)
2432 return false;
2433 }
2434 }
2435
2436 return result;
2437}
2438
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002439bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002440{
2441 bool result = false;
2442
2443 if (source && dest)
2444 {
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002445 TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance());
2446 TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance());
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002447 int levels = source9->levelCount();
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002448 for (int f = 0; f < 6; f++)
2449 {
2450 for (int i = 0; i < levels; i++)
2451 {
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002452 IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false);
2453 IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002454
daniel@transgaming.com34da3972012-12-20 21:10:29 +00002455 result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged());
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00002456
2457 if (srcSurf) srcSurf->Release();
2458 if (dstSurf) dstSurf->Release();
2459
2460 if (!result)
2461 return false;
2462 }
2463 }
2464 }
2465
2466 return result;
2467}
2468
daniel@transgaming.com2507f412012-10-31 18:46:48 +00002469D3DPOOL Renderer9::getBufferPool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00002470{
2471 if (mD3d9Ex != NULL)
2472 {
2473 return D3DPOOL_DEFAULT;
2474 }
2475 else
2476 {
2477 if (!(usage & D3DUSAGE_DYNAMIC))
2478 {
2479 return D3DPOOL_MANAGED;
2480 }
2481 }
2482
2483 return D3DPOOL_DEFAULT;
2484}
2485
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002486bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002487 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002488{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002489 RECT rect;
2490 rect.left = sourceRect.x;
2491 rect.top = sourceRect.y;
2492 rect.right = sourceRect.x + sourceRect.width;
2493 rect.bottom = sourceRect.y + sourceRect.height;
2494
2495 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002496}
2497
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002498bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002499 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002500{
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002501 RECT rect;
2502 rect.left = sourceRect.x;
2503 rect.top = sourceRect.y;
2504 rect.right = sourceRect.x + sourceRect.width;
2505 rect.bottom = sourceRect.y + sourceRect.height;
2506
2507 return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00002508}
2509
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002510bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, gl::Rectangle *readRect, gl::Framebuffer *drawFramebuffer, gl::Rectangle *drawRect,
2511 bool blitRenderTarget, bool blitDepthStencil)
2512{
2513 endScene();
2514
2515 if (blitRenderTarget)
2516 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002517 gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer();
2518 gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer();
2519 RenderTarget9 *readRenderTarget = NULL;
2520 RenderTarget9 *drawRenderTarget = NULL;
2521 IDirect3DSurface9* readSurface = NULL;
2522 IDirect3DSurface9* drawSurface = NULL;
2523
2524 if (readBuffer)
2525 {
2526 readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget());
2527 }
2528 if (drawBuffer)
2529 {
2530 drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget());
2531 }
2532
2533 if (readRenderTarget)
2534 {
2535 readSurface = readRenderTarget->getSurface();
2536 }
2537 if (drawRenderTarget)
2538 {
2539 drawSurface = drawRenderTarget->getSurface();
2540 }
2541
2542 if (!readSurface || !drawSurface)
2543 {
2544 ERR("Failed to retrieve the render target.");
2545 return error(GL_OUT_OF_MEMORY, false);
2546 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002547
2548 RECT srcRect, dstRect;
2549 RECT *srcRectPtr = NULL;
2550 RECT *dstRectPtr = NULL;
2551
2552 if (readRect)
2553 {
2554 srcRect.left = readRect->x;
2555 srcRect.right = readRect->x + readRect->width;
2556 srcRect.top = readRect->y;
2557 srcRect.bottom = readRect->y + readRect->height;
2558 srcRectPtr = &srcRect;
2559 }
2560
2561 if (drawRect)
2562 {
2563 dstRect.left = drawRect->x;
2564 dstRect.right = drawRect->x + drawRect->width;
2565 dstRect.top = drawRect->y;
2566 dstRect.bottom = drawRect->y + drawRect->height;
2567 dstRectPtr = &dstRect;
2568 }
2569
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002570 HRESULT result = mDevice->StretchRect(readSurface, srcRectPtr, drawSurface, dstRectPtr, D3DTEXF_NONE);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002571
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002572 readSurface->Release();
2573 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002574
2575 if (FAILED(result))
2576 {
2577 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2578 return false;
2579 }
2580 }
2581
2582 if (blitDepthStencil)
2583 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002584 gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer();
2585 gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer();
2586 RenderTarget9 *readDepthStencil = NULL;
2587 RenderTarget9 *drawDepthStencil = NULL;
2588 IDirect3DSurface9* readSurface = NULL;
2589 IDirect3DSurface9* drawSurface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002590
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002591 if (readBuffer)
2592 {
2593 readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil());
2594 }
2595 if (drawBuffer)
2596 {
2597 drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil());
2598 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002599
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002600 if (readDepthStencil)
2601 {
2602 readSurface = readDepthStencil->getSurface();
2603 }
2604 if (drawDepthStencil)
2605 {
2606 drawSurface = drawDepthStencil->getSurface();
2607 }
2608
2609 if (!readSurface || !drawSurface)
2610 {
2611 ERR("Failed to retrieve the render target.");
2612 return error(GL_OUT_OF_MEMORY, false);
2613 }
2614
2615 HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE);
2616
2617 readSurface->Release();
2618 drawSurface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002619
2620 if (FAILED(result))
2621 {
2622 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
2623 return false;
2624 }
2625 }
2626
2627 return true;
2628}
2629
2630void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2631 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2632{
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002633 RenderTarget9 *renderTarget = NULL;
2634 IDirect3DSurface9 *surface = NULL;
2635 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
2636
2637 if (colorbuffer)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002638 {
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002639 renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget());
2640 }
2641
2642 if (renderTarget)
2643 {
2644 surface = renderTarget->getSurface();
2645 }
2646
2647 if (!surface)
2648 {
2649 // context must be lost
2650 return;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002651 }
2652
2653 D3DSURFACE_DESC desc;
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002654 surface->GetDesc(&desc);
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002655
2656 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2657 {
2658 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002659 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002660 return error(GL_OUT_OF_MEMORY);
2661 }
2662
2663 HRESULT result;
2664 IDirect3DSurface9 *systemSurface = NULL;
2665 bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() &&
2666 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2667 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2668 if (directToPixels)
2669 {
2670 // Use the pixels ptr as a shared handle to write directly into client's memory
2671 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2672 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2673 if (FAILED(result))
2674 {
2675 // Try again without the shared handle
2676 directToPixels = false;
2677 }
2678 }
2679
2680 if (!directToPixels)
2681 {
2682 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2683 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2684 if (FAILED(result))
2685 {
2686 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002687 surface->Release();
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002688 return error(GL_OUT_OF_MEMORY);
2689 }
2690 }
2691
daniel@transgaming.comd186dc72012-11-28 19:40:16 +00002692 result = mDevice->GetRenderTargetData(surface, systemSurface);
2693 surface->Release();
2694 surface = NULL;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002695
2696 if (FAILED(result))
2697 {
2698 systemSurface->Release();
2699
2700 // It turns out that D3D will sometimes produce more error
2701 // codes than those documented.
daniel@transgaming.com414c9162012-11-28 20:54:57 +00002702 if (checkDeviceLost(result))
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002703 return error(GL_OUT_OF_MEMORY);
2704 else
2705 {
2706 UNREACHABLE();
2707 return;
2708 }
2709
2710 }
2711
2712 if (directToPixels)
2713 {
2714 systemSurface->Release();
2715 return;
2716 }
2717
2718 RECT rect;
2719 rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width));
2720 rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height));
2721 rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2722 rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2723
2724 D3DLOCKED_RECT lock;
2725 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2726
2727 if (FAILED(result))
2728 {
2729 UNREACHABLE();
2730 systemSurface->Release();
2731
2732 return; // No sensible error to generate
2733 }
2734
2735 unsigned char *dest = (unsigned char*)pixels;
2736 unsigned short *dest16 = (unsigned short*)pixels;
2737
2738 unsigned char *source;
2739 int inputPitch;
2740 if (packReverseRowOrder)
2741 {
2742 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2743 inputPitch = -lock.Pitch;
2744 }
2745 else
2746 {
2747 source = (unsigned char*)lock.pBits;
2748 inputPitch = lock.Pitch;
2749 }
2750
2751 unsigned int fastPixelSize = 0;
2752
2753 if (desc.Format == D3DFMT_A8R8G8B8 &&
2754 format == GL_BGRA_EXT &&
2755 type == GL_UNSIGNED_BYTE)
2756 {
2757 fastPixelSize = 4;
2758 }
2759 else if ((desc.Format == D3DFMT_A4R4G4B4 &&
2760 format == GL_BGRA_EXT &&
2761 type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
2762 (desc.Format == D3DFMT_A1R5G5B5 &&
2763 format == GL_BGRA_EXT &&
2764 type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
2765 {
2766 fastPixelSize = 2;
2767 }
2768 else if (desc.Format == D3DFMT_A16B16G16R16F &&
2769 format == GL_RGBA &&
2770 type == GL_HALF_FLOAT_OES)
2771 {
2772 fastPixelSize = 8;
2773 }
2774 else if (desc.Format == D3DFMT_A32B32G32R32F &&
2775 format == GL_RGBA &&
2776 type == GL_FLOAT)
2777 {
2778 fastPixelSize = 16;
2779 }
2780
2781 for (int j = 0; j < rect.bottom - rect.top; j++)
2782 {
2783 if (fastPixelSize != 0)
2784 {
2785 // Fast path for formats which require no translation:
2786 // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
2787 // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
2788 // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
2789 // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
2790 // D3DFMT_A32B32G32R32F to RGBA/FLOAT
2791 //
2792 // Note that buffers with no alpha go through the slow path below.
2793 memcpy(dest + j * outputPitch,
2794 source + j * inputPitch,
2795 (rect.right - rect.left) * fastPixelSize);
2796 continue;
2797 }
2798
2799 for (int i = 0; i < rect.right - rect.left; i++)
2800 {
2801 float r;
2802 float g;
2803 float b;
2804 float a;
2805
2806 switch (desc.Format)
2807 {
2808 case D3DFMT_R5G6B5:
2809 {
2810 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2811
2812 a = 1.0f;
2813 b = (rgb & 0x001F) * (1.0f / 0x001F);
2814 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2815 r = (rgb & 0xF800) * (1.0f / 0xF800);
2816 }
2817 break;
2818 case D3DFMT_A1R5G5B5:
2819 {
2820 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2821
2822 a = (argb & 0x8000) ? 1.0f : 0.0f;
2823 b = (argb & 0x001F) * (1.0f / 0x001F);
2824 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2825 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2826 }
2827 break;
2828 case D3DFMT_A8R8G8B8:
2829 {
2830 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2831
2832 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2833 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2834 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2835 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2836 }
2837 break;
2838 case D3DFMT_X8R8G8B8:
2839 {
2840 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2841
2842 a = 1.0f;
2843 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2844 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2845 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2846 }
2847 break;
2848 case D3DFMT_A2R10G10B10:
2849 {
2850 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2851
2852 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2853 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2854 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2855 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2856 }
2857 break;
2858 case D3DFMT_A32B32G32R32F:
2859 {
2860 // float formats in D3D are stored rgba, rather than the other way round
2861 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2862 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2863 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2864 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2865 }
2866 break;
2867 case D3DFMT_A16B16G16R16F:
2868 {
2869 // float formats in D3D are stored rgba, rather than the other way round
2870 r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0));
2871 g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1));
2872 b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2));
2873 a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3));
2874 }
2875 break;
2876 default:
2877 UNIMPLEMENTED(); // FIXME
2878 UNREACHABLE();
2879 return;
2880 }
2881
2882 switch (format)
2883 {
2884 case GL_RGBA:
2885 switch (type)
2886 {
2887 case GL_UNSIGNED_BYTE:
2888 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2889 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2890 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2891 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2892 break;
2893 default: UNREACHABLE();
2894 }
2895 break;
2896 case GL_BGRA_EXT:
2897 switch (type)
2898 {
2899 case GL_UNSIGNED_BYTE:
2900 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2901 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2902 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2903 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2904 break;
2905 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2906 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2907 // this type is packed as follows:
2908 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2909 // --------------------------------------------------------------------------------
2910 // | 4th | 3rd | 2nd | 1st component |
2911 // --------------------------------------------------------------------------------
2912 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2913 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2914 ((unsigned short)(15 * a + 0.5f) << 12)|
2915 ((unsigned short)(15 * r + 0.5f) << 8) |
2916 ((unsigned short)(15 * g + 0.5f) << 4) |
2917 ((unsigned short)(15 * b + 0.5f) << 0);
2918 break;
2919 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2920 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2921 // this type is packed as follows:
2922 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2923 // --------------------------------------------------------------------------------
2924 // | 4th | 3rd | 2nd | 1st component |
2925 // --------------------------------------------------------------------------------
2926 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2927 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2928 ((unsigned short)( a + 0.5f) << 15) |
2929 ((unsigned short)(31 * r + 0.5f) << 10) |
2930 ((unsigned short)(31 * g + 0.5f) << 5) |
2931 ((unsigned short)(31 * b + 0.5f) << 0);
2932 break;
2933 default: UNREACHABLE();
2934 }
2935 break;
2936 case GL_RGB:
2937 switch (type)
2938 {
2939 case GL_UNSIGNED_SHORT_5_6_5:
2940 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2941 ((unsigned short)(31 * b + 0.5f) << 0) |
2942 ((unsigned short)(63 * g + 0.5f) << 5) |
2943 ((unsigned short)(31 * r + 0.5f) << 11);
2944 break;
2945 case GL_UNSIGNED_BYTE:
2946 dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2947 dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2948 dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2949 break;
2950 default: UNREACHABLE();
2951 }
2952 break;
2953 default: UNREACHABLE();
2954 }
2955 }
2956 }
2957
2958 systemSurface->UnlockRect();
2959
2960 systemSurface->Release();
2961}
2962
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002963RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth)
2964{
2965 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
2966 IDirect3DSurface9 *surface = NULL;
2967 if (depth)
2968 {
2969 surface = swapChain9->getDepthStencil();
2970 }
2971 else
2972 {
2973 surface = swapChain9->getRenderTarget();
2974 }
2975
2976 RenderTarget9 *renderTarget = new RenderTarget9(this, surface);
2977
2978 return renderTarget;
2979}
2980
2981RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2982{
2983 RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples);
2984 return renderTarget;
2985}
2986
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002987ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002988{
2989 ShaderExecutable9 *executable = NULL;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002990
2991 switch (type)
2992 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002993 case rx::SHADER_VERTEX:
daniel@transgaming.com55318902012-11-28 20:58:58 +00002994 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002995 IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002996 if (vshader)
2997 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002998 executable = new ShaderExecutable9(function, length, vshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00002999 }
3000 }
3001 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003002 case rx::SHADER_PIXEL:
daniel@transgaming.com55318902012-11-28 20:58:58 +00003003 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003004 IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003005 if (pshader)
3006 {
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003007 executable = new ShaderExecutable9(function, length, pshader);
daniel@transgaming.com55318902012-11-28 20:58:58 +00003008 }
3009 }
3010 break;
3011 default:
3012 UNREACHABLE();
3013 break;
3014 }
3015
3016 return executable;
3017}
3018
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003019ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003020{
3021 const char *profile = NULL;
3022
3023 switch (type)
3024 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003025 case rx::SHADER_VERTEX:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003026 profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0";
3027 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003028 case rx::SHADER_PIXEL:
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003029 profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0";
3030 break;
3031 default:
3032 UNREACHABLE();
3033 return NULL;
3034 }
3035
daniel@transgaming.com25e16af2012-11-28 21:05:57 +00003036 ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, true);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003037 if (!binary)
3038 return NULL;
3039
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003040 ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com536dd6e2012-11-28 21:00:18 +00003041 binary->Release();
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003042
3043 return executable;
3044}
3045
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +00003046bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest)
3047{
3048 return mBlit->boxFilter(source, dest);
3049}
3050
daniel@transgaming.com2507f412012-10-31 18:46:48 +00003051D3DPOOL Renderer9::getTexturePool(DWORD usage) const
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003052{
3053 if (mD3d9Ex != NULL)
3054 {
3055 return D3DPOOL_DEFAULT;
3056 }
3057 else
3058 {
3059 if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
3060 {
3061 return D3DPOOL_MANAGED;
3062 }
3063 }
3064
3065 return D3DPOOL_DEFAULT;
3066}
3067
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003068bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged)
3069{
3070 if (source && dest)
3071 {
3072 HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003073
3074 if (fromManaged)
3075 {
3076 D3DSURFACE_DESC desc;
3077 source->GetDesc(&desc);
3078
3079 IDirect3DSurface9 *surf = 0;
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003080 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003081
3082 if (SUCCEEDED(result))
3083 {
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003084 Image9::copyLockableSurfaces(surf, source);
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003085 result = mDevice->UpdateSurface(surf, NULL, dest, NULL);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003086 surf->Release();
3087 }
3088 }
3089 else
3090 {
3091 endScene();
daniel@transgaming.com204677a2013-01-11 21:16:09 +00003092 result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +00003093 }
3094
3095 if (FAILED(result))
3096 {
3097 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
3098 return false;
3099 }
3100 }
3101
3102 return true;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +00003103}
3104
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003105Image *Renderer9::createImage()
3106{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003107 return new Image9();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003108}
3109
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003110void Renderer9::generateMipmap(Image *dest, Image *src)
3111{
daniel@transgaming.com4ba24062012-12-20 20:54:24 +00003112 Image9 *src9 = Image9::makeImage9(src);
3113 Image9 *dst9 = Image9::makeImage9(dest);
3114 Image9::generateMipmap(dst9, src9);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003115}
3116
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003117TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain)
3118{
3119 SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain);
3120 return new TextureStorage9_2D(this, swapChain9);
3121}
3122
3123TextureStorage *Renderer9::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3124{
3125 return new TextureStorage9_2D(this, levels, internalformat, usage, forceRenderable, width, height);
3126}
3127
3128TextureStorage *Renderer9::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3129{
3130 return new TextureStorage9_Cube(this, levels, internalformat, usage, forceRenderable, size);
3131}
3132
daniel@transgaming.com621ce052012-10-31 17:52:29 +00003133}