blob: 2c906b6d584a9dd84df44e1ea6036bbb4ab6e6ec [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002//
Geoff Langeeba6e12014-02-03 13:12:30 -05003// Copyright (c) 2012-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// Renderer11.cpp: Implements a back-end specific class for the D3D11 renderer.
9
daniel@transgaming.com5503fd02012-11-28 19:38:57 +000010#include "libGLESv2/main.h"
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +000011#include "common/utilities.h"
daniel@transgaming.com18adad02012-11-28 21:04:03 +000012#include "libGLESv2/Buffer.h"
daniel@transgaming.com53670042012-11-28 20:55:51 +000013#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com80fc3322012-11-28 21:02:13 +000014#include "libGLESv2/Framebuffer.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000015#include "libGLESv2/RenderBuffer.h"
Geoff Langd47e0fc2013-08-29 11:40:43 -040016#include "libGLESv2/renderer/d3d11/Renderer11.h"
17#include "libGLESv2/renderer/d3d11/RenderTarget11.h"
18#include "libGLESv2/renderer/d3d11/renderer11_utils.h"
19#include "libGLESv2/renderer/d3d11/formatutils11.h"
20#include "libGLESv2/renderer/d3d11/ShaderExecutable11.h"
21#include "libGLESv2/renderer/d3d11/SwapChain11.h"
22#include "libGLESv2/renderer/d3d11/Image11.h"
23#include "libGLESv2/renderer/d3d11/VertexBuffer11.h"
24#include "libGLESv2/renderer/d3d11/IndexBuffer11.h"
25#include "libGLESv2/renderer/d3d11/BufferStorage11.h"
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000026#include "libGLESv2/renderer/VertexDataManager.h"
27#include "libGLESv2/renderer/IndexDataManager.h"
Geoff Langd47e0fc2013-08-29 11:40:43 -040028#include "libGLESv2/renderer/d3d11/TextureStorage11.h"
29#include "libGLESv2/renderer/d3d11/Query11.h"
30#include "libGLESv2/renderer/d3d11/Fence11.h"
31#include "libGLESv2/renderer/d3d11/Blit11.h"
32#include "libGLESv2/renderer/d3d11/Clear11.h"
33#include "libGLESv2/renderer/d3d11/PixelTransfer11.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000034#include "libEGL/Display.h"
35
Geoff Lang94a90892014-02-18 17:14:19 -050036// Enable ANGLE_SKIP_DXGI_1_2_CHECK if there is not a possibility of using cross-process
37// HWNDs or the Windows 7 Platform Update (KB2670838) is expected to be installed.
38#ifndef ANGLE_SKIP_DXGI_1_2_CHECK
39#define ANGLE_SKIP_DXGI_1_2_CHECK 0
40#endif
41
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000042#ifdef _DEBUG
43// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
44// and conformance tests. to enable all warnings, remove this define.
45#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
46#endif
47
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000048namespace rx
49{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000050static const DXGI_FORMAT RenderTargetFormats[] =
51 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000052 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000053 DXGI_FORMAT_R8G8B8A8_UNORM
54 };
55
56static const DXGI_FORMAT DepthStencilFormats[] =
57 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000058 DXGI_FORMAT_UNKNOWN,
59 DXGI_FORMAT_D24_UNORM_S8_UINT,
60 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000061 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000062
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000063enum
64{
65 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
66};
67
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000068Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
69{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000070 mVertexDataManager = NULL;
71 mIndexDataManager = NULL;
72
daniel@transgaming.comc5114302012-12-20 21:11:36 +000073 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000074 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000075
Geoff Langb86b9792013-06-04 16:32:05 -040076 mBlit = NULL;
Jamie Madilla21eea32013-09-18 14:36:25 -040077 mPixelTransfer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000078
Geoff Langda507fe2013-08-20 12:01:42 -040079 mClear = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000080
daniel@transgaming.combdf787f2013-02-01 03:20:36 +000081 mSyncQuery = NULL;
82
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000083 mD3d11Module = NULL;
84 mDxgiModule = NULL;
85
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000086 mDeviceLost = false;
87
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +000088 mMaxSupportedSamples = 0;
89
daniel@transgaming.com25072f62012-11-28 19:31:32 +000090 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000091 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000092 mDxgiAdapter = NULL;
93 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +000094
95 mDriverConstantBufferVS = NULL;
96 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +000097
98 mBGRATextureSupport = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +000099
Jamie Madill6246dc82014-01-29 09:26:47 -0500100 mAppliedVertexShader = NULL;
101 mAppliedGeometryShader = NULL;
102 mAppliedPixelShader = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000103}
104
105Renderer11::~Renderer11()
106{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000107 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000108}
109
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000110Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
111{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000112 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000113 return static_cast<rx::Renderer11*>(renderer);
114}
115
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000116#ifndef __d3d11_1_h__
117#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
118#endif
119
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000120EGLint Renderer11::initialize()
121{
Geoff Langdad5ed32014-02-10 12:59:17 -0500122 if (!mCompiler.initialize())
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000123 {
124 return EGL_NOT_INITIALIZED;
125 }
126
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000127 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
128 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000129
130 if (mD3d11Module == NULL || mDxgiModule == NULL)
131 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000132 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000133 return EGL_NOT_INITIALIZED;
134 }
135
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000136 // create the D3D11 device
137 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000138 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000139
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000140 if (D3D11CreateDevice == NULL)
141 {
142 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
143 return EGL_NOT_INITIALIZED;
144 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000145
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000146 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000147 {
148 D3D_FEATURE_LEVEL_11_0,
149 D3D_FEATURE_LEVEL_10_1,
150 D3D_FEATURE_LEVEL_10_0,
151 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000152
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000153 HRESULT result = S_OK;
154
155#ifdef _DEBUG
156 result = D3D11CreateDevice(NULL,
157 D3D_DRIVER_TYPE_HARDWARE,
158 NULL,
159 D3D11_CREATE_DEVICE_DEBUG,
160 featureLevels,
161 ArraySize(featureLevels),
162 D3D11_SDK_VERSION,
163 &mDevice,
164 &mFeatureLevel,
165 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000166
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000167 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000168 {
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000169 ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n");
170 }
171
172 if (!mDevice || FAILED(result))
173#endif
174 {
175 result = D3D11CreateDevice(NULL,
176 D3D_DRIVER_TYPE_HARDWARE,
177 NULL,
178 0,
179 featureLevels,
180 ArraySize(featureLevels),
181 D3D11_SDK_VERSION,
182 &mDevice,
183 &mFeatureLevel,
184 &mDeviceContext);
185
186 if (!mDevice || FAILED(result))
187 {
188 ERR("Could not create D3D11 device - aborting!\n");
189 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
190 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000191 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000192
Geoff Lang94a90892014-02-18 17:14:19 -0500193#if !ANGLE_SKIP_DXGI_1_2_CHECK
194 // In order to create a swap chain for an HWND owned by another process, DXGI 1.2 is required.
195 // The easiest way to check is to query for a IDXGIDevice2.
196 bool requireDXGI1_2 = false;
197 HWND hwnd = WindowFromDC(mDc);
198 if (hwnd)
199 {
200 DWORD currentProcessId = GetCurrentProcessId();
201 DWORD wndProcessId;
202 GetWindowThreadProcessId(hwnd, &wndProcessId);
203 requireDXGI1_2 = (currentProcessId != wndProcessId);
204 }
205 else
206 {
207 requireDXGI1_2 = true;
208 }
209
210 if (requireDXGI1_2)
211 {
212 IDXGIDevice2 *dxgiDevice2 = NULL;
213 result = mDevice->QueryInterface(__uuidof(IDXGIDevice2), (void**)&dxgiDevice2);
214 if (FAILED(result))
215 {
216 ERR("DXGI 1.2 required to present to HWNDs owned by another process.\n");
217 return EGL_NOT_INITIALIZED;
218 }
219 SafeRelease(dxgiDevice2);
220 }
221#endif
222
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000223 IDXGIDevice *dxgiDevice = NULL;
224 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
225
226 if (FAILED(result))
227 {
228 ERR("Could not query DXGI device - aborting!\n");
229 return EGL_NOT_INITIALIZED;
230 }
231
232 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
233
234 if (FAILED(result))
235 {
236 ERR("Could not retrieve DXGI adapter - aborting!\n");
237 return EGL_NOT_INITIALIZED;
238 }
239
Geoff Langea228632013-07-30 15:17:12 -0400240 SafeRelease(dxgiDevice);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000241
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000242 mDxgiAdapter->GetDesc(&mAdapterDescription);
243 memset(mDescription, 0, sizeof(mDescription));
244 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
245
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000246 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
247
248 if (!mDxgiFactory || FAILED(result))
249 {
250 ERR("Could not create DXGI factory - aborting!\n");
251 return EGL_NOT_INITIALIZED;
252 }
253
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000254 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
255#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
256 ID3D11InfoQueue *infoQueue;
257 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
258
259 if (SUCCEEDED(result))
260 {
261 D3D11_MESSAGE_ID hideMessages[] =
262 {
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000263 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000264 };
265
266 D3D11_INFO_QUEUE_FILTER filter = {0};
267 filter.DenyList.NumIDs = ArraySize(hideMessages);
268 filter.DenyList.pIDList = hideMessages;
269
270 infoQueue->AddStorageFilterEntries(&filter);
Geoff Langea228632013-07-30 15:17:12 -0400271 SafeRelease(infoQueue);
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000272 }
273#endif
274
Geoff Lang61e49a52013-05-29 10:22:58 -0400275 mMaxSupportedSamples = 0;
276
277 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
278 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000279 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400280 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
281 mMultisampleSupportMap.insert(std::make_pair(*i, support));
282 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000283 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000284
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000285 initializeDevice();
286
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000287 // BGRA texture support is optional in feature levels 10 and 10_1
288 UINT formatSupport;
289 result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport);
290 if (FAILED(result))
291 {
292 ERR("Error checking BGRA format support: 0x%08X", result);
293 }
294 else
295 {
296 const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET);
297 mBGRATextureSupport = (formatSupport & flags) == flags;
298 }
299
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000300 // Check floating point texture support
301 static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE;
302 static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET;
303 static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE;
304
305 DXGI_FORMAT float16Formats[] =
306 {
307 DXGI_FORMAT_R16_FLOAT,
308 DXGI_FORMAT_R16G16_FLOAT,
309 DXGI_FORMAT_R16G16B16A16_FLOAT,
310 };
311
312 DXGI_FORMAT float32Formats[] =
313 {
314 DXGI_FORMAT_R32_FLOAT,
315 DXGI_FORMAT_R32G32_FLOAT,
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000316 DXGI_FORMAT_R32G32B32A32_FLOAT,
317 };
318
319 mFloat16TextureSupport = true;
320 mFloat16FilterSupport = true;
321 mFloat16RenderSupport = true;
322 for (unsigned int i = 0; i < ArraySize(float16Formats); i++)
323 {
324 if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport)))
325 {
326 mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
327 mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
328 mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
329 }
330 else
331 {
332 mFloat16TextureSupport = false;
333 mFloat16RenderSupport = false;
334 mFloat16FilterSupport = false;
335 }
336 }
337
338 mFloat32TextureSupport = true;
339 mFloat32FilterSupport = true;
340 mFloat32RenderSupport = true;
341 for (unsigned int i = 0; i < ArraySize(float32Formats); i++)
342 {
343 if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport)))
344 {
345 mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
346 mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
347 mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
348 }
349 else
350 {
351 mFloat32TextureSupport = false;
352 mFloat32FilterSupport = false;
353 mFloat32RenderSupport = false;
354 }
355 }
356
Geoff Lang632192d2013-10-04 13:40:46 -0400357 DXGI_FORMAT rgTextureFormats[] =
358 {
359 DXGI_FORMAT_R8_UNORM,
360 DXGI_FORMAT_R8G8_UNORM,
361 DXGI_FORMAT_R16_FLOAT,
362 DXGI_FORMAT_R16G16_FLOAT,
363 DXGI_FORMAT_R32_FLOAT,
364 DXGI_FORMAT_R32G32_FLOAT,
365 };
366
367 mRGTextureSupport = true;
368 for (unsigned int i = 0; i < ArraySize(rgTextureFormats); i++)
369 {
370 if (SUCCEEDED(mDevice->CheckFormatSupport(rgTextureFormats[i], &formatSupport)))
371 {
372 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
373 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
374 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
375 }
376 else
377 {
378 mRGTextureSupport = false;
379 }
380 }
381
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +0000382 // Check compressed texture support
383 const unsigned int requiredCompressedTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D;
384
385 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &formatSupport)))
386 {
387 mDXT1TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
388 }
389 else
390 {
391 mDXT1TextureSupport = false;
392 }
393
394 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport)))
395 {
396 mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
397 }
398 else
399 {
400 mDXT3TextureSupport = false;
401 }
402
403 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC5_UNORM, &formatSupport)))
404 {
405 mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
406 }
407 else
408 {
409 mDXT5TextureSupport = false;
410 }
411
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +0000412 // Check depth texture support
413 DXGI_FORMAT depthTextureFormats[] =
414 {
415 DXGI_FORMAT_D16_UNORM,
416 DXGI_FORMAT_D24_UNORM_S8_UINT,
417 };
418
419 static const unsigned int requiredDepthTextureFlags = D3D11_FORMAT_SUPPORT_DEPTH_STENCIL |
420 D3D11_FORMAT_SUPPORT_TEXTURE2D;
421
422 mDepthTextureSupport = true;
423 for (unsigned int i = 0; i < ArraySize(depthTextureFormats); i++)
424 {
425 if (SUCCEEDED(mDevice->CheckFormatSupport(depthTextureFormats[i], &formatSupport)))
426 {
427 mDepthTextureSupport = mDepthTextureSupport && ((formatSupport & requiredDepthTextureFlags) == requiredDepthTextureFlags);
428 }
429 else
430 {
431 mDepthTextureSupport = false;
432 }
433 }
434
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000435 return EGL_SUCCESS;
436}
437
438// do any one-time device initialization
439// NOTE: this is also needed after a device lost/reset
440// to reset the scene status and ensure the default states are reset.
441void Renderer11::initializeDevice()
442{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000443 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000444 mInputLayoutCache.initialize(mDevice, mDeviceContext);
445
446 ASSERT(!mVertexDataManager && !mIndexDataManager);
447 mVertexDataManager = new VertexDataManager(this);
448 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000449
Geoff Langb86b9792013-06-04 16:32:05 -0400450 ASSERT(!mBlit);
451 mBlit = new Blit11(this);
452
Geoff Langda507fe2013-08-20 12:01:42 -0400453 ASSERT(!mClear);
454 mClear = new Clear11(this);
455
Jamie Madilla21eea32013-09-18 14:36:25 -0400456 ASSERT(!mPixelTransfer);
457 mPixelTransfer = new PixelTransfer11(this);
458
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000459 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000460}
461
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000462int Renderer11::generateConfigs(ConfigDesc **configDescList)
463{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000464 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
465 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000466 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
467 int numConfigs = 0;
468
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000469 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000470 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000471 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000472 {
473 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
474
475 UINT formatSupport = 0;
476 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000477
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000478 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
479 {
480 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
481
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000482 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000483
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000484 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
485 {
Geoff Lang5c9a29a2014-02-11 10:26:24 -0500486 UINT depthStencilSupport = 0;
487 result = mDevice->CheckFormatSupport(depthStencilFormat, &depthStencilSupport);
488 depthStencilFormatOK = SUCCEEDED(result) && (depthStencilSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000489 }
490
491 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000492 {
Jamie Madilld6cb2442013-07-10 15:13:38 -0400493 // FIXME: parse types from context version
494 ASSERT(d3d11_gl::GetInternalFormat(renderTargetFormat, 2) == d3d11_gl::GetInternalFormat(renderTargetFormat, 3));
495 ASSERT(d3d11_gl::GetInternalFormat(depthStencilFormat, 2) == d3d11_gl::GetInternalFormat(depthStencilFormat, 3));
496
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000497 ConfigDesc newConfig;
Jamie Madilld6cb2442013-07-10 15:13:38 -0400498 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat, getCurrentClientVersion());
499 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat, getCurrentClientVersion());
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000500 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
501 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000502 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000503
504 (*configDescList)[numConfigs++] = newConfig;
505 }
506 }
507 }
508 }
509
510 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000511}
512
513void Renderer11::deleteConfigs(ConfigDesc *configDescList)
514{
515 delete [] (configDescList);
516}
517
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000518void Renderer11::sync(bool block)
519{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000520 if (block)
521 {
522 HRESULT result;
523
524 if (!mSyncQuery)
525 {
526 D3D11_QUERY_DESC queryDesc;
527 queryDesc.Query = D3D11_QUERY_EVENT;
528 queryDesc.MiscFlags = 0;
529
530 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
531 ASSERT(SUCCEEDED(result));
532 }
533
534 mDeviceContext->End(mSyncQuery);
535 mDeviceContext->Flush();
536
537 do
538 {
539 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
540
541 // Keep polling, but allow other threads to do something useful first
542 Sleep(0);
543
544 if (testDeviceLost(true))
545 {
546 return;
547 }
548 }
549 while (result == S_FALSE);
550 }
551 else
552 {
553 mDeviceContext->Flush();
554 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000555}
556
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000557SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
558{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000559 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000560}
561
Geoff Lange2e0ce02013-09-17 17:05:08 -0400562void Renderer11::generateSwizzle(gl::Texture *texture)
563{
Geoff Lang42477a42013-09-17 17:07:02 -0400564 if (texture)
565 {
566 TextureStorageInterface *texStorage = texture->getNativeTexture();
567 if (texStorage)
568 {
569 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
570
571 storage11->generateSwizzles(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
572 texture->getSwizzleAlpha());
573 }
574 }
Geoff Lange2e0ce02013-09-17 17:05:08 -0400575}
576
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000577void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
578{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000579 if (type == gl::SAMPLER_PIXEL)
580 {
581 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
582 {
583 ERR("Pixel shader sampler index %i is not valid.", index);
584 return;
585 }
586
587 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
588 {
589 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
590
591 if (!dxSamplerState)
592 {
593 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
594 "sampler state for pixel shaders at slot %i.", index);
595 }
596
597 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
598
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000599 mCurPixelSamplerStates[index] = samplerState;
600 }
601
602 mForceSetPixelSamplerStates[index] = false;
603 }
604 else if (type == gl::SAMPLER_VERTEX)
605 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000606 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000607 {
608 ERR("Vertex shader sampler index %i is not valid.", index);
609 return;
610 }
611
612 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
613 {
614 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
615
616 if (!dxSamplerState)
617 {
618 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
619 "sampler state for vertex shaders at slot %i.", index);
620 }
621
622 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
623
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000624 mCurVertexSamplerStates[index] = samplerState;
625 }
626
627 mForceSetVertexSamplerStates[index] = false;
628 }
629 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000630}
631
632void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
633{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000634 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000635 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000636
637 if (texture)
638 {
639 TextureStorageInterface *texStorage = texture->getNativeTexture();
640 if (texStorage)
641 {
642 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
Geoff Lang644bbf22013-09-17 17:02:43 -0400643 textureSRV = storage11->getSRV(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
644 texture->getSwizzleAlpha());
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000645 }
646
647 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
648 // missing the shader resource view
649 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000650
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000651 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000652 }
653
654 if (type == gl::SAMPLER_PIXEL)
655 {
656 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
657 {
658 ERR("Pixel shader sampler index %i is not valid.", index);
659 return;
660 }
661
Geoff Lang91382e52014-01-07 16:16:30 -0500662 if (forceSetTexture || mCurPixelSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000663 {
664 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
665 }
666
Geoff Lang91382e52014-01-07 16:16:30 -0500667 mCurPixelSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000668 }
669 else if (type == gl::SAMPLER_VERTEX)
670 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000671 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000672 {
673 ERR("Vertex shader sampler index %i is not valid.", index);
674 return;
675 }
676
Geoff Lang91382e52014-01-07 16:16:30 -0500677 if (forceSetTexture || mCurVertexSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000678 {
679 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
680 }
681
Geoff Lang91382e52014-01-07 16:16:30 -0500682 mCurVertexSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000683 }
684 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000685}
686
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000687bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
688{
689 // convert buffers to ID3D11Buffer*
690 ID3D11Buffer *vertexConstantBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = { NULL };
691 ID3D11Buffer *pixelConstantBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = { NULL };
692
693 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
694 {
695 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
696 if (uniformBuffer)
697 {
698 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500699 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000700
701 if (!constantBuffer)
702 {
703 return false;
704 }
705
Geoff Langc6354ee2013-07-22 10:40:07 -0400706 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
707 {
708 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
709 1, &constantBuffer);
710 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
711 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000712 }
713 }
714
715 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
716 {
717 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
718 if (uniformBuffer)
719 {
720 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500721 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000722
723 if (!constantBuffer)
724 {
725 return false;
726 }
727
Geoff Langc6354ee2013-07-22 10:40:07 -0400728 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
729 {
730 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
731 1, &constantBuffer);
732 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
733 }
734
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000735 pixelConstantBuffers[uniformBufferIndex] = constantBuffer;
736 }
737 }
738
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000739 return true;
740}
741
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000742void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000743{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000744 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000745 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000746 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
747 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000748 if (!dxRasterState)
749 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000750 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000751 "rasterizer state.");
752 }
753
754 mDeviceContext->RSSetState(dxRasterState);
755
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000756 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000757 }
758
759 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000760}
761
Geoff Langc142e9d2013-09-30 15:19:47 -0400762void Renderer11::setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000763 unsigned int sampleMask)
764{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000765 if (mForceSetBlendState ||
766 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400767 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000768 sampleMask != mCurSampleMask)
769 {
Geoff Langc142e9d2013-09-30 15:19:47 -0400770 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(framebuffer, blendState);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000771 if (!dxBlendState)
772 {
773 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
774 "blend state.");
775 }
776
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000777 float blendColors[4] = {0.0f};
778 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
779 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
780 {
781 blendColors[0] = blendColor.red;
782 blendColors[1] = blendColor.green;
783 blendColors[2] = blendColor.blue;
784 blendColors[3] = blendColor.alpha;
785 }
786 else
787 {
788 blendColors[0] = blendColor.alpha;
789 blendColors[1] = blendColor.alpha;
790 blendColors[2] = blendColor.alpha;
791 blendColors[3] = blendColor.alpha;
792 }
793
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000794 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
795
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000796 mCurBlendState = blendState;
797 mCurBlendColor = blendColor;
798 mCurSampleMask = sampleMask;
799 }
800
801 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000802}
803
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000804void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000805 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000806{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000807 if (mForceSetDepthStencilState ||
808 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
809 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
810 {
811 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
812 stencilRef != stencilBackRef ||
813 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
814 {
815 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
816 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000817 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000818 }
819
820 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
821 if (!dxDepthStencilState)
822 {
823 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
824 "setting the default depth stencil state.");
825 }
826
Jamie Madillec91cd32014-01-21 16:38:12 -0500827 // Max D3D11 stencil reference value is 0xFF, corresponding to the max 8 bits in a stencil buffer
828 // GL specifies we should clamp the ref value to the nearest bit depth when doing stencil ops
829 META_ASSERT(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF);
830 META_ASSERT(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF);
Geoff Lang0bf3a982014-02-11 09:40:48 -0500831 UINT dxStencilRef = std::min<UINT>(stencilRef, 0xFFu);
Jamie Madillec91cd32014-01-21 16:38:12 -0500832
Geoff Lang0bf3a982014-02-11 09:40:48 -0500833 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, dxStencilRef);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000834
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000835 mCurDepthStencilState = depthStencilState;
836 mCurStencilRef = stencilRef;
837 mCurStencilBackRef = stencilBackRef;
838 }
839
840 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000841}
842
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000843void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000844{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000845 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
846 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000847 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000848 if (enabled)
849 {
850 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000851 rect.left = std::max(0, scissor.x);
852 rect.top = std::max(0, scissor.y);
853 rect.right = scissor.x + std::max(0, scissor.width);
854 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000855
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000856 mDeviceContext->RSSetScissorRects(1, &rect);
857 }
858
859 if (enabled != mScissorEnabled)
860 {
861 mForceSetRasterState = true;
862 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000863
864 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000865 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000866 }
867
868 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000869}
870
daniel@transgaming.com12985182012-12-20 20:56:31 +0000871bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000872 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000873{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000874 gl::Rectangle actualViewport = viewport;
875 float actualZNear = gl::clamp01(zNear);
876 float actualZFar = gl::clamp01(zFar);
877 if (ignoreViewport)
878 {
879 actualViewport.x = 0;
880 actualViewport.y = 0;
881 actualViewport.width = mRenderTargetDesc.width;
882 actualViewport.height = mRenderTargetDesc.height;
883 actualZNear = 0.0f;
884 actualZFar = 1.0f;
885 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000886
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000887 // Get D3D viewport bounds, which depends on the feature level
888 const Range& viewportBounds = getViewportBounds();
889
890 // Clamp width and height first to the gl maximum, then clamp further if we extend past the D3D maximum bounds
daniel@transgaming.com53670042012-11-28 20:55:51 +0000891 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000892 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
893 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
894 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
895 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
896 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
897 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000898 dxViewport.MinDepth = actualZNear;
899 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000900
901 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
902 {
903 return false; // Nothing to render
904 }
905
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000906 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
907 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000908
daniel@transgaming.com53670042012-11-28 20:55:51 +0000909 if (viewportChanged)
910 {
911 mDeviceContext->RSSetViewports(1, &dxViewport);
912
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000913 mCurViewport = actualViewport;
914 mCurNear = actualZNear;
915 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000916
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000917 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
918 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
919 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
920 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000921
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000922 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
923 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000924
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000925 mVertexConstants.depthRange[0] = actualZNear;
926 mVertexConstants.depthRange[1] = actualZFar;
927 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000928
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000929 mPixelConstants.depthRange[0] = actualZNear;
930 mPixelConstants.depthRange[1] = actualZFar;
931 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000932 }
933
934 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000935 return true;
936}
937
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000938bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
939{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000940 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000941
Geoff Lang57e713e2013-07-31 17:01:58 -0400942 GLsizei minCount = 0;
943
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000944 switch (mode)
945 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400946 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
947 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
948 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
949 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
950 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
951 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000952 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400953 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000954 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000955 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000956 }
957
Geoff Lang4c095862013-07-22 10:43:36 -0400958 if (primitiveTopology != mCurrentPrimitiveTopology)
959 {
960 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
961 mCurrentPrimitiveTopology = primitiveTopology;
962 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000963
Geoff Lang57e713e2013-07-31 17:01:58 -0400964 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000965}
966
967bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000968{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000969 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000970 // Also extract the render target dimensions and view
971 unsigned int renderTargetWidth = 0;
972 unsigned int renderTargetHeight = 0;
973 GLenum renderTargetFormat = 0;
974 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
975 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
976 bool missingColorRenderTarget = true;
977
978 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000979 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000980 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
981
982 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000983 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000984 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
985 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
986
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000987 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000988
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000989 if (!colorbuffer)
990 {
991 ERR("render target pointer unexpectedly null.");
992 return false;
993 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000994
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000995 // check for zero-sized default framebuffer, which is a special case.
996 // in this case we do not wish to modify any state and just silently return false.
997 // this will not report any gl error but will cause the calling method to return.
998 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
999 {
1000 return false;
1001 }
1002
1003 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
1004
1005 // Extract the render target dimensions and view
1006 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
1007 if (!renderTarget)
1008 {
1009 ERR("render target pointer unexpectedly null.");
1010 return false;
1011 }
1012
1013 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
1014 if (!framebufferRTVs[colorAttachment])
1015 {
1016 ERR("render target view pointer unexpectedly null.");
1017 return false;
1018 }
1019
1020 if (missingColorRenderTarget)
1021 {
1022 renderTargetWidth = colorbuffer->getWidth();
1023 renderTargetHeight = colorbuffer->getHeight();
1024 renderTargetFormat = colorbuffer->getActualFormat();
1025 missingColorRenderTarget = false;
1026 }
Jamie Madillba597af2013-10-22 13:12:15 -04001027
Geoff Lang91382e52014-01-07 16:16:30 -05001028 // TODO: Detect if this color buffer is already bound as a texture and unbind it first to prevent
1029 // D3D11 warnings.
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001030 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001031 }
1032
1033 // Get the depth stencil render buffer and serials
1034 gl::Renderbuffer *depthStencil = NULL;
1035 unsigned int depthbufferSerial = 0;
1036 unsigned int stencilbufferSerial = 0;
1037 if (framebuffer->getDepthbufferType() != GL_NONE)
1038 {
1039 depthStencil = framebuffer->getDepthbuffer();
1040 if (!depthStencil)
1041 {
1042 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001043 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001044 return false;
1045 }
1046
1047 depthbufferSerial = depthStencil->getSerial();
1048 }
1049 else if (framebuffer->getStencilbufferType() != GL_NONE)
1050 {
1051 depthStencil = framebuffer->getStencilbuffer();
1052 if (!depthStencil)
1053 {
1054 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001055 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001056 return false;
1057 }
1058
1059 stencilbufferSerial = depthStencil->getSerial();
1060 }
1061
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001062 // Extract the depth stencil sizes and view
1063 unsigned int depthSize = 0;
1064 unsigned int stencilSize = 0;
1065 ID3D11DepthStencilView* framebufferDSV = NULL;
1066 if (depthStencil)
1067 {
1068 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
1069 if (!depthStencilRenderTarget)
1070 {
1071 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001072 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001073 return false;
1074 }
1075
1076 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
1077 if (!framebufferDSV)
1078 {
1079 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001080 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001081 return false;
1082 }
1083
1084 // If there is no render buffer, the width, height and format values come from
1085 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001086 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001087 {
1088 renderTargetWidth = depthStencil->getWidth();
1089 renderTargetHeight = depthStencil->getHeight();
1090 renderTargetFormat = depthStencil->getActualFormat();
1091 }
1092
1093 depthSize = depthStencil->getDepthSize();
1094 stencilSize = depthStencil->getStencilSize();
1095 }
1096
1097 // Apply the render target and depth stencil
1098 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001099 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001100 depthbufferSerial != mAppliedDepthbufferSerial ||
1101 stencilbufferSerial != mAppliedStencilbufferSerial)
1102 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001103 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001104
1105 mRenderTargetDesc.width = renderTargetWidth;
1106 mRenderTargetDesc.height = renderTargetHeight;
1107 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001108 mForceSetViewport = true;
1109 mForceSetScissor = true;
Geoff Langc142e9d2013-09-30 15:19:47 -04001110 mForceSetBlendState = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001111
1112 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1113 {
1114 mCurDepthSize = depthSize;
1115 mForceSetRasterState = true;
1116 }
1117
1118 mCurStencilSize = stencilSize;
1119
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001120 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1121 {
1122 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
1123 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001124 mAppliedDepthbufferSerial = depthbufferSerial;
1125 mAppliedStencilbufferSerial = stencilbufferSerial;
1126 mRenderTargetDescInitialized = true;
1127 mDepthStencilInitialized = true;
1128 }
1129
Geoff Lang42477a42013-09-17 17:07:02 -04001130 invalidateFramebufferSwizzles(framebuffer);
1131
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001132 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001133}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001134
Jamie Madill57a89722013-07-02 11:57:03 -04001135GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001136 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001137{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001138 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001139 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001140 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001141 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001142 return err;
1143 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001144
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001145 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001146}
1147
daniel@transgaming.com31240482012-11-28 21:06:41 +00001148GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001149{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001150 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001151
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001152 if (err == GL_NO_ERROR)
1153 {
Geoff Lang7840b172014-03-13 11:20:44 -04001154 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1155
1156 ID3D11Buffer *buffer = NULL;
1157 DXGI_FORMAT bufferFormat = indexBuffer->getIndexFormat();
1158
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001159 if (indexInfo->storage)
1160 {
Geoff Lang7840b172014-03-13 11:20:44 -04001161 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1162 buffer = storage->getBuffer(BUFFER_USAGE_INDEX);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001163 }
Geoff Lang7840b172014-03-13 11:20:44 -04001164 else
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001165 {
Geoff Lang7840b172014-03-13 11:20:44 -04001166 buffer = indexBuffer->getBuffer();
1167 }
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001168
Geoff Lang7840b172014-03-13 11:20:44 -04001169 if (buffer != mAppliedIB || bufferFormat != mAppliedIBFormat || indexInfo->startOffset != mAppliedIBOffset)
1170 {
1171 mDeviceContext->IASetIndexBuffer(buffer, bufferFormat, indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001172
Geoff Lang7840b172014-03-13 11:20:44 -04001173 mAppliedIB = buffer;
1174 mAppliedIBFormat = bufferFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001175 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001176 }
1177 }
1178
1179 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001180}
1181
Geoff Langeeba6e12014-02-03 13:12:30 -05001182void Renderer11::applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[])
1183{
1184 ID3D11Buffer* d3dBuffers[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1185 UINT d3dOffsets[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1186 bool requiresUpdate = false;
1187 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1188 {
1189 if (transformFeedbackBuffers[i])
1190 {
1191 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(transformFeedbackBuffers[i]->getStorage());
1192 ID3D11Buffer *buffer = storage->getBuffer(BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK);
1193
1194 d3dBuffers[i] = buffer;
1195 d3dOffsets[i] = (mAppliedTFBuffers[i] != buffer) ? static_cast<UINT>(offsets[i]) : -1;
1196 }
1197 else
1198 {
1199 d3dBuffers[i] = NULL;
1200 d3dOffsets[i] = 0;
1201 }
1202
1203 if (d3dBuffers[i] != mAppliedTFBuffers[i] || offsets[i] != mAppliedTFOffsets[i])
1204 {
1205 requiresUpdate = true;
1206 }
1207 }
1208
1209 if (requiresUpdate)
1210 {
1211 mDeviceContext->SOSetTargets(ArraySize(d3dBuffers), d3dBuffers, d3dOffsets);
1212 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1213 {
1214 mAppliedTFBuffers[i] = d3dBuffers[i];
1215 mAppliedTFOffsets[i] = offsets[i];
1216 }
1217 }
1218}
1219
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001220void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1221{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001222 if (mode == GL_LINE_LOOP)
1223 {
1224 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1225 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001226 else if (mode == GL_TRIANGLE_FAN)
1227 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001228 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001229 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001230 else if (instances > 0)
1231 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001232 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001233 }
1234 else
1235 {
1236 mDeviceContext->Draw(count, 0);
1237 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001238}
1239
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001240void Renderer11::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 +00001241{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001242 if (mode == GL_LINE_LOOP)
1243 {
1244 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1245 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001246 else if (mode == GL_TRIANGLE_FAN)
1247 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001248 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1249 }
1250 else if (instances > 0)
1251 {
1252 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001253 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001254 else
1255 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001256 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001257 }
1258}
1259
1260void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1261{
1262 // Get the raw indices for an indexed draw
1263 if (type != GL_NONE && elementArrayBuffer)
1264 {
1265 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001266 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001267 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001268 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001269 }
1270
1271 if (!mLineLoopIB)
1272 {
1273 mLineLoopIB = new StreamingIndexBufferInterface(this);
1274 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1275 {
1276 delete mLineLoopIB;
1277 mLineLoopIB = NULL;
1278
1279 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001280 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001281 }
1282 }
1283
Geoff Lang57e713e2013-07-31 17:01:58 -04001284 // Checked by Renderer11::applyPrimitiveType
1285 ASSERT(count >= 0);
1286
1287 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001288 {
1289 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1290 return gl::error(GL_OUT_OF_MEMORY);
1291 }
1292
Geoff Lang57e713e2013-07-31 17:01:58 -04001293 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001294 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1295 {
1296 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001297 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001298 }
1299
1300 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001301 unsigned int offset;
1302 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001303 {
1304 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001305 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001306 }
1307
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001308 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001309 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001310
1311 switch (type)
1312 {
1313 case GL_NONE: // Non-indexed draw
1314 for (int i = 0; i < count; i++)
1315 {
1316 data[i] = i;
1317 }
1318 data[count] = 0;
1319 break;
1320 case GL_UNSIGNED_BYTE:
1321 for (int i = 0; i < count; i++)
1322 {
1323 data[i] = static_cast<const GLubyte*>(indices)[i];
1324 }
1325 data[count] = static_cast<const GLubyte*>(indices)[0];
1326 break;
1327 case GL_UNSIGNED_SHORT:
1328 for (int i = 0; i < count; i++)
1329 {
1330 data[i] = static_cast<const GLushort*>(indices)[i];
1331 }
1332 data[count] = static_cast<const GLushort*>(indices)[0];
1333 break;
1334 case GL_UNSIGNED_INT:
1335 for (int i = 0; i < count; i++)
1336 {
1337 data[i] = static_cast<const GLuint*>(indices)[i];
1338 }
1339 data[count] = static_cast<const GLuint*>(indices)[0];
1340 break;
1341 default: UNREACHABLE();
1342 }
1343
1344 if (!mLineLoopIB->unmapBuffer())
1345 {
1346 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001347 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001348 }
1349
Geoff Lang7840b172014-03-13 11:20:44 -04001350 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1351 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1352 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001353
Geoff Lang7840b172014-03-13 11:20:44 -04001354 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1355 {
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001356 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001357 mAppliedIB = d3dIndexBuffer;
1358 mAppliedIBFormat = indexFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001359 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001360 }
1361
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001362 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001363}
1364
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001365void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001366{
1367 // Get the raw indices for an indexed draw
1368 if (type != GL_NONE && elementArrayBuffer)
1369 {
1370 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001371 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001372 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001373 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001374 }
1375
1376 if (!mTriangleFanIB)
1377 {
1378 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1379 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1380 {
1381 delete mTriangleFanIB;
1382 mTriangleFanIB = NULL;
1383
1384 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001385 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001386 }
1387 }
1388
Geoff Lang57e713e2013-07-31 17:01:58 -04001389 // Checked by Renderer11::applyPrimitiveType
1390 ASSERT(count >= 3);
1391
Geoff Langeadfd572013-07-09 15:55:07 -04001392 const unsigned int numTris = count - 2;
1393
Geoff Lang57e713e2013-07-31 17:01:58 -04001394 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001395 {
1396 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1397 return gl::error(GL_OUT_OF_MEMORY);
1398 }
1399
1400 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001401 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1402 {
1403 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001404 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001405 }
1406
1407 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001408 unsigned int offset;
1409 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001410 {
1411 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001412 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001413 }
1414
1415 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001416 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001417
1418 switch (type)
1419 {
1420 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001421 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001422 {
1423 data[i*3 + 0] = 0;
1424 data[i*3 + 1] = i + 1;
1425 data[i*3 + 2] = i + 2;
1426 }
1427 break;
1428 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001429 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001430 {
1431 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1432 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1433 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1434 }
1435 break;
1436 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001437 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001438 {
1439 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1440 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1441 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1442 }
1443 break;
1444 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001445 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001446 {
1447 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1448 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1449 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1450 }
1451 break;
1452 default: UNREACHABLE();
1453 }
1454
1455 if (!mTriangleFanIB->unmapBuffer())
1456 {
1457 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001458 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001459 }
1460
Geoff Lang7840b172014-03-13 11:20:44 -04001461 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1462 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1463 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001464
Geoff Lang7840b172014-03-13 11:20:44 -04001465 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1466 {
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001467 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001468 mAppliedIB = d3dIndexBuffer;
1469 mAppliedIBFormat = indexFormat;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001470 mAppliedIBOffset = indexBufferOffset;
1471 }
1472
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001473 if (instances > 0)
1474 {
1475 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1476 }
1477 else
1478 {
1479 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1480 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001481}
1482
Jamie Madillc5a83002014-02-14 16:41:25 -05001483void Renderer11::applyShaders(gl::ProgramBinary *programBinary, bool rasterizerDiscard, const gl::VertexFormat inputLayout[])
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001484{
Jamie Madillc5a83002014-02-14 16:41:25 -05001485 ShaderExecutable *vertexExe = programBinary->getVertexExecutableForInputLayout(inputLayout);
Jamie Madill6246dc82014-01-29 09:26:47 -05001486 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
1487 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001488
Jamie Madill6246dc82014-01-29 09:26:47 -05001489 ID3D11VertexShader *vertexShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader() : NULL);
1490 ID3D11PixelShader *pixelShader = (pixelExe ? ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader() : NULL);
1491 ID3D11GeometryShader *geometryShader = (geometryExe ? ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader() : NULL);
1492
1493 // Skip GS if we aren't drawing points
1494 if (!mCurRasterState.pointDrawMode)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001495 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001496 geometryShader = NULL;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001497 }
1498
Geoff Lang0550d032014-01-30 11:29:07 -05001499 // Skip pixel shader if we're doing rasterizer discard.
1500 if (rasterizerDiscard)
1501 {
1502 pixelShader = NULL;
1503 }
1504
Jamie Madill6246dc82014-01-29 09:26:47 -05001505 bool dirtyUniforms = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001506
Jamie Madill6246dc82014-01-29 09:26:47 -05001507 if (vertexShader != mAppliedVertexShader)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001508 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001509 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1510 mAppliedVertexShader = vertexShader;
1511 dirtyUniforms = true;
1512 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001513
Jamie Madill6246dc82014-01-29 09:26:47 -05001514 if (geometryShader != mAppliedGeometryShader)
1515 {
1516 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1517 mAppliedGeometryShader = geometryShader;
1518 dirtyUniforms = true;
1519 }
1520
1521 if (pixelShader != mAppliedPixelShader)
1522 {
1523 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1524 mAppliedPixelShader = pixelShader;
1525 dirtyUniforms = true;
1526 }
1527
1528 if (dirtyUniforms)
1529 {
1530 programBinary->dirtyAllUniforms();
daniel@transgaming.come4991412012-12-20 20:55:34 +00001531 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001532}
1533
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001534void Renderer11::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001535{
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001536 const gl::UniformArray &uniformArray = programBinary.getUniforms();
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001537
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001538 unsigned int totalRegisterCountVS = 0;
1539 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001540
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001541 bool vertexUniformsDirty = false;
1542 bool pixelUniformsDirty = false;
1543
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001544 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001545 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001546 const gl::Uniform &uniform = *uniformArray[uniformIndex];
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001547
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001548 if (uniform.isReferencedByVertexShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001549 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001550 totalRegisterCountVS += uniform.registerCount;
1551 vertexUniformsDirty = (vertexUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001552 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001553
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001554 if (uniform.isReferencedByFragmentShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001555 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001556 totalRegisterCountPS += uniform.registerCount;
1557 pixelUniformsDirty = (pixelUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001558 }
1559 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001560
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001561 const UniformStorage11 *vertexUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getVertexUniformStorage());
1562 const UniformStorage11 *fragmentUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getFragmentUniformStorage());
1563 ASSERT(vertexUniformStorage);
1564 ASSERT(fragmentUniformStorage);
1565
1566 ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
1567 ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001568
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001569 float (*mapVS)[4] = NULL;
1570 float (*mapPS)[4] = NULL;
1571
Shannon Woods5ab33c82013-06-26 15:31:09 -04001572 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1573 {
1574 D3D11_MAPPED_SUBRESOURCE map = {0};
1575 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1576 ASSERT(SUCCEEDED(result));
1577 mapVS = (float(*)[4])map.pData;
1578 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001579
Shannon Woods5ab33c82013-06-26 15:31:09 -04001580 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1581 {
1582 D3D11_MAPPED_SUBRESOURCE map = {0};
1583 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1584 ASSERT(SUCCEEDED(result));
1585 mapPS = (float(*)[4])map.pData;
1586 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001587
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001588 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001589 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001590 gl::Uniform *uniform = uniformArray[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001591
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001592 if (!uniform->isSampler())
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001593 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001594 unsigned int componentCount = (4 - uniform->registerElement);
1595
Jamie Madill71cc91f2013-09-18 12:51:22 -04001596 // we assume that uniforms from structs are arranged in struct order in our uniforms list. otherwise we would
Jamie Madill5b085dc2013-08-30 13:21:11 -04001597 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001598
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001599 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001600 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001601 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001602 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001603
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001604 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001605 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001606 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001607 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001608 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001609 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001610
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001611 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001612 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001613 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001614 }
1615
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001616 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001617 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001618 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001619 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001620
1621 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1622 {
1623 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1624 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1625 }
1626
1627 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1628 {
1629 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1630 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1631 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001632
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001633 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001634 if (!mDriverConstantBufferVS)
1635 {
1636 D3D11_BUFFER_DESC constantBufferDescription = {0};
1637 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1638 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1639 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1640 constantBufferDescription.CPUAccessFlags = 0;
1641 constantBufferDescription.MiscFlags = 0;
1642 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001643
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001644 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001645 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001646
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001647 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1648 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001649
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001650 if (!mDriverConstantBufferPS)
1651 {
1652 D3D11_BUFFER_DESC constantBufferDescription = {0};
1653 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1654 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1655 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1656 constantBufferDescription.CPUAccessFlags = 0;
1657 constantBufferDescription.MiscFlags = 0;
1658 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001659
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001660 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001661 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001662
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001663 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1664 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001665
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001666 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1667 {
1668 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1669 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1670 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001671
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001672 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1673 {
1674 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1675 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1676 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001677
1678 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001679 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1680 {
1681 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1682 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1683 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001684}
1685
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001686void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001687{
Geoff Langda507fe2013-08-20 12:01:42 -04001688 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001689 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001690}
1691
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001692void Renderer11::markAllStateDirty()
1693{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001694 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1695 {
1696 mAppliedRenderTargetSerials[rtIndex] = 0;
1697 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001698 mAppliedDepthbufferSerial = 0;
1699 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001700 mDepthStencilInitialized = false;
1701 mRenderTargetDescInitialized = false;
1702
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001703 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001704 {
1705 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001706 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001707 }
1708 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1709 {
1710 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001711 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001712 }
1713
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001714 mForceSetBlendState = true;
1715 mForceSetRasterState = true;
1716 mForceSetDepthStencilState = true;
1717 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001718 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001719
Geoff Lang7840b172014-03-13 11:20:44 -04001720 mAppliedIB = NULL;
1721 mAppliedIBFormat = DXGI_FORMAT_UNKNOWN;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001722 mAppliedIBOffset = 0;
1723
Jamie Madill6246dc82014-01-29 09:26:47 -05001724 mAppliedVertexShader = NULL;
1725 mAppliedGeometryShader = NULL;
1726 mAppliedPixelShader = NULL;
Geoff Langeeba6e12014-02-03 13:12:30 -05001727
1728 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1729 {
1730 mAppliedTFBuffers[i] = NULL;
1731 mAppliedTFOffsets[i] = 0;
1732 }
1733
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001734 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1735 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001736
1737 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001738
1739 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1740 {
1741 mCurrentConstantBufferVS[i] = -1;
1742 mCurrentConstantBufferPS[i] = -1;
1743 }
1744
1745 mCurrentVertexConstantBuffer = NULL;
1746 mCurrentPixelConstantBuffer = NULL;
1747 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001748
1749 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001750}
1751
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001752void Renderer11::releaseDeviceResources()
1753{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001754 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001755 mInputLayoutCache.clear();
1756
Geoff Langea228632013-07-30 15:17:12 -04001757 SafeDelete(mVertexDataManager);
1758 SafeDelete(mIndexDataManager);
1759 SafeDelete(mLineLoopIB);
1760 SafeDelete(mTriangleFanIB);
1761 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001762 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001763 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001764
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001765 SafeRelease(mDriverConstantBufferVS);
1766 SafeRelease(mDriverConstantBufferPS);
1767 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001768}
1769
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001770void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001771{
1772 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001773 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001774}
1775
1776bool Renderer11::isDeviceLost()
1777{
1778 return mDeviceLost;
1779}
1780
1781// set notify to true to broadcast a message to all contexts of the device loss
1782bool Renderer11::testDeviceLost(bool notify)
1783{
1784 bool isLost = false;
1785
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001786 // GetRemovedReason is used to test if the device is removed
1787 HRESULT result = mDevice->GetDeviceRemovedReason();
1788 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001789
1790 if (isLost)
1791 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001792 // Log error if this is a new device lost event
1793 if (mDeviceLost == false)
1794 {
1795 ERR("The D3D11 device was removed: 0x%08X", result);
1796 }
1797
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001798 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001799 // we'll probably get this done again by notifyDeviceLost
1800 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001801 // Note that we don't want to clear the device loss status here
1802 // -- this needs to be done by resetDevice
1803 mDeviceLost = true;
1804 if (notify)
1805 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001806 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001807 }
1808 }
1809
1810 return isLost;
1811}
1812
1813bool Renderer11::testDeviceResettable()
1814{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001815 // determine if the device is resettable by creating a dummy device
1816 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001817
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001818 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001819 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001820 return false;
1821 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001822
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001823 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001824 {
1825 D3D_FEATURE_LEVEL_11_0,
1826 D3D_FEATURE_LEVEL_10_1,
1827 D3D_FEATURE_LEVEL_10_0,
1828 };
1829
1830 ID3D11Device* dummyDevice;
1831 D3D_FEATURE_LEVEL dummyFeatureLevel;
1832 ID3D11DeviceContext* dummyContext;
1833
1834 HRESULT result = D3D11CreateDevice(NULL,
1835 D3D_DRIVER_TYPE_HARDWARE,
1836 NULL,
1837 #if defined(_DEBUG)
1838 D3D11_CREATE_DEVICE_DEBUG,
1839 #else
1840 0,
1841 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001842 featureLevels,
1843 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001844 D3D11_SDK_VERSION,
1845 &dummyDevice,
1846 &dummyFeatureLevel,
1847 &dummyContext);
1848
1849 if (!mDevice || FAILED(result))
1850 {
1851 return false;
1852 }
1853
Geoff Langea228632013-07-30 15:17:12 -04001854 SafeRelease(dummyContext);
1855 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001856
1857 return true;
1858}
1859
1860void Renderer11::release()
1861{
1862 releaseDeviceResources();
1863
Geoff Langea228632013-07-30 15:17:12 -04001864 SafeRelease(mDxgiFactory);
1865 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001866
1867 if (mDeviceContext)
1868 {
1869 mDeviceContext->ClearState();
1870 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001871 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001872 }
1873
Geoff Langea228632013-07-30 15:17:12 -04001874 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001875
1876 if (mD3d11Module)
1877 {
1878 FreeLibrary(mD3d11Module);
1879 mD3d11Module = NULL;
1880 }
1881
1882 if (mDxgiModule)
1883 {
1884 FreeLibrary(mDxgiModule);
1885 mDxgiModule = NULL;
1886 }
Geoff Langdad5ed32014-02-10 12:59:17 -05001887
1888 mCompiler.release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001889}
1890
1891bool Renderer11::resetDevice()
1892{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001893 // recreate everything
1894 release();
1895 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001896
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001897 if (result != EGL_SUCCESS)
1898 {
1899 ERR("Could not reinitialize D3D11 device: %08X", result);
1900 return false;
1901 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001902
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001903 mDeviceLost = false;
1904
1905 return true;
1906}
1907
1908DWORD Renderer11::getAdapterVendor() const
1909{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001910 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001911}
1912
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001913std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001914{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001915 std::ostringstream rendererString;
1916
1917 rendererString << mDescription;
1918 rendererString << " Direct3D11";
1919
1920 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1921 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1922
1923 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001924}
1925
1926GUID Renderer11::getAdapterIdentifier() const
1927{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001928 // Use the adapter LUID as our adapter ID
1929 // This number is local to a machine is only guaranteed to be unique between restarts
1930 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1931 GUID adapterId = {0};
1932 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1933 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001934}
1935
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001936bool Renderer11::getBGRATextureSupport() const
1937{
1938 return mBGRATextureSupport;
1939}
1940
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001941bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001942{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001943 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001944}
1945
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001946bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001947{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001948 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001949}
1950
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001951bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001952{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001953 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001954}
1955
1956bool Renderer11::getDepthTextureSupport() const
1957{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001958 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001959}
1960
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001961bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001962{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001963 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001964}
1965
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001966bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001967{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001968 return mFloat32FilterSupport;
1969}
1970
1971bool Renderer11::getFloat32TextureRenderingSupport() const
1972{
1973 return mFloat32RenderSupport;
1974}
1975
1976bool Renderer11::getFloat16TextureSupport() const
1977{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001978 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001979}
1980
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001981bool Renderer11::getFloat16TextureFilteringSupport() const
1982{
1983 return mFloat16FilterSupport;
1984}
1985
1986bool Renderer11::getFloat16TextureRenderingSupport() const
1987{
1988 return mFloat16RenderSupport;
1989}
1990
Geoff Langd42cf4e2013-06-05 16:09:17 -04001991bool Renderer11::getRGB565TextureSupport() const
1992{
1993 return false;
1994}
1995
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001996bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001997{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001998 return false;
1999}
2000
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002001bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002002{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002003 return false;
2004}
2005
Geoff Lang632192d2013-10-04 13:40:46 -04002006bool Renderer11::getRGTextureSupport() const
2007{
2008 return mRGTextureSupport;
2009}
2010
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002011bool Renderer11::getTextureFilterAnisotropySupport() const
2012{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002013 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002014}
2015
2016float Renderer11::getTextureMaxAnisotropy() const
2017{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002018 switch (mFeatureLevel)
2019 {
2020 case D3D_FEATURE_LEVEL_11_0:
2021 return D3D11_MAX_MAXANISOTROPY;
2022 case D3D_FEATURE_LEVEL_10_1:
2023 case D3D_FEATURE_LEVEL_10_0:
2024 return D3D10_MAX_MAXANISOTROPY;
2025 default: UNREACHABLE();
2026 return 0;
2027 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002028}
2029
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002030bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002031{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002032 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002033}
2034
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002035Range Renderer11::getViewportBounds() const
2036{
2037 switch (mFeatureLevel)
2038 {
2039 case D3D_FEATURE_LEVEL_11_0:
2040 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
2041 case D3D_FEATURE_LEVEL_10_1:
2042 case D3D_FEATURE_LEVEL_10_0:
2043 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
2044 default: UNREACHABLE();
2045 return Range(0, 0);
2046 }
2047}
2048
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002049unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002050{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002051 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2052 switch (mFeatureLevel)
2053 {
2054 case D3D_FEATURE_LEVEL_11_0:
2055 case D3D_FEATURE_LEVEL_10_1:
2056 case D3D_FEATURE_LEVEL_10_0:
2057 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
2058 default: UNREACHABLE();
2059 return 0;
2060 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002061}
2062
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002063unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
2064{
2065 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2066}
2067
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002068unsigned int Renderer11::getReservedVertexUniformVectors() const
2069{
Shannon Woods5ab33c82013-06-26 15:31:09 -04002070 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002071}
2072
2073unsigned int Renderer11::getReservedFragmentUniformVectors() const
2074{
Shannon Woods5ab33c82013-06-26 15:31:09 -04002075 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002076}
2077
2078unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002079{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002080 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2081 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2082 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002083}
2084
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002085unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002086{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002087 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2088 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2089 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002090}
2091
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002092unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002093{
2094 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00002095 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
2096 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002097 switch (mFeatureLevel)
2098 {
2099 case D3D_FEATURE_LEVEL_11_0:
2100 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2101 case D3D_FEATURE_LEVEL_10_1:
2102 case D3D_FEATURE_LEVEL_10_0:
2103 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2104 default: UNREACHABLE();
2105 return 0;
2106 }
2107}
2108
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002109unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2110{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002111 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2112 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2113
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002114 switch (mFeatureLevel)
2115 {
2116 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002117 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002118 case D3D_FEATURE_LEVEL_10_1:
2119 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002120 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002121 default: UNREACHABLE();
2122 return 0;
2123 }
2124}
2125
2126unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2127{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002128 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2129 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2130
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002131 switch (mFeatureLevel)
2132 {
2133 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002134 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002135 case D3D_FEATURE_LEVEL_10_1:
2136 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002137 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002138 default: UNREACHABLE();
2139 return 0;
2140 }
2141}
2142
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002143unsigned int Renderer11::getReservedVertexUniformBuffers() const
2144{
2145 // we reserve one buffer for the application uniforms, and one for driver uniforms
2146 return 2;
2147}
2148
2149unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2150{
2151 // we reserve one buffer for the application uniforms, and one for driver uniforms
2152 return 2;
2153}
2154
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002155unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2156{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002157 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2158 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2159
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002160 switch (mFeatureLevel)
2161 {
2162 case D3D_FEATURE_LEVEL_11_0:
2163 return D3D11_SO_BUFFER_SLOT_COUNT;
2164 case D3D_FEATURE_LEVEL_10_1:
2165 case D3D_FEATURE_LEVEL_10_0:
2166 return D3D10_SO_BUFFER_SLOT_COUNT;
2167 default: UNREACHABLE();
2168 return 0;
2169 }
2170}
2171
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002172unsigned int Renderer11::getMaxUniformBufferSize() const
2173{
2174 // Each component is a 4-element vector of 4-byte units (floats)
2175 const unsigned int bytesPerComponent = 4 * sizeof(float);
2176
2177 switch (mFeatureLevel)
2178 {
2179 case D3D_FEATURE_LEVEL_11_0:
2180 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2181 case D3D_FEATURE_LEVEL_10_1:
2182 case D3D_FEATURE_LEVEL_10_0:
2183 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2184 default: UNREACHABLE();
2185 return 0;
2186 }
2187}
2188
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002189bool Renderer11::getNonPower2TextureSupport() const
2190{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002191 switch (mFeatureLevel)
2192 {
2193 case D3D_FEATURE_LEVEL_11_0:
2194 case D3D_FEATURE_LEVEL_10_1:
2195 case D3D_FEATURE_LEVEL_10_0:
2196 return true;
2197 default: UNREACHABLE();
2198 return false;
2199 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002200}
2201
2202bool Renderer11::getOcclusionQuerySupport() const
2203{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002204 switch (mFeatureLevel)
2205 {
2206 case D3D_FEATURE_LEVEL_11_0:
2207 case D3D_FEATURE_LEVEL_10_1:
2208 case D3D_FEATURE_LEVEL_10_0:
2209 return true;
2210 default: UNREACHABLE();
2211 return false;
2212 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002213}
2214
2215bool Renderer11::getInstancingSupport() const
2216{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002217 switch (mFeatureLevel)
2218 {
2219 case D3D_FEATURE_LEVEL_11_0:
2220 case D3D_FEATURE_LEVEL_10_1:
2221 case D3D_FEATURE_LEVEL_10_0:
2222 return true;
2223 default: UNREACHABLE();
2224 return false;
2225 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002226}
2227
2228bool Renderer11::getShareHandleSupport() const
2229{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002230 // We only currently support share handles with BGRA surfaces, because
2231 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002232 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002233 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002234}
2235
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002236bool Renderer11::getDerivativeInstructionSupport() const
2237{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002238 switch (mFeatureLevel)
2239 {
2240 case D3D_FEATURE_LEVEL_11_0:
2241 case D3D_FEATURE_LEVEL_10_1:
2242 case D3D_FEATURE_LEVEL_10_0:
2243 return true;
2244 default: UNREACHABLE();
2245 return false;
2246 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002247}
2248
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002249bool Renderer11::getPostSubBufferSupport() const
2250{
2251 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2252 return false;
2253}
2254
Jamie Madill13a2f852013-12-11 16:35:08 -05002255int Renderer11::getMaxRecommendedElementsIndices() const
2256{
2257 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2258 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2259
2260 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2261 return std::numeric_limits<GLint>::max();
2262}
2263
2264int Renderer11::getMaxRecommendedElementsVertices() const
2265{
2266 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2267 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2268
2269 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2270 return std::numeric_limits<GLint>::max();
2271}
2272
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002273int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002274{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002275 switch (mFeatureLevel)
2276 {
2277 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002278 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002279 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2280 default: UNREACHABLE(); return 0;
2281 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002282}
2283
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002284int Renderer11::getMinorShaderModel() const
2285{
2286 switch (mFeatureLevel)
2287 {
2288 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2289 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2290 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2291 default: UNREACHABLE(); return 0;
2292 }
2293}
2294
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002295float Renderer11::getMaxPointSize() const
2296{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002297 // choose a reasonable maximum. we enforce this in the shader.
2298 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2299 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002300}
2301
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002302int Renderer11::getMaxViewportDimension() const
2303{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002304 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2305 // In our case return the maximum texture size, which is the maximum render buffer size.
2306 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2307 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2308
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002309 switch (mFeatureLevel)
2310 {
2311 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002312 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002313 case D3D_FEATURE_LEVEL_10_1:
2314 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002315 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002316 default: UNREACHABLE();
2317 return 0;
2318 }
2319}
2320
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002321int Renderer11::getMaxTextureWidth() const
2322{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002323 switch (mFeatureLevel)
2324 {
2325 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2326 case D3D_FEATURE_LEVEL_10_1:
2327 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2328 default: UNREACHABLE(); return 0;
2329 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002330}
2331
2332int Renderer11::getMaxTextureHeight() const
2333{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002334 switch (mFeatureLevel)
2335 {
2336 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2337 case D3D_FEATURE_LEVEL_10_1:
2338 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2339 default: UNREACHABLE(); return 0;
2340 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002341}
2342
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002343int Renderer11::getMaxTextureDepth() const
2344{
2345 switch (mFeatureLevel)
2346 {
2347 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2348 case D3D_FEATURE_LEVEL_10_1:
2349 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2350 default: UNREACHABLE(); return 0;
2351 }
2352}
2353
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002354int Renderer11::getMaxTextureArrayLayers() const
2355{
2356 switch (mFeatureLevel)
2357 {
2358 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2359 case D3D_FEATURE_LEVEL_10_1:
2360 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2361 default: UNREACHABLE(); return 0;
2362 }
2363}
2364
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002365bool Renderer11::get32BitIndexSupport() const
2366{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002367 switch (mFeatureLevel)
2368 {
2369 case D3D_FEATURE_LEVEL_11_0:
2370 case D3D_FEATURE_LEVEL_10_1:
2371 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2372 default: UNREACHABLE(); return false;
2373 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002374}
2375
2376int Renderer11::getMinSwapInterval() const
2377{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002378 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002379}
2380
2381int Renderer11::getMaxSwapInterval() const
2382{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002383 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002384}
2385
2386int Renderer11::getMaxSupportedSamples() const
2387{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002388 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002389}
2390
Geoff Lang005df412013-10-16 14:12:50 -04002391GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002392{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002393 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002394 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2395 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2396}
2397
Geoff Lang005df412013-10-16 14:12:50 -04002398GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002399{
2400 unsigned int numCounts = 0;
2401
2402 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002403 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2404 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002405 {
2406 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2407 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2408
2409 if (iter != mMultisampleSupportMap.end())
2410 {
2411 const MultisampleSupportInfo& info = iter->second;
2412 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2413 {
2414 if (info.qualityLevels[i] > 0)
2415 {
2416 numCounts++;
2417 }
2418 }
2419 }
2420 }
2421
2422 return numCounts;
2423}
2424
Geoff Lang005df412013-10-16 14:12:50 -04002425void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002426{
2427 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002428 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2429 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2430 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002431 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002432 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002433
2434 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2435 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2436
2437 if (iter != mMultisampleSupportMap.end())
2438 {
2439 const MultisampleSupportInfo& info = iter->second;
2440 int bufPos = 0;
2441 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2442 {
2443 if (info.qualityLevels[i] > 0)
2444 {
2445 params[bufPos++] = i + 1;
2446 }
2447 }
2448 }
2449}
2450
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002451int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2452{
2453 if (requested == 0)
2454 {
2455 return 0;
2456 }
2457
2458 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2459 if (iter != mMultisampleSupportMap.end())
2460 {
2461 const MultisampleSupportInfo& info = iter->second;
2462 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2463 {
2464 if (info.qualityLevels[i] > 0)
2465 {
2466 return i + 1;
2467 }
2468 }
2469 }
2470
2471 return -1;
2472}
2473
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002474unsigned int Renderer11::getMaxRenderTargets() const
2475{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002476 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2477 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2478
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002479 switch (mFeatureLevel)
2480 {
2481 case D3D_FEATURE_LEVEL_11_0:
2482 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2483 case D3D_FEATURE_LEVEL_10_1:
2484 case D3D_FEATURE_LEVEL_10_0:
2485 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2486 default:
2487 UNREACHABLE();
2488 return 1;
2489 }
2490}
2491
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002492bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002493{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002494 if (source && dest)
2495 {
2496 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2497 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2498
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002499 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002500
2501 dest11->invalidateSwizzleCache();
2502
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002503 return true;
2504 }
2505
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002506 return false;
2507}
2508
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002509bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002510{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002511 if (source && dest)
2512 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002513 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2514 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002515
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002516 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002517
2518 dest11->invalidateSwizzleCache();
2519
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002520 return true;
2521 }
2522
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002523 return false;
2524}
2525
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002526bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2527{
2528 if (source && dest)
2529 {
2530 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2531 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2532
2533 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002534
2535 dest11->invalidateSwizzleCache();
2536
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002537 return true;
2538 }
2539
2540 return false;
2541}
2542
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002543bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2544{
2545 if (source && dest)
2546 {
2547 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2548 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2549
2550 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002551
2552 dest11->invalidateSwizzleCache();
2553
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002554 return true;
2555 }
2556
2557 return false;
2558}
2559
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002560bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002561 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002562{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002563 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002564 if (!colorbuffer)
2565 {
2566 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002567 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002568 }
2569
2570 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2571 if (!sourceRenderTarget)
2572 {
2573 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002574 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002575 }
2576
2577 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2578 if (!source)
2579 {
2580 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002581 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002582 }
2583
2584 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2585 if (!storage11)
2586 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002587 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002588 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002589 }
2590
2591 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2592 if (!destRenderTarget)
2593 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002594 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002595 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002596 }
2597
2598 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2599 if (!dest)
2600 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002601 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002602 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002603 }
2604
Geoff Langb86b9792013-06-04 16:32:05 -04002605 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2606 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002607
Geoff Langb86b9792013-06-04 16:32:05 -04002608 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2609 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002610
Geoff Langb86b9792013-06-04 16:32:05 -04002611 // Use nearest filtering because source and destination are the same size for the direct
2612 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002613 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002614 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002615
Geoff Lang42477a42013-09-17 17:07:02 -04002616 storage11->invalidateSwizzleCacheLevel(level);
2617
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002618 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002619}
2620
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002621bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002622 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002623{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002624 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002625 if (!colorbuffer)
2626 {
2627 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002628 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002629 }
2630
2631 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2632 if (!sourceRenderTarget)
2633 {
2634 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002635 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002636 }
2637
2638 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2639 if (!source)
2640 {
2641 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002642 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002643 }
2644
2645 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2646 if (!storage11)
2647 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002648 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002649 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002650 }
2651
Nicolas Capensb13f8662013-06-04 13:30:19 -04002652 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002653 if (!destRenderTarget)
2654 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002655 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002656 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002657 }
2658
2659 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2660 if (!dest)
2661 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002662 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002663 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002664 }
2665
Geoff Langb86b9792013-06-04 16:32:05 -04002666 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2667 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002668
Geoff Langb86b9792013-06-04 16:32:05 -04002669 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2670 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002671
Geoff Langb86b9792013-06-04 16:32:05 -04002672 // Use nearest filtering because source and destination are the same size for the direct
2673 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002674 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002675 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002676
Geoff Lang42477a42013-09-17 17:07:02 -04002677 storage11->invalidateSwizzleCacheLevel(level);
2678
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002679 return ret;
2680}
2681
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002682bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2683 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2684{
2685 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2686 if (!colorbuffer)
2687 {
2688 ERR("Failed to retrieve the color buffer from the frame buffer.");
2689 return gl::error(GL_OUT_OF_MEMORY, false);
2690 }
2691
2692 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2693 if (!sourceRenderTarget)
2694 {
2695 ERR("Failed to retrieve the render target from the frame buffer.");
2696 return gl::error(GL_OUT_OF_MEMORY, false);
2697 }
2698
2699 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2700 if (!source)
2701 {
2702 ERR("Failed to retrieve the render target view from the render target.");
2703 return gl::error(GL_OUT_OF_MEMORY, false);
2704 }
2705
2706 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2707 if (!storage11)
2708 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002709 ERR("Failed to retrieve the texture storage from the destination.");
2710 return gl::error(GL_OUT_OF_MEMORY, false);
2711 }
2712
2713 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2714 if (!destRenderTarget)
2715 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002716 ERR("Failed to retrieve the render target from the destination storage.");
2717 return gl::error(GL_OUT_OF_MEMORY, false);
2718 }
2719
2720 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2721 if (!dest)
2722 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002723 ERR("Failed to retrieve the render target view from the destination render target.");
2724 return gl::error(GL_OUT_OF_MEMORY, false);
2725 }
2726
Geoff Langb86b9792013-06-04 16:32:05 -04002727 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2728 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002729
Geoff Langb86b9792013-06-04 16:32:05 -04002730 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2731 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002732
Geoff Langb86b9792013-06-04 16:32:05 -04002733 // Use nearest filtering because source and destination are the same size for the direct
2734 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002735 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002736 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002737
Geoff Lang42477a42013-09-17 17:07:02 -04002738 storage11->invalidateSwizzleCacheLevel(level);
2739
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002740 return ret;
2741}
2742
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002743bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2744 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2745{
2746 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2747 if (!colorbuffer)
2748 {
2749 ERR("Failed to retrieve the color buffer from the frame buffer.");
2750 return gl::error(GL_OUT_OF_MEMORY, false);
2751 }
2752
2753 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2754 if (!sourceRenderTarget)
2755 {
2756 ERR("Failed to retrieve the render target from the frame buffer.");
2757 return gl::error(GL_OUT_OF_MEMORY, false);
2758 }
2759
2760 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2761 if (!source)
2762 {
2763 ERR("Failed to retrieve the render target view from the render target.");
2764 return gl::error(GL_OUT_OF_MEMORY, false);
2765 }
2766
2767 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2768 if (!storage11)
2769 {
Geoff Langea228632013-07-30 15:17:12 -04002770 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002771 ERR("Failed to retrieve the texture storage from the destination.");
2772 return gl::error(GL_OUT_OF_MEMORY, false);
2773 }
2774
2775 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2776 if (!destRenderTarget)
2777 {
Geoff Langea228632013-07-30 15:17:12 -04002778 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002779 ERR("Failed to retrieve the render target from the destination storage.");
2780 return gl::error(GL_OUT_OF_MEMORY, false);
2781 }
2782
2783 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2784 if (!dest)
2785 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002786 ERR("Failed to retrieve the render target view from the destination render target.");
2787 return gl::error(GL_OUT_OF_MEMORY, false);
2788 }
2789
Geoff Langb86b9792013-06-04 16:32:05 -04002790 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2791 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002792
Geoff Langb86b9792013-06-04 16:32:05 -04002793 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2794 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002795
Geoff Langb86b9792013-06-04 16:32:05 -04002796 // Use nearest filtering because source and destination are the same size for the direct
2797 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002798 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002799 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002800
Geoff Lang42477a42013-09-17 17:07:02 -04002801 storage11->invalidateSwizzleCacheLevel(level);
2802
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002803 return ret;
2804}
2805
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002806void Renderer11::unapplyRenderTargets()
2807{
2808 setOneTimeRenderTarget(NULL);
2809}
2810
2811void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2812{
2813 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2814
2815 rtvArray[0] = renderTargetView;
2816
2817 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2818
2819 // Do not preserve the serial for this one-time-use render target
2820 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2821 {
2822 mAppliedRenderTargetSerials[rtIndex] = 0;
2823 }
2824}
2825
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002826RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2827{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002828 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002829 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002830
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002831 if (depth)
2832 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002833 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002834 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002835 swapChain11->getDepthStencilTexture(),
2836 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002837 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002838 }
2839 else
2840 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002841 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002842 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002843 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002844 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002845 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002846 }
2847 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002848}
2849
Geoff Langa2d97f12013-06-11 11:44:02 -04002850RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002851{
Geoff Langa2d97f12013-06-11 11:44:02 -04002852 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002853 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002854}
2855
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002856ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002857{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002858 ShaderExecutable11 *executable = NULL;
2859
2860 switch (type)
2861 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002862 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002863 {
2864 ID3D11VertexShader *vshader = NULL;
2865 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2866 ASSERT(SUCCEEDED(result));
2867
2868 if (vshader)
2869 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002870 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002871 }
2872 }
2873 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002874 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002875 {
2876 ID3D11PixelShader *pshader = NULL;
2877 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2878 ASSERT(SUCCEEDED(result));
2879
2880 if (pshader)
2881 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002882 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002883 }
2884 }
2885 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002886 case rx::SHADER_GEOMETRY:
2887 {
2888 ID3D11GeometryShader *gshader = NULL;
2889 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2890 ASSERT(SUCCEEDED(result));
2891
2892 if (gshader)
2893 {
2894 executable = new ShaderExecutable11(function, length, gshader);
2895 }
2896 }
2897 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002898 default:
2899 UNREACHABLE();
2900 break;
2901 }
2902
2903 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002904}
2905
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002906ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002907{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002908 const char *profile = NULL;
2909
2910 switch (type)
2911 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002912 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002913 profile = "vs_4_0";
2914 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002915 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002916 profile = "ps_4_0";
2917 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002918 case rx::SHADER_GEOMETRY:
2919 profile = "gs_4_0";
2920 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002921 default:
2922 UNREACHABLE();
2923 return NULL;
2924 }
2925
Geoff Langdad5ed32014-02-10 12:59:17 -05002926 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002927 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002928 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002929 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002930 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002931
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002932 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002933 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002934
2935 return executable;
2936}
2937
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002938rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2939{
2940 return new UniformStorage11(this, storageSize);
2941}
2942
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002943VertexBuffer *Renderer11::createVertexBuffer()
2944{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002945 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002946}
2947
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002948IndexBuffer *Renderer11::createIndexBuffer()
2949{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002950 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002951}
2952
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002953BufferStorage *Renderer11::createBufferStorage()
2954{
2955 return new BufferStorage11(this);
2956}
2957
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002958QueryImpl *Renderer11::createQuery(GLenum type)
2959{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002960 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002961}
2962
2963FenceImpl *Renderer11::createFence()
2964{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002965 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002966}
2967
Geoff Lang005df412013-10-16 14:12:50 -04002968bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002969{
Jamie Madill4461f092013-10-10 15:10:39 -04002970 int clientVersion = getCurrentClientVersion();
2971
2972 // We only support buffer to texture copies in ES3
2973 if (clientVersion <= 2)
2974 {
2975 return false;
2976 }
2977
2978 // sRGB formats do not work with D3D11 buffer SRVs
2979 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2980 {
2981 return false;
2982 }
2983
2984 // We cannot support direct copies to non-color-renderable formats
2985 if (!gl::IsColorRenderingSupported(internalFormat, this))
2986 {
2987 return false;
2988 }
2989
2990 // We skip all 3-channel formats since sometimes format support is missing
2991 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2992 {
2993 return false;
2994 }
2995
2996 // We don't support formats which we can't represent without conversion
2997 if (getNativeTextureFormat(internalFormat) != internalFormat)
2998 {
2999 return false;
3000 }
3001
3002 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04003003}
3004
Jamie Madilla21eea32013-09-18 14:36:25 -04003005bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
3006 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
3007{
Jamie Madill0e0510f2013-10-10 15:46:23 -04003008 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04003009 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
3010}
3011
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003012bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003013{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003014 ASSERT(colorbuffer != NULL);
3015
3016 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
3017 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003018 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003019 *subresourceIndex = renderTarget->getSubresourceIndex();
3020
3021 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
3022 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003023 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003024 ID3D11Resource *textureResource = NULL;
3025 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003026
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003027 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003028 {
Geoff Lang8e328842014-02-10 13:11:20 -05003029 HRESULT result = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04003030 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003031
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003032 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003033 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003034 return true;
3035 }
3036 else
3037 {
3038 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
3039 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003040 }
3041 }
3042 }
3043 }
3044
3045 return false;
3046}
3047
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003048bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04003049 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003050{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003051 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003052 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003053 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003054
3055 if (!readBuffer)
3056 {
3057 ERR("Failed to retrieve the read buffer from the read framebuffer.");
3058 return gl::error(GL_OUT_OF_MEMORY, false);
3059 }
3060
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003061 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003062
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003063 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003064 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003065 if (drawTarget->isEnabledColorAttachment(colorAttachment))
3066 {
3067 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
3068
3069 if (!drawBuffer)
3070 {
3071 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
3072 return gl::error(GL_OUT_OF_MEMORY, false);
3073 }
3074
3075 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
3076
Geoff Lang125deab2013-08-09 13:34:16 -04003077 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04003078 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003079 {
3080 return false;
3081 }
3082 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003083 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003084 }
3085
Geoff Lang685806d2013-06-12 11:16:36 -04003086 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003087 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003088 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
3089 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
3090
3091 if (!readBuffer)
3092 {
3093 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
3094 return gl::error(GL_OUT_OF_MEMORY, false);
3095 }
3096
3097 if (!drawBuffer)
3098 {
3099 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
3100 return gl::error(GL_OUT_OF_MEMORY, false);
3101 }
3102
3103 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3104 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3105
Geoff Lang125deab2013-08-09 13:34:16 -04003106 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04003107 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003108 {
3109 return false;
3110 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003111 }
3112
Geoff Lang42477a42013-09-17 17:07:02 -04003113 invalidateFramebufferSwizzles(drawTarget);
3114
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003115 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003116}
3117
3118void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3119 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3120{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003121 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003122 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003123
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003124 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3125
3126 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003127 {
3128 gl::Rectangle area;
3129 area.x = x;
3130 area.y = y;
3131 area.width = width;
3132 area.height = height;
3133
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003134 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3135 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003136
Geoff Langea228632013-07-30 15:17:12 -04003137 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003138 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003139}
3140
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003141Image *Renderer11::createImage()
3142{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003143 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003144}
3145
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003146void Renderer11::generateMipmap(Image *dest, Image *src)
3147{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003148 Image11 *dest11 = Image11::makeImage11(dest);
3149 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003150 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003151}
3152
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003153TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3154{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003155 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3156 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003157}
3158
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003159TextureStorage *Renderer11::createTextureStorage2D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003160{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003161 return new TextureStorage11_2D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003162}
3163
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003164TextureStorage *Renderer11::createTextureStorageCube(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, int size)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003165{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003166 return new TextureStorage11_Cube(this, baseLevel, maxLevel, internalformat, renderTarget, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003167}
3168
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003169TextureStorage *Renderer11::createTextureStorage3D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth)
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003170{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003171 return new TextureStorage11_3D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003172}
3173
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003174TextureStorage *Renderer11::createTextureStorage2DArray(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth)
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003175{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003176 return new TextureStorage11_2DArray(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003177}
3178
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003179void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3180 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3181 GLint packAlignment, void *pixels)
3182{
3183 D3D11_TEXTURE2D_DESC textureDesc;
3184 texture->GetDesc(&textureDesc);
3185
3186 D3D11_TEXTURE2D_DESC stagingDesc;
3187 stagingDesc.Width = area.width;
3188 stagingDesc.Height = area.height;
3189 stagingDesc.MipLevels = 1;
3190 stagingDesc.ArraySize = 1;
3191 stagingDesc.Format = textureDesc.Format;
3192 stagingDesc.SampleDesc.Count = 1;
3193 stagingDesc.SampleDesc.Quality = 0;
3194 stagingDesc.Usage = D3D11_USAGE_STAGING;
3195 stagingDesc.BindFlags = 0;
3196 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3197 stagingDesc.MiscFlags = 0;
3198
3199 ID3D11Texture2D* stagingTex = NULL;
3200 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3201 if (FAILED(result))
3202 {
3203 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3204 return;
3205 }
3206
3207 ID3D11Texture2D* srcTex = NULL;
3208 if (textureDesc.SampleDesc.Count > 1)
3209 {
3210 D3D11_TEXTURE2D_DESC resolveDesc;
3211 resolveDesc.Width = textureDesc.Width;
3212 resolveDesc.Height = textureDesc.Height;
3213 resolveDesc.MipLevels = 1;
3214 resolveDesc.ArraySize = 1;
3215 resolveDesc.Format = textureDesc.Format;
3216 resolveDesc.SampleDesc.Count = 1;
3217 resolveDesc.SampleDesc.Quality = 0;
3218 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3219 resolveDesc.BindFlags = 0;
3220 resolveDesc.CPUAccessFlags = 0;
3221 resolveDesc.MiscFlags = 0;
3222
3223 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3224 if (FAILED(result))
3225 {
3226 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003227 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003228 return;
3229 }
3230
3231 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3232 subResource = 0;
3233 }
3234 else
3235 {
3236 srcTex = texture;
3237 srcTex->AddRef();
3238 }
3239
3240 D3D11_BOX srcBox;
3241 srcBox.left = area.x;
3242 srcBox.right = area.x + area.width;
3243 srcBox.top = area.y;
3244 srcBox.bottom = area.y + area.height;
3245 srcBox.front = 0;
3246 srcBox.back = 1;
3247
3248 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3249
Geoff Langea228632013-07-30 15:17:12 -04003250 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003251
3252 D3D11_MAPPED_SUBRESOURCE mapping;
3253 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3254
3255 unsigned char *source;
3256 int inputPitch;
3257 if (packReverseRowOrder)
3258 {
3259 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3260 inputPitch = -static_cast<int>(mapping.RowPitch);
3261 }
3262 else
3263 {
3264 source = static_cast<unsigned char*>(mapping.pData);
3265 inputPitch = static_cast<int>(mapping.RowPitch);
3266 }
3267
Geoff Lang697ad3e2013-06-04 10:11:28 -04003268 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003269
Geoff Lang005df412013-10-16 14:12:50 -04003270 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003271 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3272 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3273
3274 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3275
3276 if (sourceFormat == format && sourceType == type)
3277 {
3278 // Direct copy possible
3279 unsigned char *dest = static_cast<unsigned char*>(pixels);
3280 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003281 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003282 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003283 }
3284 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003285 else
3286 {
Geoff Lang005df412013-10-16 14:12:50 -04003287 GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003288 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3289
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003290 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003291 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003292 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003293 // Fast copy is possible through some special function
3294 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003295 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003296 for (int x = 0; x < area.width; x++)
3297 {
3298 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3299 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3300
3301 fastCopyFunc(src, dest);
3302 }
3303 }
3304 }
3305 else
3306 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003307 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003308 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3309
3310 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3311 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3312 sizeof(temp) >= sizeof(gl::ColorUI) &&
3313 sizeof(temp) >= sizeof(gl::ColorI));
3314
3315 for (int y = 0; y < area.height; y++)
3316 {
3317 for (int x = 0; x < area.width; x++)
3318 {
3319 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3320 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3321
3322 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3323 // will not allow the copy otherwise.
3324 readFunc(src, temp);
3325 writeFunc(temp, dest);
3326 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003327 }
3328 }
3329 }
3330
3331 mDeviceContext->Unmap(stagingTex, 0);
3332
Geoff Langea228632013-07-30 15:17:12 -04003333 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003334}
3335
Geoff Lang758d5b22013-06-11 11:42:50 -04003336bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003337 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3338 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003339{
Geoff Lang975af372013-06-12 11:19:22 -04003340 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3341 // it should never be the case that both color and depth/stencil need to be blitted at
3342 // at the same time.
3343 ASSERT(colorBlit != (depthBlit || stencilBlit));
3344
Geoff Langc1f51be2013-06-11 11:49:14 -04003345 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003346
Geoff Lang4d782732013-07-22 10:44:18 -04003347 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3348 if (!drawRenderTarget)
3349 {
3350 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3351 return gl::error(GL_OUT_OF_MEMORY, false);
3352 }
3353
3354 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3355 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3356 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3357 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3358
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003359 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3360 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003361 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003362 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003363 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003364 }
3365
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003366 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003367 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003368 unsigned int readSubresource = 0;
3369 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003370 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003371 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3372 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003373
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003374 if (unresolvedTexture)
3375 {
3376 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3377 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003378
Geoff Langea228632013-07-30 15:17:12 -04003379 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003380
Geoff Lang5c9a29a2014-02-11 10:26:24 -05003381 HRESULT hresult = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3382 if (FAILED(hresult))
Geoff Langc1f51be2013-06-11 11:49:14 -04003383 {
Geoff Langea228632013-07-30 15:17:12 -04003384 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003385 return gl::error(GL_OUT_OF_MEMORY, false);
3386 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003387 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003388 }
3389 else
3390 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003391 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003392 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003393 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003394 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003395 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003396 }
3397
Geoff Lang4d782732013-07-22 10:44:18 -04003398 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003399 {
Geoff Lang4d782732013-07-22 10:44:18 -04003400 SafeRelease(readTexture);
3401 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003402 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003403 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003404 }
3405
Geoff Lang125deab2013-08-09 13:34:16 -04003406 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3407 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3408
3409 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3410
3411 bool wholeBufferCopy = !scissorNeeded &&
3412 readRect.x == 0 && readRect.width == readSize.width &&
3413 readRect.y == 0 && readRect.height == readSize.height &&
3414 drawRect.x == 0 && drawRect.width == drawSize.width &&
3415 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003416
Geoff Langc1f51be2013-06-11 11:49:14 -04003417 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003418
Geoff Lang1cd1b212014-02-11 09:42:27 -05003419 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003420
3421 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3422 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3423 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3424 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3425
3426 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3427 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3428 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3429
Geoff Langc1f51be2013-06-11 11:49:14 -04003430 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003431 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3432 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003433 {
Geoff Lang125deab2013-08-09 13:34:16 -04003434 UINT dstX = drawRect.x;
3435 UINT dstY = drawRect.y;
3436
Geoff Langc1f51be2013-06-11 11:49:14 -04003437 D3D11_BOX readBox;
3438 readBox.left = readRect.x;
3439 readBox.right = readRect.x + readRect.width;
3440 readBox.top = readRect.y;
3441 readBox.bottom = readRect.y + readRect.height;
3442 readBox.front = 0;
3443 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003444
Geoff Lang125deab2013-08-09 13:34:16 -04003445 if (scissorNeeded)
3446 {
3447 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3448 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3449
3450 if (drawRect.x < scissor->x)
3451 {
3452 dstX = scissor->x;
3453 readBox.left += (scissor->x - drawRect.x);
3454 }
3455 if (drawRect.y < scissor->y)
3456 {
3457 dstY = scissor->y;
3458 readBox.top += (scissor->y - drawRect.y);
3459 }
3460 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3461 {
3462 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3463 }
3464 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3465 {
3466 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3467 }
3468 }
3469
Geoff Langc1f51be2013-06-11 11:49:14 -04003470 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3471 // We also require complete framebuffer copies for depth-stencil blit.
3472 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003473
Geoff Lang125deab2013-08-09 13:34:16 -04003474 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003475 readTexture, readSubresource, pSrcBox);
3476 result = true;
3477 }
3478 else
3479 {
3480 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003481 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003482
Geoff Lang975af372013-06-12 11:19:22 -04003483 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003484 {
Geoff Lang975af372013-06-12 11:19:22 -04003485 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003486 drawTexture, drawSubresource, drawArea, drawSize,
3487 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003488 }
3489 else if (depthBlit)
3490 {
Geoff Lang125deab2013-08-09 13:34:16 -04003491 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3492 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003493 }
3494 else if (stencilBlit)
3495 {
3496 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003497 drawTexture, drawSubresource, drawArea, drawSize,
3498 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003499 }
3500 else
3501 {
Geoff Lang685806d2013-06-12 11:16:36 -04003502 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003503 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3504 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003505 }
3506 }
3507
3508 SafeRelease(readTexture);
3509 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003510
3511 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003512}
3513
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003514ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3515{
3516 D3D11_TEXTURE2D_DESC textureDesc;
3517 source->GetDesc(&textureDesc);
3518
3519 if (textureDesc.SampleDesc.Count > 1)
3520 {
3521 D3D11_TEXTURE2D_DESC resolveDesc;
3522 resolveDesc.Width = textureDesc.Width;
3523 resolveDesc.Height = textureDesc.Height;
3524 resolveDesc.MipLevels = 1;
3525 resolveDesc.ArraySize = 1;
3526 resolveDesc.Format = textureDesc.Format;
3527 resolveDesc.SampleDesc.Count = 1;
3528 resolveDesc.SampleDesc.Quality = 0;
3529 resolveDesc.Usage = textureDesc.Usage;
3530 resolveDesc.BindFlags = textureDesc.BindFlags;
3531 resolveDesc.CPUAccessFlags = 0;
3532 resolveDesc.MiscFlags = 0;
3533
3534 ID3D11Texture2D *resolveTexture = NULL;
3535 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3536 if (FAILED(result))
3537 {
3538 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3539 return NULL;
3540 }
3541
3542 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3543 return resolveTexture;
3544 }
3545 else
3546 {
3547 source->AddRef();
3548 return source;
3549 }
3550}
3551
Geoff Lang42477a42013-09-17 17:07:02 -04003552void Renderer11::invalidateRenderbufferSwizzles(gl::Renderbuffer *renderBuffer, int mipLevel)
3553{
3554 TextureStorage *texStorage = renderBuffer->getTextureStorage();
3555 if (texStorage)
3556 {
3557 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3558 if (!texStorage11)
3559 {
3560 ERR("texture storage pointer unexpectedly null.");
3561 return;
3562 }
3563
3564 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3565 }
3566}
3567
3568void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3569{
3570 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3571 {
3572 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
3573 if (colorbuffer)
3574 {
3575 invalidateRenderbufferSwizzles(colorbuffer, framebuffer->getColorbufferMipLevel(colorAttachment));
3576 }
3577 }
3578
3579 gl::Renderbuffer *depthBuffer = framebuffer->getDepthbuffer();
3580 if (depthBuffer)
3581 {
3582 invalidateRenderbufferSwizzles(depthBuffer, framebuffer->getDepthbufferMipLevel());
3583 }
3584
3585 gl::Renderbuffer *stencilBuffer = framebuffer->getStencilbuffer();
3586 if (stencilBuffer)
3587 {
3588 invalidateRenderbufferSwizzles(stencilBuffer, framebuffer->getStencilbufferMipLevel());
3589 }
3590}
3591
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003592bool Renderer11::getLUID(LUID *adapterLuid) const
3593{
3594 adapterLuid->HighPart = 0;
3595 adapterLuid->LowPart = 0;
3596
3597 if (!mDxgiAdapter)
3598 {
3599 return false;
3600 }
3601
3602 DXGI_ADAPTER_DESC adapterDesc;
3603 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3604 {
3605 return false;
3606 }
3607
3608 *adapterLuid = adapterDesc.AdapterLuid;
3609 return true;
3610}
3611
Geoff Lang005df412013-10-16 14:12:50 -04003612GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003613{
3614 int clientVersion = getCurrentClientVersion();
3615 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3616}
3617
Jamie Madill95ffb862014-01-29 09:26:59 -05003618rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3619{
3620 return gl_d3d11::GetVertexConversionType(vertexFormat);
3621}
3622
3623GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3624{
3625 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3626}
3627
Geoff Lang61e49a52013-05-29 10:22:58 -04003628Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3629{
3630 MultisampleSupportInfo supportInfo = { 0 };
3631
3632 UINT formatSupport;
3633 HRESULT result;
3634
3635 result = mDevice->CheckFormatSupport(format, &formatSupport);
3636 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3637 {
3638 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3639 {
3640 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3641 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3642 {
3643 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3644 }
3645 else
3646 {
3647 supportInfo.qualityLevels[i - 1] = 0;
3648 }
3649 }
3650 }
3651
3652 return supportInfo;
3653}
3654
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003655}