blob: 8e881ca9df82702f5446bb9bda7d0b15be44e2c2 [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:
Geoff Lang626d54e2014-02-07 14:24:12 -05002485 // Feature level 10.0 and 10.1 cards perform very poorly when the pixel shader
2486 // outputs to multiple RTs that are not bound.
2487 // TODO: Remove pixel shader outputs for render targets that are not bound.
2488 return 1;
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002489 default:
2490 UNREACHABLE();
2491 return 1;
2492 }
2493}
2494
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002495bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002496{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002497 if (source && dest)
2498 {
2499 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2500 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2501
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002502 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002503
2504 dest11->invalidateSwizzleCache();
2505
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002506 return true;
2507 }
2508
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002509 return false;
2510}
2511
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002512bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002513{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002514 if (source && dest)
2515 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002516 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2517 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002518
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002519 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002520
2521 dest11->invalidateSwizzleCache();
2522
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002523 return true;
2524 }
2525
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002526 return false;
2527}
2528
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002529bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2530{
2531 if (source && dest)
2532 {
2533 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2534 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2535
2536 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002537
2538 dest11->invalidateSwizzleCache();
2539
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002540 return true;
2541 }
2542
2543 return false;
2544}
2545
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002546bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2547{
2548 if (source && dest)
2549 {
2550 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2551 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2552
2553 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002554
2555 dest11->invalidateSwizzleCache();
2556
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002557 return true;
2558 }
2559
2560 return false;
2561}
2562
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002563bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002564 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002565{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002566 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002567 if (!colorbuffer)
2568 {
2569 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002570 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002571 }
2572
2573 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2574 if (!sourceRenderTarget)
2575 {
2576 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002577 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002578 }
2579
2580 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2581 if (!source)
2582 {
2583 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002584 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002585 }
2586
2587 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2588 if (!storage11)
2589 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002590 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002591 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002592 }
2593
2594 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2595 if (!destRenderTarget)
2596 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002597 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002598 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002599 }
2600
2601 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2602 if (!dest)
2603 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002604 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002605 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002606 }
2607
Geoff Langb86b9792013-06-04 16:32:05 -04002608 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2609 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002610
Geoff Langb86b9792013-06-04 16:32:05 -04002611 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2612 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002613
Geoff Langb86b9792013-06-04 16:32:05 -04002614 // Use nearest filtering because source and destination are the same size for the direct
2615 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002616 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002617 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002618
Geoff Lang42477a42013-09-17 17:07:02 -04002619 storage11->invalidateSwizzleCacheLevel(level);
2620
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002621 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002622}
2623
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002624bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002625 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002626{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002627 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002628 if (!colorbuffer)
2629 {
2630 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002631 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002632 }
2633
2634 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2635 if (!sourceRenderTarget)
2636 {
2637 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002638 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002639 }
2640
2641 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2642 if (!source)
2643 {
2644 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002645 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002646 }
2647
2648 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2649 if (!storage11)
2650 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002651 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002652 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002653 }
2654
Nicolas Capensb13f8662013-06-04 13:30:19 -04002655 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002656 if (!destRenderTarget)
2657 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002658 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002659 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002660 }
2661
2662 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2663 if (!dest)
2664 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002665 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002666 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002667 }
2668
Geoff Langb86b9792013-06-04 16:32:05 -04002669 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2670 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002671
Geoff Langb86b9792013-06-04 16:32:05 -04002672 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2673 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002674
Geoff Langb86b9792013-06-04 16:32:05 -04002675 // Use nearest filtering because source and destination are the same size for the direct
2676 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002677 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002678 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002679
Geoff Lang42477a42013-09-17 17:07:02 -04002680 storage11->invalidateSwizzleCacheLevel(level);
2681
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002682 return ret;
2683}
2684
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002685bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2686 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2687{
2688 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2689 if (!colorbuffer)
2690 {
2691 ERR("Failed to retrieve the color buffer from the frame buffer.");
2692 return gl::error(GL_OUT_OF_MEMORY, false);
2693 }
2694
2695 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2696 if (!sourceRenderTarget)
2697 {
2698 ERR("Failed to retrieve the render target from the frame buffer.");
2699 return gl::error(GL_OUT_OF_MEMORY, false);
2700 }
2701
2702 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2703 if (!source)
2704 {
2705 ERR("Failed to retrieve the render target view from the render target.");
2706 return gl::error(GL_OUT_OF_MEMORY, false);
2707 }
2708
2709 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2710 if (!storage11)
2711 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002712 ERR("Failed to retrieve the texture storage from the destination.");
2713 return gl::error(GL_OUT_OF_MEMORY, false);
2714 }
2715
2716 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2717 if (!destRenderTarget)
2718 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002719 ERR("Failed to retrieve the render target from the destination storage.");
2720 return gl::error(GL_OUT_OF_MEMORY, false);
2721 }
2722
2723 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2724 if (!dest)
2725 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002726 ERR("Failed to retrieve the render target view from the destination render target.");
2727 return gl::error(GL_OUT_OF_MEMORY, false);
2728 }
2729
Geoff Langb86b9792013-06-04 16:32:05 -04002730 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2731 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002732
Geoff Langb86b9792013-06-04 16:32:05 -04002733 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2734 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002735
Geoff Langb86b9792013-06-04 16:32:05 -04002736 // Use nearest filtering because source and destination are the same size for the direct
2737 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002738 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002739 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002740
Geoff Lang42477a42013-09-17 17:07:02 -04002741 storage11->invalidateSwizzleCacheLevel(level);
2742
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002743 return ret;
2744}
2745
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002746bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2747 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2748{
2749 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2750 if (!colorbuffer)
2751 {
2752 ERR("Failed to retrieve the color buffer from the frame buffer.");
2753 return gl::error(GL_OUT_OF_MEMORY, false);
2754 }
2755
2756 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2757 if (!sourceRenderTarget)
2758 {
2759 ERR("Failed to retrieve the render target from the frame buffer.");
2760 return gl::error(GL_OUT_OF_MEMORY, false);
2761 }
2762
2763 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2764 if (!source)
2765 {
2766 ERR("Failed to retrieve the render target view from the render target.");
2767 return gl::error(GL_OUT_OF_MEMORY, false);
2768 }
2769
2770 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2771 if (!storage11)
2772 {
Geoff Langea228632013-07-30 15:17:12 -04002773 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002774 ERR("Failed to retrieve the texture storage from the destination.");
2775 return gl::error(GL_OUT_OF_MEMORY, false);
2776 }
2777
2778 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2779 if (!destRenderTarget)
2780 {
Geoff Langea228632013-07-30 15:17:12 -04002781 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002782 ERR("Failed to retrieve the render target from the destination storage.");
2783 return gl::error(GL_OUT_OF_MEMORY, false);
2784 }
2785
2786 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2787 if (!dest)
2788 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002789 ERR("Failed to retrieve the render target view from the destination render target.");
2790 return gl::error(GL_OUT_OF_MEMORY, false);
2791 }
2792
Geoff Langb86b9792013-06-04 16:32:05 -04002793 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2794 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002795
Geoff Langb86b9792013-06-04 16:32:05 -04002796 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2797 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002798
Geoff Langb86b9792013-06-04 16:32:05 -04002799 // Use nearest filtering because source and destination are the same size for the direct
2800 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002801 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002802 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002803
Geoff Lang42477a42013-09-17 17:07:02 -04002804 storage11->invalidateSwizzleCacheLevel(level);
2805
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002806 return ret;
2807}
2808
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002809void Renderer11::unapplyRenderTargets()
2810{
2811 setOneTimeRenderTarget(NULL);
2812}
2813
2814void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2815{
2816 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2817
2818 rtvArray[0] = renderTargetView;
2819
2820 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2821
2822 // Do not preserve the serial for this one-time-use render target
2823 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2824 {
2825 mAppliedRenderTargetSerials[rtIndex] = 0;
2826 }
2827}
2828
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002829RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2830{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002831 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002832 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002833
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002834 if (depth)
2835 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002836 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002837 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002838 swapChain11->getDepthStencilTexture(),
2839 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002840 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002841 }
2842 else
2843 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002844 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002845 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002846 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002847 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002848 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002849 }
2850 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002851}
2852
Geoff Langa2d97f12013-06-11 11:44:02 -04002853RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002854{
Geoff Langa2d97f12013-06-11 11:44:02 -04002855 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002856 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002857}
2858
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002859ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002860{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002861 ShaderExecutable11 *executable = NULL;
2862
2863 switch (type)
2864 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002865 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002866 {
2867 ID3D11VertexShader *vshader = NULL;
2868 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2869 ASSERT(SUCCEEDED(result));
2870
2871 if (vshader)
2872 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002873 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002874 }
2875 }
2876 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002877 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002878 {
2879 ID3D11PixelShader *pshader = NULL;
2880 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2881 ASSERT(SUCCEEDED(result));
2882
2883 if (pshader)
2884 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002885 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002886 }
2887 }
2888 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002889 case rx::SHADER_GEOMETRY:
2890 {
2891 ID3D11GeometryShader *gshader = NULL;
2892 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2893 ASSERT(SUCCEEDED(result));
2894
2895 if (gshader)
2896 {
2897 executable = new ShaderExecutable11(function, length, gshader);
2898 }
2899 }
2900 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002901 default:
2902 UNREACHABLE();
2903 break;
2904 }
2905
2906 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002907}
2908
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002909ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002910{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002911 const char *profile = NULL;
2912
2913 switch (type)
2914 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002915 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002916 profile = "vs_4_0";
2917 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002918 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002919 profile = "ps_4_0";
2920 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002921 case rx::SHADER_GEOMETRY:
2922 profile = "gs_4_0";
2923 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002924 default:
2925 UNREACHABLE();
2926 return NULL;
2927 }
2928
Geoff Langdad5ed32014-02-10 12:59:17 -05002929 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002930 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002931 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002932 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002933 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002934
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002935 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002936 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002937
2938 return executable;
2939}
2940
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002941rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2942{
2943 return new UniformStorage11(this, storageSize);
2944}
2945
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002946VertexBuffer *Renderer11::createVertexBuffer()
2947{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002948 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002949}
2950
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002951IndexBuffer *Renderer11::createIndexBuffer()
2952{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002953 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002954}
2955
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002956BufferStorage *Renderer11::createBufferStorage()
2957{
2958 return new BufferStorage11(this);
2959}
2960
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002961QueryImpl *Renderer11::createQuery(GLenum type)
2962{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002963 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002964}
2965
2966FenceImpl *Renderer11::createFence()
2967{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002968 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002969}
2970
Geoff Lang005df412013-10-16 14:12:50 -04002971bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002972{
Jamie Madill4461f092013-10-10 15:10:39 -04002973 int clientVersion = getCurrentClientVersion();
2974
2975 // We only support buffer to texture copies in ES3
2976 if (clientVersion <= 2)
2977 {
2978 return false;
2979 }
2980
2981 // sRGB formats do not work with D3D11 buffer SRVs
2982 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2983 {
2984 return false;
2985 }
2986
2987 // We cannot support direct copies to non-color-renderable formats
2988 if (!gl::IsColorRenderingSupported(internalFormat, this))
2989 {
2990 return false;
2991 }
2992
2993 // We skip all 3-channel formats since sometimes format support is missing
2994 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2995 {
2996 return false;
2997 }
2998
2999 // We don't support formats which we can't represent without conversion
3000 if (getNativeTextureFormat(internalFormat) != internalFormat)
3001 {
3002 return false;
3003 }
3004
3005 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04003006}
3007
Jamie Madilla21eea32013-09-18 14:36:25 -04003008bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
3009 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
3010{
Jamie Madill0e0510f2013-10-10 15:46:23 -04003011 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04003012 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
3013}
3014
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003015bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003016{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003017 ASSERT(colorbuffer != NULL);
3018
3019 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
3020 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003021 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003022 *subresourceIndex = renderTarget->getSubresourceIndex();
3023
3024 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
3025 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003026 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003027 ID3D11Resource *textureResource = NULL;
3028 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003029
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003030 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003031 {
Geoff Lang8e328842014-02-10 13:11:20 -05003032 HRESULT result = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04003033 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003034
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003035 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003036 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003037 return true;
3038 }
3039 else
3040 {
3041 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
3042 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003043 }
3044 }
3045 }
3046 }
3047
3048 return false;
3049}
3050
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003051bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04003052 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003053{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003054 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003055 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003056 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003057
3058 if (!readBuffer)
3059 {
3060 ERR("Failed to retrieve the read buffer from the read framebuffer.");
3061 return gl::error(GL_OUT_OF_MEMORY, false);
3062 }
3063
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003064 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003065
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003066 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003067 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003068 if (drawTarget->isEnabledColorAttachment(colorAttachment))
3069 {
3070 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
3071
3072 if (!drawBuffer)
3073 {
3074 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
3075 return gl::error(GL_OUT_OF_MEMORY, false);
3076 }
3077
3078 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
3079
Geoff Lang125deab2013-08-09 13:34:16 -04003080 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04003081 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003082 {
3083 return false;
3084 }
3085 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003086 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003087 }
3088
Geoff Lang685806d2013-06-12 11:16:36 -04003089 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003090 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003091 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
3092 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
3093
3094 if (!readBuffer)
3095 {
3096 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
3097 return gl::error(GL_OUT_OF_MEMORY, false);
3098 }
3099
3100 if (!drawBuffer)
3101 {
3102 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
3103 return gl::error(GL_OUT_OF_MEMORY, false);
3104 }
3105
3106 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3107 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3108
Geoff Lang125deab2013-08-09 13:34:16 -04003109 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04003110 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003111 {
3112 return false;
3113 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003114 }
3115
Geoff Lang42477a42013-09-17 17:07:02 -04003116 invalidateFramebufferSwizzles(drawTarget);
3117
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003118 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003119}
3120
3121void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3122 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3123{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003124 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003125 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003126
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003127 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3128
3129 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003130 {
3131 gl::Rectangle area;
3132 area.x = x;
3133 area.y = y;
3134 area.width = width;
3135 area.height = height;
3136
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003137 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3138 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003139
Geoff Langea228632013-07-30 15:17:12 -04003140 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003141 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003142}
3143
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003144Image *Renderer11::createImage()
3145{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003146 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003147}
3148
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003149void Renderer11::generateMipmap(Image *dest, Image *src)
3150{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003151 Image11 *dest11 = Image11::makeImage11(dest);
3152 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003153 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003154}
3155
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003156TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3157{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003158 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3159 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003160}
3161
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003162TextureStorage *Renderer11::createTextureStorage2D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003163{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003164 return new TextureStorage11_2D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003165}
3166
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003167TextureStorage *Renderer11::createTextureStorageCube(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, int size)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003168{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003169 return new TextureStorage11_Cube(this, baseLevel, maxLevel, internalformat, renderTarget, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003170}
3171
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003172TextureStorage *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 +00003173{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003174 return new TextureStorage11_3D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003175}
3176
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003177TextureStorage *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 +00003178{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003179 return new TextureStorage11_2DArray(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003180}
3181
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003182void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3183 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3184 GLint packAlignment, void *pixels)
3185{
3186 D3D11_TEXTURE2D_DESC textureDesc;
3187 texture->GetDesc(&textureDesc);
3188
3189 D3D11_TEXTURE2D_DESC stagingDesc;
3190 stagingDesc.Width = area.width;
3191 stagingDesc.Height = area.height;
3192 stagingDesc.MipLevels = 1;
3193 stagingDesc.ArraySize = 1;
3194 stagingDesc.Format = textureDesc.Format;
3195 stagingDesc.SampleDesc.Count = 1;
3196 stagingDesc.SampleDesc.Quality = 0;
3197 stagingDesc.Usage = D3D11_USAGE_STAGING;
3198 stagingDesc.BindFlags = 0;
3199 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3200 stagingDesc.MiscFlags = 0;
3201
3202 ID3D11Texture2D* stagingTex = NULL;
3203 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3204 if (FAILED(result))
3205 {
3206 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3207 return;
3208 }
3209
3210 ID3D11Texture2D* srcTex = NULL;
3211 if (textureDesc.SampleDesc.Count > 1)
3212 {
3213 D3D11_TEXTURE2D_DESC resolveDesc;
3214 resolveDesc.Width = textureDesc.Width;
3215 resolveDesc.Height = textureDesc.Height;
3216 resolveDesc.MipLevels = 1;
3217 resolveDesc.ArraySize = 1;
3218 resolveDesc.Format = textureDesc.Format;
3219 resolveDesc.SampleDesc.Count = 1;
3220 resolveDesc.SampleDesc.Quality = 0;
3221 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3222 resolveDesc.BindFlags = 0;
3223 resolveDesc.CPUAccessFlags = 0;
3224 resolveDesc.MiscFlags = 0;
3225
3226 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3227 if (FAILED(result))
3228 {
3229 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003230 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003231 return;
3232 }
3233
3234 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3235 subResource = 0;
3236 }
3237 else
3238 {
3239 srcTex = texture;
3240 srcTex->AddRef();
3241 }
3242
3243 D3D11_BOX srcBox;
3244 srcBox.left = area.x;
3245 srcBox.right = area.x + area.width;
3246 srcBox.top = area.y;
3247 srcBox.bottom = area.y + area.height;
3248 srcBox.front = 0;
3249 srcBox.back = 1;
3250
3251 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3252
Geoff Langea228632013-07-30 15:17:12 -04003253 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003254
3255 D3D11_MAPPED_SUBRESOURCE mapping;
3256 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3257
3258 unsigned char *source;
3259 int inputPitch;
3260 if (packReverseRowOrder)
3261 {
3262 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3263 inputPitch = -static_cast<int>(mapping.RowPitch);
3264 }
3265 else
3266 {
3267 source = static_cast<unsigned char*>(mapping.pData);
3268 inputPitch = static_cast<int>(mapping.RowPitch);
3269 }
3270
Geoff Lang697ad3e2013-06-04 10:11:28 -04003271 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003272
Geoff Lang005df412013-10-16 14:12:50 -04003273 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003274 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3275 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3276
3277 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3278
3279 if (sourceFormat == format && sourceType == type)
3280 {
3281 // Direct copy possible
3282 unsigned char *dest = static_cast<unsigned char*>(pixels);
3283 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003284 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003285 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003286 }
3287 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003288 else
3289 {
Geoff Lang005df412013-10-16 14:12:50 -04003290 GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003291 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3292
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003293 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003294 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003295 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003296 // Fast copy is possible through some special function
3297 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003298 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003299 for (int x = 0; x < area.width; x++)
3300 {
3301 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3302 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3303
3304 fastCopyFunc(src, dest);
3305 }
3306 }
3307 }
3308 else
3309 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003310 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003311 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3312
3313 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3314 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3315 sizeof(temp) >= sizeof(gl::ColorUI) &&
3316 sizeof(temp) >= sizeof(gl::ColorI));
3317
3318 for (int y = 0; y < area.height; y++)
3319 {
3320 for (int x = 0; x < area.width; x++)
3321 {
3322 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3323 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3324
3325 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3326 // will not allow the copy otherwise.
3327 readFunc(src, temp);
3328 writeFunc(temp, dest);
3329 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003330 }
3331 }
3332 }
3333
3334 mDeviceContext->Unmap(stagingTex, 0);
3335
Geoff Langea228632013-07-30 15:17:12 -04003336 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003337}
3338
Geoff Lang758d5b22013-06-11 11:42:50 -04003339bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003340 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3341 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003342{
Geoff Lang975af372013-06-12 11:19:22 -04003343 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3344 // it should never be the case that both color and depth/stencil need to be blitted at
3345 // at the same time.
3346 ASSERT(colorBlit != (depthBlit || stencilBlit));
3347
Geoff Langc1f51be2013-06-11 11:49:14 -04003348 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003349
Geoff Lang4d782732013-07-22 10:44:18 -04003350 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3351 if (!drawRenderTarget)
3352 {
3353 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3354 return gl::error(GL_OUT_OF_MEMORY, false);
3355 }
3356
3357 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3358 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3359 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3360 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3361
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003362 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3363 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003364 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003365 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003366 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003367 }
3368
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003369 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003370 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003371 unsigned int readSubresource = 0;
3372 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003373 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003374 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3375 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003376
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003377 if (unresolvedTexture)
3378 {
3379 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3380 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003381
Geoff Langea228632013-07-30 15:17:12 -04003382 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003383
Geoff Lang5c9a29a2014-02-11 10:26:24 -05003384 HRESULT hresult = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3385 if (FAILED(hresult))
Geoff Langc1f51be2013-06-11 11:49:14 -04003386 {
Geoff Langea228632013-07-30 15:17:12 -04003387 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003388 return gl::error(GL_OUT_OF_MEMORY, false);
3389 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003390 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003391 }
3392 else
3393 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003394 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003395 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003396 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003397 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003398 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003399 }
3400
Geoff Lang4d782732013-07-22 10:44:18 -04003401 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003402 {
Geoff Lang4d782732013-07-22 10:44:18 -04003403 SafeRelease(readTexture);
3404 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003405 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003406 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003407 }
3408
Geoff Lang125deab2013-08-09 13:34:16 -04003409 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3410 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3411
3412 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3413
3414 bool wholeBufferCopy = !scissorNeeded &&
3415 readRect.x == 0 && readRect.width == readSize.width &&
3416 readRect.y == 0 && readRect.height == readSize.height &&
3417 drawRect.x == 0 && drawRect.width == drawSize.width &&
3418 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003419
Geoff Langc1f51be2013-06-11 11:49:14 -04003420 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003421
Geoff Lang1cd1b212014-02-11 09:42:27 -05003422 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003423
3424 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3425 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3426 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3427 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3428
3429 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3430 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3431 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3432
Geoff Langc1f51be2013-06-11 11:49:14 -04003433 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003434 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3435 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003436 {
Geoff Lang125deab2013-08-09 13:34:16 -04003437 UINT dstX = drawRect.x;
3438 UINT dstY = drawRect.y;
3439
Geoff Langc1f51be2013-06-11 11:49:14 -04003440 D3D11_BOX readBox;
3441 readBox.left = readRect.x;
3442 readBox.right = readRect.x + readRect.width;
3443 readBox.top = readRect.y;
3444 readBox.bottom = readRect.y + readRect.height;
3445 readBox.front = 0;
3446 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003447
Geoff Lang125deab2013-08-09 13:34:16 -04003448 if (scissorNeeded)
3449 {
3450 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3451 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3452
3453 if (drawRect.x < scissor->x)
3454 {
3455 dstX = scissor->x;
3456 readBox.left += (scissor->x - drawRect.x);
3457 }
3458 if (drawRect.y < scissor->y)
3459 {
3460 dstY = scissor->y;
3461 readBox.top += (scissor->y - drawRect.y);
3462 }
3463 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3464 {
3465 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3466 }
3467 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3468 {
3469 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3470 }
3471 }
3472
Geoff Langc1f51be2013-06-11 11:49:14 -04003473 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3474 // We also require complete framebuffer copies for depth-stencil blit.
3475 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003476
Geoff Lang125deab2013-08-09 13:34:16 -04003477 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003478 readTexture, readSubresource, pSrcBox);
3479 result = true;
3480 }
3481 else
3482 {
3483 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003484 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003485
Geoff Lang975af372013-06-12 11:19:22 -04003486 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003487 {
Geoff Lang975af372013-06-12 11:19:22 -04003488 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003489 drawTexture, drawSubresource, drawArea, drawSize,
3490 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003491 }
3492 else if (depthBlit)
3493 {
Geoff Lang125deab2013-08-09 13:34:16 -04003494 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3495 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003496 }
3497 else if (stencilBlit)
3498 {
3499 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003500 drawTexture, drawSubresource, drawArea, drawSize,
3501 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003502 }
3503 else
3504 {
Geoff Lang685806d2013-06-12 11:16:36 -04003505 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003506 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3507 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003508 }
3509 }
3510
3511 SafeRelease(readTexture);
3512 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003513
3514 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003515}
3516
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003517ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3518{
3519 D3D11_TEXTURE2D_DESC textureDesc;
3520 source->GetDesc(&textureDesc);
3521
3522 if (textureDesc.SampleDesc.Count > 1)
3523 {
3524 D3D11_TEXTURE2D_DESC resolveDesc;
3525 resolveDesc.Width = textureDesc.Width;
3526 resolveDesc.Height = textureDesc.Height;
3527 resolveDesc.MipLevels = 1;
3528 resolveDesc.ArraySize = 1;
3529 resolveDesc.Format = textureDesc.Format;
3530 resolveDesc.SampleDesc.Count = 1;
3531 resolveDesc.SampleDesc.Quality = 0;
3532 resolveDesc.Usage = textureDesc.Usage;
3533 resolveDesc.BindFlags = textureDesc.BindFlags;
3534 resolveDesc.CPUAccessFlags = 0;
3535 resolveDesc.MiscFlags = 0;
3536
3537 ID3D11Texture2D *resolveTexture = NULL;
3538 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3539 if (FAILED(result))
3540 {
3541 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3542 return NULL;
3543 }
3544
3545 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3546 return resolveTexture;
3547 }
3548 else
3549 {
3550 source->AddRef();
3551 return source;
3552 }
3553}
3554
Geoff Lang42477a42013-09-17 17:07:02 -04003555void Renderer11::invalidateRenderbufferSwizzles(gl::Renderbuffer *renderBuffer, int mipLevel)
3556{
3557 TextureStorage *texStorage = renderBuffer->getTextureStorage();
3558 if (texStorage)
3559 {
3560 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3561 if (!texStorage11)
3562 {
3563 ERR("texture storage pointer unexpectedly null.");
3564 return;
3565 }
3566
3567 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3568 }
3569}
3570
3571void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3572{
3573 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3574 {
3575 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
3576 if (colorbuffer)
3577 {
3578 invalidateRenderbufferSwizzles(colorbuffer, framebuffer->getColorbufferMipLevel(colorAttachment));
3579 }
3580 }
3581
3582 gl::Renderbuffer *depthBuffer = framebuffer->getDepthbuffer();
3583 if (depthBuffer)
3584 {
3585 invalidateRenderbufferSwizzles(depthBuffer, framebuffer->getDepthbufferMipLevel());
3586 }
3587
3588 gl::Renderbuffer *stencilBuffer = framebuffer->getStencilbuffer();
3589 if (stencilBuffer)
3590 {
3591 invalidateRenderbufferSwizzles(stencilBuffer, framebuffer->getStencilbufferMipLevel());
3592 }
3593}
3594
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003595bool Renderer11::getLUID(LUID *adapterLuid) const
3596{
3597 adapterLuid->HighPart = 0;
3598 adapterLuid->LowPart = 0;
3599
3600 if (!mDxgiAdapter)
3601 {
3602 return false;
3603 }
3604
3605 DXGI_ADAPTER_DESC adapterDesc;
3606 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3607 {
3608 return false;
3609 }
3610
3611 *adapterLuid = adapterDesc.AdapterLuid;
3612 return true;
3613}
3614
Geoff Lang005df412013-10-16 14:12:50 -04003615GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003616{
3617 int clientVersion = getCurrentClientVersion();
3618 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3619}
3620
Jamie Madill95ffb862014-01-29 09:26:59 -05003621rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3622{
3623 return gl_d3d11::GetVertexConversionType(vertexFormat);
3624}
3625
3626GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3627{
3628 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3629}
3630
Geoff Lang61e49a52013-05-29 10:22:58 -04003631Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3632{
3633 MultisampleSupportInfo supportInfo = { 0 };
3634
3635 UINT formatSupport;
3636 HRESULT result;
3637
3638 result = mDevice->CheckFormatSupport(format, &formatSupport);
3639 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3640 {
3641 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3642 {
3643 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3644 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3645 {
3646 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3647 }
3648 else
3649 {
3650 supportInfo.qualityLevels[i - 1] = 0;
3651 }
3652 }
3653 }
3654
3655 return supportInfo;
3656}
3657
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003658}