blob: e73b20d479033458ee76fb688508708a662e846a [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 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001154 if (indexInfo->storage)
1155 {
1156 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1157 {
1158 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1159 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1160
Geoff Lang9c53f1e2014-01-08 13:41:47 -05001161 mDeviceContext->IASetIndexBuffer(storage->getBuffer(BUFFER_USAGE_INDEX), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001162
1163 mAppliedIBSerial = 0;
1164 mAppliedStorageIBSerial = storage->getSerial();
1165 mAppliedIBOffset = indexInfo->startOffset;
1166 }
1167 }
1168 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001169 {
1170 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1171
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001172 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001173
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001174 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001175 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001176 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001177 }
1178 }
1179
1180 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001181}
1182
Geoff Langeeba6e12014-02-03 13:12:30 -05001183void Renderer11::applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[])
1184{
1185 ID3D11Buffer* d3dBuffers[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1186 UINT d3dOffsets[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1187 bool requiresUpdate = false;
1188 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1189 {
1190 if (transformFeedbackBuffers[i])
1191 {
1192 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(transformFeedbackBuffers[i]->getStorage());
1193 ID3D11Buffer *buffer = storage->getBuffer(BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK);
1194
1195 d3dBuffers[i] = buffer;
1196 d3dOffsets[i] = (mAppliedTFBuffers[i] != buffer) ? static_cast<UINT>(offsets[i]) : -1;
1197 }
1198 else
1199 {
1200 d3dBuffers[i] = NULL;
1201 d3dOffsets[i] = 0;
1202 }
1203
1204 if (d3dBuffers[i] != mAppliedTFBuffers[i] || offsets[i] != mAppliedTFOffsets[i])
1205 {
1206 requiresUpdate = true;
1207 }
1208 }
1209
1210 if (requiresUpdate)
1211 {
1212 mDeviceContext->SOSetTargets(ArraySize(d3dBuffers), d3dBuffers, d3dOffsets);
1213 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1214 {
1215 mAppliedTFBuffers[i] = d3dBuffers[i];
1216 mAppliedTFOffsets[i] = offsets[i];
1217 }
1218 }
1219}
1220
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001221void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1222{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001223 if (mode == GL_LINE_LOOP)
1224 {
1225 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1226 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001227 else if (mode == GL_TRIANGLE_FAN)
1228 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001229 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001230 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001231 else if (instances > 0)
1232 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001233 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001234 }
1235 else
1236 {
1237 mDeviceContext->Draw(count, 0);
1238 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001239}
1240
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001241void 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 +00001242{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001243 if (mode == GL_LINE_LOOP)
1244 {
1245 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1246 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001247 else if (mode == GL_TRIANGLE_FAN)
1248 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001249 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1250 }
1251 else if (instances > 0)
1252 {
1253 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001254 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001255 else
1256 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001257 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001258 }
1259}
1260
1261void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1262{
1263 // Get the raw indices for an indexed draw
1264 if (type != GL_NONE && elementArrayBuffer)
1265 {
1266 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001267 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001268 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001269 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001270 }
1271
1272 if (!mLineLoopIB)
1273 {
1274 mLineLoopIB = new StreamingIndexBufferInterface(this);
1275 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1276 {
1277 delete mLineLoopIB;
1278 mLineLoopIB = NULL;
1279
1280 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001281 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001282 }
1283 }
1284
Geoff Lang57e713e2013-07-31 17:01:58 -04001285 // Checked by Renderer11::applyPrimitiveType
1286 ASSERT(count >= 0);
1287
1288 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001289 {
1290 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1291 return gl::error(GL_OUT_OF_MEMORY);
1292 }
1293
Geoff Lang57e713e2013-07-31 17:01:58 -04001294 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001295 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1296 {
1297 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001298 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001299 }
1300
1301 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001302 unsigned int offset;
1303 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001304 {
1305 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001306 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001307 }
1308
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001309 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001310 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001311
1312 switch (type)
1313 {
1314 case GL_NONE: // Non-indexed draw
1315 for (int i = 0; i < count; i++)
1316 {
1317 data[i] = i;
1318 }
1319 data[count] = 0;
1320 break;
1321 case GL_UNSIGNED_BYTE:
1322 for (int i = 0; i < count; i++)
1323 {
1324 data[i] = static_cast<const GLubyte*>(indices)[i];
1325 }
1326 data[count] = static_cast<const GLubyte*>(indices)[0];
1327 break;
1328 case GL_UNSIGNED_SHORT:
1329 for (int i = 0; i < count; i++)
1330 {
1331 data[i] = static_cast<const GLushort*>(indices)[i];
1332 }
1333 data[count] = static_cast<const GLushort*>(indices)[0];
1334 break;
1335 case GL_UNSIGNED_INT:
1336 for (int i = 0; i < count; i++)
1337 {
1338 data[i] = static_cast<const GLuint*>(indices)[i];
1339 }
1340 data[count] = static_cast<const GLuint*>(indices)[0];
1341 break;
1342 default: UNREACHABLE();
1343 }
1344
1345 if (!mLineLoopIB->unmapBuffer())
1346 {
1347 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001348 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001349 }
1350
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001351 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001352 {
1353 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1354
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001355 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001356 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001357 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001358 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001359 }
1360
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001361 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001362}
1363
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001364void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001365{
1366 // Get the raw indices for an indexed draw
1367 if (type != GL_NONE && elementArrayBuffer)
1368 {
1369 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001370 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001371 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001372 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001373 }
1374
1375 if (!mTriangleFanIB)
1376 {
1377 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1378 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1379 {
1380 delete mTriangleFanIB;
1381 mTriangleFanIB = NULL;
1382
1383 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001384 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001385 }
1386 }
1387
Geoff Lang57e713e2013-07-31 17:01:58 -04001388 // Checked by Renderer11::applyPrimitiveType
1389 ASSERT(count >= 3);
1390
Geoff Langeadfd572013-07-09 15:55:07 -04001391 const unsigned int numTris = count - 2;
1392
Geoff Lang57e713e2013-07-31 17:01:58 -04001393 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001394 {
1395 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1396 return gl::error(GL_OUT_OF_MEMORY);
1397 }
1398
1399 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001400 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1401 {
1402 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001403 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001404 }
1405
1406 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001407 unsigned int offset;
1408 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001409 {
1410 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001411 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001412 }
1413
1414 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001415 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001416
1417 switch (type)
1418 {
1419 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001420 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001421 {
1422 data[i*3 + 0] = 0;
1423 data[i*3 + 1] = i + 1;
1424 data[i*3 + 2] = i + 2;
1425 }
1426 break;
1427 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001428 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001429 {
1430 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1431 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1432 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1433 }
1434 break;
1435 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001436 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001437 {
1438 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1439 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1440 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1441 }
1442 break;
1443 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001444 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001445 {
1446 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1447 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1448 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1449 }
1450 break;
1451 default: UNREACHABLE();
1452 }
1453
1454 if (!mTriangleFanIB->unmapBuffer())
1455 {
1456 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001457 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001458 }
1459
1460 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1461 {
1462 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1463
1464 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1465 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001466 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001467 mAppliedIBOffset = indexBufferOffset;
1468 }
1469
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001470 if (instances > 0)
1471 {
1472 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1473 }
1474 else
1475 {
1476 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1477 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001478}
1479
Jamie Madillc5a83002014-02-14 16:41:25 -05001480void Renderer11::applyShaders(gl::ProgramBinary *programBinary, bool rasterizerDiscard, const gl::VertexFormat inputLayout[])
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001481{
Jamie Madillc5a83002014-02-14 16:41:25 -05001482 ShaderExecutable *vertexExe = programBinary->getVertexExecutableForInputLayout(inputLayout);
Jamie Madill6246dc82014-01-29 09:26:47 -05001483 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
1484 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001485
Jamie Madill6246dc82014-01-29 09:26:47 -05001486 ID3D11VertexShader *vertexShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader() : NULL);
1487 ID3D11PixelShader *pixelShader = (pixelExe ? ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader() : NULL);
1488 ID3D11GeometryShader *geometryShader = (geometryExe ? ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader() : NULL);
1489
1490 // Skip GS if we aren't drawing points
1491 if (!mCurRasterState.pointDrawMode)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001492 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001493 geometryShader = NULL;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001494 }
1495
Geoff Lang0550d032014-01-30 11:29:07 -05001496 // Skip pixel shader if we're doing rasterizer discard.
1497 if (rasterizerDiscard)
1498 {
1499 pixelShader = NULL;
1500 }
1501
Jamie Madill6246dc82014-01-29 09:26:47 -05001502 bool dirtyUniforms = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001503
Jamie Madill6246dc82014-01-29 09:26:47 -05001504 if (vertexShader != mAppliedVertexShader)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001505 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001506 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1507 mAppliedVertexShader = vertexShader;
1508 dirtyUniforms = true;
1509 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001510
Jamie Madill6246dc82014-01-29 09:26:47 -05001511 if (geometryShader != mAppliedGeometryShader)
1512 {
1513 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1514 mAppliedGeometryShader = geometryShader;
1515 dirtyUniforms = true;
1516 }
1517
1518 if (pixelShader != mAppliedPixelShader)
1519 {
1520 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1521 mAppliedPixelShader = pixelShader;
1522 dirtyUniforms = true;
1523 }
1524
1525 if (dirtyUniforms)
1526 {
1527 programBinary->dirtyAllUniforms();
daniel@transgaming.come4991412012-12-20 20:55:34 +00001528 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001529}
1530
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001531void Renderer11::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001532{
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001533 const gl::UniformArray &uniformArray = programBinary.getUniforms();
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001534
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001535 unsigned int totalRegisterCountVS = 0;
1536 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001537
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001538 bool vertexUniformsDirty = false;
1539 bool pixelUniformsDirty = false;
1540
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001541 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001542 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001543 const gl::Uniform &uniform = *uniformArray[uniformIndex];
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001544
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001545 if (uniform.isReferencedByVertexShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001546 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001547 totalRegisterCountVS += uniform.registerCount;
1548 vertexUniformsDirty = (vertexUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001549 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001550
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001551 if (uniform.isReferencedByFragmentShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001552 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001553 totalRegisterCountPS += uniform.registerCount;
1554 pixelUniformsDirty = (pixelUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001555 }
1556 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001557
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001558 const UniformStorage11 *vertexUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getVertexUniformStorage());
1559 const UniformStorage11 *fragmentUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getFragmentUniformStorage());
1560 ASSERT(vertexUniformStorage);
1561 ASSERT(fragmentUniformStorage);
1562
1563 ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
1564 ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001565
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001566 float (*mapVS)[4] = NULL;
1567 float (*mapPS)[4] = NULL;
1568
Shannon Woods5ab33c82013-06-26 15:31:09 -04001569 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1570 {
1571 D3D11_MAPPED_SUBRESOURCE map = {0};
1572 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1573 ASSERT(SUCCEEDED(result));
1574 mapVS = (float(*)[4])map.pData;
1575 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001576
Shannon Woods5ab33c82013-06-26 15:31:09 -04001577 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1578 {
1579 D3D11_MAPPED_SUBRESOURCE map = {0};
1580 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1581 ASSERT(SUCCEEDED(result));
1582 mapPS = (float(*)[4])map.pData;
1583 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001584
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001585 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001586 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001587 gl::Uniform *uniform = uniformArray[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001588
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001589 if (!uniform->isSampler())
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001590 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001591 unsigned int componentCount = (4 - uniform->registerElement);
1592
Jamie Madill71cc91f2013-09-18 12:51:22 -04001593 // 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 -04001594 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001595
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001596 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001597 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001598 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001599 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001600
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001601 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001602 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001603 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001604 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001605 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001606 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001607
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001608 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001609 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001610 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001611 }
1612
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001613 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001614 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001615 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001616 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001617
1618 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1619 {
1620 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1621 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1622 }
1623
1624 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1625 {
1626 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1627 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1628 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001629
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001630 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001631 if (!mDriverConstantBufferVS)
1632 {
1633 D3D11_BUFFER_DESC constantBufferDescription = {0};
1634 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1635 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1636 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1637 constantBufferDescription.CPUAccessFlags = 0;
1638 constantBufferDescription.MiscFlags = 0;
1639 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001640
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001641 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001642 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001643
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001644 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1645 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001646
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001647 if (!mDriverConstantBufferPS)
1648 {
1649 D3D11_BUFFER_DESC constantBufferDescription = {0};
1650 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1651 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1652 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1653 constantBufferDescription.CPUAccessFlags = 0;
1654 constantBufferDescription.MiscFlags = 0;
1655 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001656
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001657 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001658 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001659
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001660 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1661 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001662
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001663 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1664 {
1665 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1666 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1667 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001668
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001669 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1670 {
1671 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1672 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1673 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001674
1675 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001676 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1677 {
1678 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1679 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1680 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001681}
1682
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001683void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001684{
Geoff Langda507fe2013-08-20 12:01:42 -04001685 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001686 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001687}
1688
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001689void Renderer11::markAllStateDirty()
1690{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001691 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1692 {
1693 mAppliedRenderTargetSerials[rtIndex] = 0;
1694 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001695 mAppliedDepthbufferSerial = 0;
1696 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001697 mDepthStencilInitialized = false;
1698 mRenderTargetDescInitialized = false;
1699
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001700 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001701 {
1702 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001703 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001704 }
1705 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1706 {
1707 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001708 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001709 }
1710
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001711 mForceSetBlendState = true;
1712 mForceSetRasterState = true;
1713 mForceSetDepthStencilState = true;
1714 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001715 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001716
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001717 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001718 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001719 mAppliedIBOffset = 0;
1720
Jamie Madill6246dc82014-01-29 09:26:47 -05001721 mAppliedVertexShader = NULL;
1722 mAppliedGeometryShader = NULL;
1723 mAppliedPixelShader = NULL;
Geoff Langeeba6e12014-02-03 13:12:30 -05001724
1725 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1726 {
1727 mAppliedTFBuffers[i] = NULL;
1728 mAppliedTFOffsets[i] = 0;
1729 }
1730
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001731 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1732 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001733
1734 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001735
1736 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1737 {
1738 mCurrentConstantBufferVS[i] = -1;
1739 mCurrentConstantBufferPS[i] = -1;
1740 }
1741
1742 mCurrentVertexConstantBuffer = NULL;
1743 mCurrentPixelConstantBuffer = NULL;
1744 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001745
1746 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001747}
1748
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001749void Renderer11::releaseDeviceResources()
1750{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001751 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001752 mInputLayoutCache.clear();
1753
Geoff Langea228632013-07-30 15:17:12 -04001754 SafeDelete(mVertexDataManager);
1755 SafeDelete(mIndexDataManager);
1756 SafeDelete(mLineLoopIB);
1757 SafeDelete(mTriangleFanIB);
1758 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001759 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001760 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001761
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001762 SafeRelease(mDriverConstantBufferVS);
1763 SafeRelease(mDriverConstantBufferPS);
1764 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001765}
1766
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001767void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001768{
1769 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001770 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001771}
1772
1773bool Renderer11::isDeviceLost()
1774{
1775 return mDeviceLost;
1776}
1777
1778// set notify to true to broadcast a message to all contexts of the device loss
1779bool Renderer11::testDeviceLost(bool notify)
1780{
1781 bool isLost = false;
1782
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001783 // GetRemovedReason is used to test if the device is removed
1784 HRESULT result = mDevice->GetDeviceRemovedReason();
1785 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001786
1787 if (isLost)
1788 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001789 // Log error if this is a new device lost event
1790 if (mDeviceLost == false)
1791 {
1792 ERR("The D3D11 device was removed: 0x%08X", result);
1793 }
1794
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001795 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001796 // we'll probably get this done again by notifyDeviceLost
1797 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001798 // Note that we don't want to clear the device loss status here
1799 // -- this needs to be done by resetDevice
1800 mDeviceLost = true;
1801 if (notify)
1802 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001803 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001804 }
1805 }
1806
1807 return isLost;
1808}
1809
1810bool Renderer11::testDeviceResettable()
1811{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001812 // determine if the device is resettable by creating a dummy device
1813 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001814
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001815 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001816 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001817 return false;
1818 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001819
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001820 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001821 {
1822 D3D_FEATURE_LEVEL_11_0,
1823 D3D_FEATURE_LEVEL_10_1,
1824 D3D_FEATURE_LEVEL_10_0,
1825 };
1826
1827 ID3D11Device* dummyDevice;
1828 D3D_FEATURE_LEVEL dummyFeatureLevel;
1829 ID3D11DeviceContext* dummyContext;
1830
1831 HRESULT result = D3D11CreateDevice(NULL,
1832 D3D_DRIVER_TYPE_HARDWARE,
1833 NULL,
1834 #if defined(_DEBUG)
1835 D3D11_CREATE_DEVICE_DEBUG,
1836 #else
1837 0,
1838 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001839 featureLevels,
1840 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001841 D3D11_SDK_VERSION,
1842 &dummyDevice,
1843 &dummyFeatureLevel,
1844 &dummyContext);
1845
1846 if (!mDevice || FAILED(result))
1847 {
1848 return false;
1849 }
1850
Geoff Langea228632013-07-30 15:17:12 -04001851 SafeRelease(dummyContext);
1852 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001853
1854 return true;
1855}
1856
1857void Renderer11::release()
1858{
1859 releaseDeviceResources();
1860
Geoff Langea228632013-07-30 15:17:12 -04001861 SafeRelease(mDxgiFactory);
1862 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001863
1864 if (mDeviceContext)
1865 {
1866 mDeviceContext->ClearState();
1867 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001868 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001869 }
1870
Geoff Langea228632013-07-30 15:17:12 -04001871 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001872
1873 if (mD3d11Module)
1874 {
1875 FreeLibrary(mD3d11Module);
1876 mD3d11Module = NULL;
1877 }
1878
1879 if (mDxgiModule)
1880 {
1881 FreeLibrary(mDxgiModule);
1882 mDxgiModule = NULL;
1883 }
Geoff Langdad5ed32014-02-10 12:59:17 -05001884
1885 mCompiler.release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001886}
1887
1888bool Renderer11::resetDevice()
1889{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001890 // recreate everything
1891 release();
1892 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001893
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001894 if (result != EGL_SUCCESS)
1895 {
1896 ERR("Could not reinitialize D3D11 device: %08X", result);
1897 return false;
1898 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001899
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001900 mDeviceLost = false;
1901
1902 return true;
1903}
1904
1905DWORD Renderer11::getAdapterVendor() const
1906{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001907 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001908}
1909
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001910std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001911{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001912 std::ostringstream rendererString;
1913
1914 rendererString << mDescription;
1915 rendererString << " Direct3D11";
1916
1917 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1918 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1919
1920 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001921}
1922
1923GUID Renderer11::getAdapterIdentifier() const
1924{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001925 // Use the adapter LUID as our adapter ID
1926 // This number is local to a machine is only guaranteed to be unique between restarts
1927 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1928 GUID adapterId = {0};
1929 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1930 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001931}
1932
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001933bool Renderer11::getBGRATextureSupport() const
1934{
1935 return mBGRATextureSupport;
1936}
1937
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001938bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001939{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001940 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001941}
1942
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001943bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001944{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001945 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001946}
1947
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001948bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001949{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001950 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001951}
1952
1953bool Renderer11::getDepthTextureSupport() const
1954{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001955 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001956}
1957
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001958bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001959{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001960 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001961}
1962
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001963bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001964{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001965 return mFloat32FilterSupport;
1966}
1967
1968bool Renderer11::getFloat32TextureRenderingSupport() const
1969{
1970 return mFloat32RenderSupport;
1971}
1972
1973bool Renderer11::getFloat16TextureSupport() const
1974{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001975 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001976}
1977
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001978bool Renderer11::getFloat16TextureFilteringSupport() const
1979{
1980 return mFloat16FilterSupport;
1981}
1982
1983bool Renderer11::getFloat16TextureRenderingSupport() const
1984{
1985 return mFloat16RenderSupport;
1986}
1987
Geoff Langd42cf4e2013-06-05 16:09:17 -04001988bool Renderer11::getRGB565TextureSupport() const
1989{
1990 return false;
1991}
1992
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001993bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001994{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001995 return false;
1996}
1997
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001998bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001999{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002000 return false;
2001}
2002
Geoff Lang632192d2013-10-04 13:40:46 -04002003bool Renderer11::getRGTextureSupport() const
2004{
2005 return mRGTextureSupport;
2006}
2007
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002008bool Renderer11::getTextureFilterAnisotropySupport() const
2009{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002010 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002011}
2012
2013float Renderer11::getTextureMaxAnisotropy() const
2014{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002015 switch (mFeatureLevel)
2016 {
2017 case D3D_FEATURE_LEVEL_11_0:
2018 return D3D11_MAX_MAXANISOTROPY;
2019 case D3D_FEATURE_LEVEL_10_1:
2020 case D3D_FEATURE_LEVEL_10_0:
2021 return D3D10_MAX_MAXANISOTROPY;
2022 default: UNREACHABLE();
2023 return 0;
2024 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002025}
2026
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002027bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002028{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002029 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002030}
2031
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002032Range Renderer11::getViewportBounds() const
2033{
2034 switch (mFeatureLevel)
2035 {
2036 case D3D_FEATURE_LEVEL_11_0:
2037 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
2038 case D3D_FEATURE_LEVEL_10_1:
2039 case D3D_FEATURE_LEVEL_10_0:
2040 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
2041 default: UNREACHABLE();
2042 return Range(0, 0);
2043 }
2044}
2045
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002046unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002047{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002048 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2049 switch (mFeatureLevel)
2050 {
2051 case D3D_FEATURE_LEVEL_11_0:
2052 case D3D_FEATURE_LEVEL_10_1:
2053 case D3D_FEATURE_LEVEL_10_0:
2054 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
2055 default: UNREACHABLE();
2056 return 0;
2057 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002058}
2059
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002060unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
2061{
2062 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2063}
2064
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002065unsigned int Renderer11::getReservedVertexUniformVectors() const
2066{
Shannon Woods5ab33c82013-06-26 15:31:09 -04002067 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002068}
2069
2070unsigned int Renderer11::getReservedFragmentUniformVectors() const
2071{
Shannon Woods5ab33c82013-06-26 15:31:09 -04002072 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002073}
2074
2075unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002076{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002077 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2078 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2079 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002080}
2081
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002082unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002083{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002084 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2085 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2086 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002087}
2088
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002089unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002090{
2091 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00002092 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
2093 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002094 switch (mFeatureLevel)
2095 {
2096 case D3D_FEATURE_LEVEL_11_0:
2097 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2098 case D3D_FEATURE_LEVEL_10_1:
2099 case D3D_FEATURE_LEVEL_10_0:
2100 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2101 default: UNREACHABLE();
2102 return 0;
2103 }
2104}
2105
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002106unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2107{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002108 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2109 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2110
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002111 switch (mFeatureLevel)
2112 {
2113 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002114 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002115 case D3D_FEATURE_LEVEL_10_1:
2116 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002117 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002118 default: UNREACHABLE();
2119 return 0;
2120 }
2121}
2122
2123unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2124{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002125 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2126 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2127
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002128 switch (mFeatureLevel)
2129 {
2130 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002131 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002132 case D3D_FEATURE_LEVEL_10_1:
2133 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002134 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002135 default: UNREACHABLE();
2136 return 0;
2137 }
2138}
2139
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002140unsigned int Renderer11::getReservedVertexUniformBuffers() const
2141{
2142 // we reserve one buffer for the application uniforms, and one for driver uniforms
2143 return 2;
2144}
2145
2146unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2147{
2148 // we reserve one buffer for the application uniforms, and one for driver uniforms
2149 return 2;
2150}
2151
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002152unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2153{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002154 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2155 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2156
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002157 switch (mFeatureLevel)
2158 {
2159 case D3D_FEATURE_LEVEL_11_0:
2160 return D3D11_SO_BUFFER_SLOT_COUNT;
2161 case D3D_FEATURE_LEVEL_10_1:
2162 case D3D_FEATURE_LEVEL_10_0:
2163 return D3D10_SO_BUFFER_SLOT_COUNT;
2164 default: UNREACHABLE();
2165 return 0;
2166 }
2167}
2168
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002169unsigned int Renderer11::getMaxUniformBufferSize() const
2170{
2171 // Each component is a 4-element vector of 4-byte units (floats)
2172 const unsigned int bytesPerComponent = 4 * sizeof(float);
2173
2174 switch (mFeatureLevel)
2175 {
2176 case D3D_FEATURE_LEVEL_11_0:
2177 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2178 case D3D_FEATURE_LEVEL_10_1:
2179 case D3D_FEATURE_LEVEL_10_0:
2180 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2181 default: UNREACHABLE();
2182 return 0;
2183 }
2184}
2185
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002186bool Renderer11::getNonPower2TextureSupport() const
2187{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002188 switch (mFeatureLevel)
2189 {
2190 case D3D_FEATURE_LEVEL_11_0:
2191 case D3D_FEATURE_LEVEL_10_1:
2192 case D3D_FEATURE_LEVEL_10_0:
2193 return true;
2194 default: UNREACHABLE();
2195 return false;
2196 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002197}
2198
2199bool Renderer11::getOcclusionQuerySupport() const
2200{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002201 switch (mFeatureLevel)
2202 {
2203 case D3D_FEATURE_LEVEL_11_0:
2204 case D3D_FEATURE_LEVEL_10_1:
2205 case D3D_FEATURE_LEVEL_10_0:
2206 return true;
2207 default: UNREACHABLE();
2208 return false;
2209 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002210}
2211
2212bool Renderer11::getInstancingSupport() const
2213{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002214 switch (mFeatureLevel)
2215 {
2216 case D3D_FEATURE_LEVEL_11_0:
2217 case D3D_FEATURE_LEVEL_10_1:
2218 case D3D_FEATURE_LEVEL_10_0:
2219 return true;
2220 default: UNREACHABLE();
2221 return false;
2222 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002223}
2224
2225bool Renderer11::getShareHandleSupport() const
2226{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002227 // We only currently support share handles with BGRA surfaces, because
2228 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002229 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002230 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002231}
2232
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002233bool Renderer11::getDerivativeInstructionSupport() const
2234{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002235 switch (mFeatureLevel)
2236 {
2237 case D3D_FEATURE_LEVEL_11_0:
2238 case D3D_FEATURE_LEVEL_10_1:
2239 case D3D_FEATURE_LEVEL_10_0:
2240 return true;
2241 default: UNREACHABLE();
2242 return false;
2243 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002244}
2245
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002246bool Renderer11::getPostSubBufferSupport() const
2247{
2248 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2249 return false;
2250}
2251
Jamie Madill13a2f852013-12-11 16:35:08 -05002252int Renderer11::getMaxRecommendedElementsIndices() const
2253{
2254 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2255 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2256
2257 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2258 return std::numeric_limits<GLint>::max();
2259}
2260
2261int Renderer11::getMaxRecommendedElementsVertices() const
2262{
2263 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2264 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2265
2266 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2267 return std::numeric_limits<GLint>::max();
2268}
2269
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002270int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002271{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002272 switch (mFeatureLevel)
2273 {
2274 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002275 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002276 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2277 default: UNREACHABLE(); return 0;
2278 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002279}
2280
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002281int Renderer11::getMinorShaderModel() const
2282{
2283 switch (mFeatureLevel)
2284 {
2285 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2286 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2287 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2288 default: UNREACHABLE(); return 0;
2289 }
2290}
2291
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002292float Renderer11::getMaxPointSize() const
2293{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002294 // choose a reasonable maximum. we enforce this in the shader.
2295 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2296 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002297}
2298
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002299int Renderer11::getMaxViewportDimension() const
2300{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002301 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2302 // In our case return the maximum texture size, which is the maximum render buffer size.
2303 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2304 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2305
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002306 switch (mFeatureLevel)
2307 {
2308 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002309 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002310 case D3D_FEATURE_LEVEL_10_1:
2311 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002312 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002313 default: UNREACHABLE();
2314 return 0;
2315 }
2316}
2317
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002318int Renderer11::getMaxTextureWidth() const
2319{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002320 switch (mFeatureLevel)
2321 {
2322 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2323 case D3D_FEATURE_LEVEL_10_1:
2324 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2325 default: UNREACHABLE(); return 0;
2326 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002327}
2328
2329int Renderer11::getMaxTextureHeight() const
2330{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002331 switch (mFeatureLevel)
2332 {
2333 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2334 case D3D_FEATURE_LEVEL_10_1:
2335 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2336 default: UNREACHABLE(); return 0;
2337 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002338}
2339
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002340int Renderer11::getMaxTextureDepth() const
2341{
2342 switch (mFeatureLevel)
2343 {
2344 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2345 case D3D_FEATURE_LEVEL_10_1:
2346 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2347 default: UNREACHABLE(); return 0;
2348 }
2349}
2350
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002351int Renderer11::getMaxTextureArrayLayers() const
2352{
2353 switch (mFeatureLevel)
2354 {
2355 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2356 case D3D_FEATURE_LEVEL_10_1:
2357 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2358 default: UNREACHABLE(); return 0;
2359 }
2360}
2361
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002362bool Renderer11::get32BitIndexSupport() const
2363{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002364 switch (mFeatureLevel)
2365 {
2366 case D3D_FEATURE_LEVEL_11_0:
2367 case D3D_FEATURE_LEVEL_10_1:
2368 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2369 default: UNREACHABLE(); return false;
2370 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002371}
2372
2373int Renderer11::getMinSwapInterval() const
2374{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002375 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002376}
2377
2378int Renderer11::getMaxSwapInterval() const
2379{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002380 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002381}
2382
2383int Renderer11::getMaxSupportedSamples() const
2384{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002385 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002386}
2387
Geoff Lang005df412013-10-16 14:12:50 -04002388GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002389{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002390 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002391 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2392 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2393}
2394
Geoff Lang005df412013-10-16 14:12:50 -04002395GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002396{
2397 unsigned int numCounts = 0;
2398
2399 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002400 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2401 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002402 {
2403 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2404 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2405
2406 if (iter != mMultisampleSupportMap.end())
2407 {
2408 const MultisampleSupportInfo& info = iter->second;
2409 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2410 {
2411 if (info.qualityLevels[i] > 0)
2412 {
2413 numCounts++;
2414 }
2415 }
2416 }
2417 }
2418
2419 return numCounts;
2420}
2421
Geoff Lang005df412013-10-16 14:12:50 -04002422void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002423{
2424 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002425 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2426 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2427 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002428 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002429 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002430
2431 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2432 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2433
2434 if (iter != mMultisampleSupportMap.end())
2435 {
2436 const MultisampleSupportInfo& info = iter->second;
2437 int bufPos = 0;
2438 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2439 {
2440 if (info.qualityLevels[i] > 0)
2441 {
2442 params[bufPos++] = i + 1;
2443 }
2444 }
2445 }
2446}
2447
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002448int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2449{
2450 if (requested == 0)
2451 {
2452 return 0;
2453 }
2454
2455 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2456 if (iter != mMultisampleSupportMap.end())
2457 {
2458 const MultisampleSupportInfo& info = iter->second;
2459 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2460 {
2461 if (info.qualityLevels[i] > 0)
2462 {
2463 return i + 1;
2464 }
2465 }
2466 }
2467
2468 return -1;
2469}
2470
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002471unsigned int Renderer11::getMaxRenderTargets() const
2472{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002473 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2474 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2475
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002476 switch (mFeatureLevel)
2477 {
2478 case D3D_FEATURE_LEVEL_11_0:
2479 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2480 case D3D_FEATURE_LEVEL_10_1:
2481 case D3D_FEATURE_LEVEL_10_0:
2482 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2483 default:
2484 UNREACHABLE();
2485 return 1;
2486 }
2487}
2488
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002489bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002490{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002491 if (source && dest)
2492 {
2493 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2494 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2495
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002496 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002497
2498 dest11->invalidateSwizzleCache();
2499
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002500 return true;
2501 }
2502
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002503 return false;
2504}
2505
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002506bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002507{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002508 if (source && dest)
2509 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002510 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2511 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002512
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002513 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002514
2515 dest11->invalidateSwizzleCache();
2516
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002517 return true;
2518 }
2519
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002520 return false;
2521}
2522
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002523bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2524{
2525 if (source && dest)
2526 {
2527 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2528 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2529
2530 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002531
2532 dest11->invalidateSwizzleCache();
2533
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002534 return true;
2535 }
2536
2537 return false;
2538}
2539
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002540bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2541{
2542 if (source && dest)
2543 {
2544 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2545 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2546
2547 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002548
2549 dest11->invalidateSwizzleCache();
2550
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002551 return true;
2552 }
2553
2554 return false;
2555}
2556
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002557bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002558 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002559{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002560 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002561 if (!colorbuffer)
2562 {
2563 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002564 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002565 }
2566
2567 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2568 if (!sourceRenderTarget)
2569 {
2570 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002571 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002572 }
2573
2574 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2575 if (!source)
2576 {
2577 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002578 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002579 }
2580
2581 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2582 if (!storage11)
2583 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002584 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002585 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002586 }
2587
2588 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2589 if (!destRenderTarget)
2590 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002591 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002592 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002593 }
2594
2595 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2596 if (!dest)
2597 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002598 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002599 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002600 }
2601
Geoff Langb86b9792013-06-04 16:32:05 -04002602 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2603 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002604
Geoff Langb86b9792013-06-04 16:32:05 -04002605 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2606 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002607
Geoff Langb86b9792013-06-04 16:32:05 -04002608 // Use nearest filtering because source and destination are the same size for the direct
2609 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002610 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002611 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002612
Geoff Lang42477a42013-09-17 17:07:02 -04002613 storage11->invalidateSwizzleCacheLevel(level);
2614
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002615 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002616}
2617
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002618bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002619 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002620{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002621 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002622 if (!colorbuffer)
2623 {
2624 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002625 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002626 }
2627
2628 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2629 if (!sourceRenderTarget)
2630 {
2631 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002632 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002633 }
2634
2635 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2636 if (!source)
2637 {
2638 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002639 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002640 }
2641
2642 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2643 if (!storage11)
2644 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002645 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002646 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002647 }
2648
Nicolas Capensb13f8662013-06-04 13:30:19 -04002649 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002650 if (!destRenderTarget)
2651 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002652 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002653 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002654 }
2655
2656 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2657 if (!dest)
2658 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002659 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002660 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002661 }
2662
Geoff Langb86b9792013-06-04 16:32:05 -04002663 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2664 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002665
Geoff Langb86b9792013-06-04 16:32:05 -04002666 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2667 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002668
Geoff Langb86b9792013-06-04 16:32:05 -04002669 // Use nearest filtering because source and destination are the same size for the direct
2670 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002671 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002672 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002673
Geoff Lang42477a42013-09-17 17:07:02 -04002674 storage11->invalidateSwizzleCacheLevel(level);
2675
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002676 return ret;
2677}
2678
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002679bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2680 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2681{
2682 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2683 if (!colorbuffer)
2684 {
2685 ERR("Failed to retrieve the color buffer from the frame buffer.");
2686 return gl::error(GL_OUT_OF_MEMORY, false);
2687 }
2688
2689 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2690 if (!sourceRenderTarget)
2691 {
2692 ERR("Failed to retrieve the render target from the frame buffer.");
2693 return gl::error(GL_OUT_OF_MEMORY, false);
2694 }
2695
2696 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2697 if (!source)
2698 {
2699 ERR("Failed to retrieve the render target view from the render target.");
2700 return gl::error(GL_OUT_OF_MEMORY, false);
2701 }
2702
2703 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2704 if (!storage11)
2705 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002706 ERR("Failed to retrieve the texture storage from the destination.");
2707 return gl::error(GL_OUT_OF_MEMORY, false);
2708 }
2709
2710 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2711 if (!destRenderTarget)
2712 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002713 ERR("Failed to retrieve the render target from the destination storage.");
2714 return gl::error(GL_OUT_OF_MEMORY, false);
2715 }
2716
2717 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2718 if (!dest)
2719 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002720 ERR("Failed to retrieve the render target view from the destination render target.");
2721 return gl::error(GL_OUT_OF_MEMORY, false);
2722 }
2723
Geoff Langb86b9792013-06-04 16:32:05 -04002724 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2725 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002726
Geoff Langb86b9792013-06-04 16:32:05 -04002727 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2728 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002729
Geoff Langb86b9792013-06-04 16:32:05 -04002730 // Use nearest filtering because source and destination are the same size for the direct
2731 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002732 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002733 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002734
Geoff Lang42477a42013-09-17 17:07:02 -04002735 storage11->invalidateSwizzleCacheLevel(level);
2736
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002737 return ret;
2738}
2739
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002740bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2741 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2742{
2743 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2744 if (!colorbuffer)
2745 {
2746 ERR("Failed to retrieve the color buffer from the frame buffer.");
2747 return gl::error(GL_OUT_OF_MEMORY, false);
2748 }
2749
2750 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2751 if (!sourceRenderTarget)
2752 {
2753 ERR("Failed to retrieve the render target from the frame buffer.");
2754 return gl::error(GL_OUT_OF_MEMORY, false);
2755 }
2756
2757 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2758 if (!source)
2759 {
2760 ERR("Failed to retrieve the render target view from the render target.");
2761 return gl::error(GL_OUT_OF_MEMORY, false);
2762 }
2763
2764 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2765 if (!storage11)
2766 {
Geoff Langea228632013-07-30 15:17:12 -04002767 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002768 ERR("Failed to retrieve the texture storage from the destination.");
2769 return gl::error(GL_OUT_OF_MEMORY, false);
2770 }
2771
2772 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2773 if (!destRenderTarget)
2774 {
Geoff Langea228632013-07-30 15:17:12 -04002775 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002776 ERR("Failed to retrieve the render target from the destination storage.");
2777 return gl::error(GL_OUT_OF_MEMORY, false);
2778 }
2779
2780 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2781 if (!dest)
2782 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002783 ERR("Failed to retrieve the render target view from the destination render target.");
2784 return gl::error(GL_OUT_OF_MEMORY, false);
2785 }
2786
Geoff Langb86b9792013-06-04 16:32:05 -04002787 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2788 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002789
Geoff Langb86b9792013-06-04 16:32:05 -04002790 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2791 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002792
Geoff Langb86b9792013-06-04 16:32:05 -04002793 // Use nearest filtering because source and destination are the same size for the direct
2794 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002795 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002796 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002797
Geoff Lang42477a42013-09-17 17:07:02 -04002798 storage11->invalidateSwizzleCacheLevel(level);
2799
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002800 return ret;
2801}
2802
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002803void Renderer11::unapplyRenderTargets()
2804{
2805 setOneTimeRenderTarget(NULL);
2806}
2807
2808void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2809{
2810 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2811
2812 rtvArray[0] = renderTargetView;
2813
2814 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2815
2816 // Do not preserve the serial for this one-time-use render target
2817 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2818 {
2819 mAppliedRenderTargetSerials[rtIndex] = 0;
2820 }
2821}
2822
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002823RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2824{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002825 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002826 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002827
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002828 if (depth)
2829 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002830 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002831 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002832 swapChain11->getDepthStencilTexture(),
2833 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002834 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002835 }
2836 else
2837 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002838 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002839 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002840 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002841 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002842 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002843 }
2844 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002845}
2846
Geoff Langa2d97f12013-06-11 11:44:02 -04002847RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002848{
Geoff Langa2d97f12013-06-11 11:44:02 -04002849 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002850 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002851}
2852
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002853ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002854{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002855 ShaderExecutable11 *executable = NULL;
2856
2857 switch (type)
2858 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002859 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002860 {
2861 ID3D11VertexShader *vshader = NULL;
2862 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2863 ASSERT(SUCCEEDED(result));
2864
2865 if (vshader)
2866 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002867 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002868 }
2869 }
2870 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002871 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002872 {
2873 ID3D11PixelShader *pshader = NULL;
2874 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2875 ASSERT(SUCCEEDED(result));
2876
2877 if (pshader)
2878 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002879 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002880 }
2881 }
2882 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002883 case rx::SHADER_GEOMETRY:
2884 {
2885 ID3D11GeometryShader *gshader = NULL;
2886 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2887 ASSERT(SUCCEEDED(result));
2888
2889 if (gshader)
2890 {
2891 executable = new ShaderExecutable11(function, length, gshader);
2892 }
2893 }
2894 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002895 default:
2896 UNREACHABLE();
2897 break;
2898 }
2899
2900 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002901}
2902
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002903ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002904{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002905 const char *profile = NULL;
2906
2907 switch (type)
2908 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002909 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002910 profile = "vs_4_0";
2911 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002912 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002913 profile = "ps_4_0";
2914 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002915 case rx::SHADER_GEOMETRY:
2916 profile = "gs_4_0";
2917 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002918 default:
2919 UNREACHABLE();
2920 return NULL;
2921 }
2922
Geoff Langdad5ed32014-02-10 12:59:17 -05002923 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002924 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002925 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002926 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002927 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002928
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002929 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002930 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002931
2932 return executable;
2933}
2934
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002935rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2936{
2937 return new UniformStorage11(this, storageSize);
2938}
2939
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002940VertexBuffer *Renderer11::createVertexBuffer()
2941{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002942 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002943}
2944
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002945IndexBuffer *Renderer11::createIndexBuffer()
2946{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002947 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002948}
2949
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002950BufferStorage *Renderer11::createBufferStorage()
2951{
2952 return new BufferStorage11(this);
2953}
2954
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002955QueryImpl *Renderer11::createQuery(GLenum type)
2956{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002957 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002958}
2959
2960FenceImpl *Renderer11::createFence()
2961{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002962 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002963}
2964
Geoff Lang005df412013-10-16 14:12:50 -04002965bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002966{
Jamie Madill4461f092013-10-10 15:10:39 -04002967 int clientVersion = getCurrentClientVersion();
2968
2969 // We only support buffer to texture copies in ES3
2970 if (clientVersion <= 2)
2971 {
2972 return false;
2973 }
2974
2975 // sRGB formats do not work with D3D11 buffer SRVs
2976 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2977 {
2978 return false;
2979 }
2980
2981 // We cannot support direct copies to non-color-renderable formats
2982 if (!gl::IsColorRenderingSupported(internalFormat, this))
2983 {
2984 return false;
2985 }
2986
2987 // We skip all 3-channel formats since sometimes format support is missing
2988 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2989 {
2990 return false;
2991 }
2992
2993 // We don't support formats which we can't represent without conversion
2994 if (getNativeTextureFormat(internalFormat) != internalFormat)
2995 {
2996 return false;
2997 }
2998
2999 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04003000}
3001
Jamie Madilla21eea32013-09-18 14:36:25 -04003002bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
3003 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
3004{
Jamie Madill0e0510f2013-10-10 15:46:23 -04003005 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04003006 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
3007}
3008
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003009bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003010{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003011 ASSERT(colorbuffer != NULL);
3012
3013 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
3014 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003015 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003016 *subresourceIndex = renderTarget->getSubresourceIndex();
3017
3018 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
3019 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003020 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003021 ID3D11Resource *textureResource = NULL;
3022 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003023
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003024 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003025 {
Geoff Lang8e328842014-02-10 13:11:20 -05003026 HRESULT result = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04003027 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003028
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003029 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003030 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003031 return true;
3032 }
3033 else
3034 {
3035 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
3036 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003037 }
3038 }
3039 }
3040 }
3041
3042 return false;
3043}
3044
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003045bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04003046 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003047{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003048 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003049 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003050 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003051
3052 if (!readBuffer)
3053 {
3054 ERR("Failed to retrieve the read buffer from the read framebuffer.");
3055 return gl::error(GL_OUT_OF_MEMORY, false);
3056 }
3057
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003058 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003059
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003060 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003061 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003062 if (drawTarget->isEnabledColorAttachment(colorAttachment))
3063 {
3064 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
3065
3066 if (!drawBuffer)
3067 {
3068 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
3069 return gl::error(GL_OUT_OF_MEMORY, false);
3070 }
3071
3072 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
3073
Geoff Lang125deab2013-08-09 13:34:16 -04003074 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04003075 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003076 {
3077 return false;
3078 }
3079 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003080 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003081 }
3082
Geoff Lang685806d2013-06-12 11:16:36 -04003083 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003084 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003085 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
3086 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
3087
3088 if (!readBuffer)
3089 {
3090 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
3091 return gl::error(GL_OUT_OF_MEMORY, false);
3092 }
3093
3094 if (!drawBuffer)
3095 {
3096 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
3097 return gl::error(GL_OUT_OF_MEMORY, false);
3098 }
3099
3100 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3101 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3102
Geoff Lang125deab2013-08-09 13:34:16 -04003103 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04003104 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003105 {
3106 return false;
3107 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003108 }
3109
Geoff Lang42477a42013-09-17 17:07:02 -04003110 invalidateFramebufferSwizzles(drawTarget);
3111
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003112 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003113}
3114
3115void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3116 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3117{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003118 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003119 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003120
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003121 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3122
3123 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003124 {
3125 gl::Rectangle area;
3126 area.x = x;
3127 area.y = y;
3128 area.width = width;
3129 area.height = height;
3130
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003131 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3132 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003133
Geoff Langea228632013-07-30 15:17:12 -04003134 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003135 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003136}
3137
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003138Image *Renderer11::createImage()
3139{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003140 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003141}
3142
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003143void Renderer11::generateMipmap(Image *dest, Image *src)
3144{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003145 Image11 *dest11 = Image11::makeImage11(dest);
3146 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003147 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003148}
3149
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003150TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3151{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003152 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3153 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003154}
3155
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003156TextureStorage *Renderer11::createTextureStorage2D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003157{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003158 return new TextureStorage11_2D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003159}
3160
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003161TextureStorage *Renderer11::createTextureStorageCube(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, int size)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003162{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003163 return new TextureStorage11_Cube(this, baseLevel, maxLevel, internalformat, renderTarget, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003164}
3165
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003166TextureStorage *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 +00003167{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003168 return new TextureStorage11_3D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003169}
3170
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003171TextureStorage *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 +00003172{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003173 return new TextureStorage11_2DArray(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003174}
3175
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003176void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3177 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3178 GLint packAlignment, void *pixels)
3179{
3180 D3D11_TEXTURE2D_DESC textureDesc;
3181 texture->GetDesc(&textureDesc);
3182
3183 D3D11_TEXTURE2D_DESC stagingDesc;
3184 stagingDesc.Width = area.width;
3185 stagingDesc.Height = area.height;
3186 stagingDesc.MipLevels = 1;
3187 stagingDesc.ArraySize = 1;
3188 stagingDesc.Format = textureDesc.Format;
3189 stagingDesc.SampleDesc.Count = 1;
3190 stagingDesc.SampleDesc.Quality = 0;
3191 stagingDesc.Usage = D3D11_USAGE_STAGING;
3192 stagingDesc.BindFlags = 0;
3193 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3194 stagingDesc.MiscFlags = 0;
3195
3196 ID3D11Texture2D* stagingTex = NULL;
3197 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3198 if (FAILED(result))
3199 {
3200 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3201 return;
3202 }
3203
3204 ID3D11Texture2D* srcTex = NULL;
3205 if (textureDesc.SampleDesc.Count > 1)
3206 {
3207 D3D11_TEXTURE2D_DESC resolveDesc;
3208 resolveDesc.Width = textureDesc.Width;
3209 resolveDesc.Height = textureDesc.Height;
3210 resolveDesc.MipLevels = 1;
3211 resolveDesc.ArraySize = 1;
3212 resolveDesc.Format = textureDesc.Format;
3213 resolveDesc.SampleDesc.Count = 1;
3214 resolveDesc.SampleDesc.Quality = 0;
3215 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3216 resolveDesc.BindFlags = 0;
3217 resolveDesc.CPUAccessFlags = 0;
3218 resolveDesc.MiscFlags = 0;
3219
3220 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3221 if (FAILED(result))
3222 {
3223 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003224 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003225 return;
3226 }
3227
3228 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3229 subResource = 0;
3230 }
3231 else
3232 {
3233 srcTex = texture;
3234 srcTex->AddRef();
3235 }
3236
3237 D3D11_BOX srcBox;
3238 srcBox.left = area.x;
3239 srcBox.right = area.x + area.width;
3240 srcBox.top = area.y;
3241 srcBox.bottom = area.y + area.height;
3242 srcBox.front = 0;
3243 srcBox.back = 1;
3244
3245 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3246
Geoff Langea228632013-07-30 15:17:12 -04003247 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003248
3249 D3D11_MAPPED_SUBRESOURCE mapping;
3250 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3251
3252 unsigned char *source;
3253 int inputPitch;
3254 if (packReverseRowOrder)
3255 {
3256 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3257 inputPitch = -static_cast<int>(mapping.RowPitch);
3258 }
3259 else
3260 {
3261 source = static_cast<unsigned char*>(mapping.pData);
3262 inputPitch = static_cast<int>(mapping.RowPitch);
3263 }
3264
Geoff Lang697ad3e2013-06-04 10:11:28 -04003265 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003266
Geoff Lang005df412013-10-16 14:12:50 -04003267 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003268 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3269 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3270
3271 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3272
3273 if (sourceFormat == format && sourceType == type)
3274 {
3275 // Direct copy possible
3276 unsigned char *dest = static_cast<unsigned char*>(pixels);
3277 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003278 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003279 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003280 }
3281 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003282 else
3283 {
Geoff Lang005df412013-10-16 14:12:50 -04003284 GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003285 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3286
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003287 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003288 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003289 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003290 // Fast copy is possible through some special function
3291 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003292 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003293 for (int x = 0; x < area.width; x++)
3294 {
3295 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3296 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3297
3298 fastCopyFunc(src, dest);
3299 }
3300 }
3301 }
3302 else
3303 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003304 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003305 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3306
3307 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3308 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3309 sizeof(temp) >= sizeof(gl::ColorUI) &&
3310 sizeof(temp) >= sizeof(gl::ColorI));
3311
3312 for (int y = 0; y < area.height; y++)
3313 {
3314 for (int x = 0; x < area.width; x++)
3315 {
3316 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3317 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3318
3319 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3320 // will not allow the copy otherwise.
3321 readFunc(src, temp);
3322 writeFunc(temp, dest);
3323 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003324 }
3325 }
3326 }
3327
3328 mDeviceContext->Unmap(stagingTex, 0);
3329
Geoff Langea228632013-07-30 15:17:12 -04003330 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003331}
3332
Geoff Lang758d5b22013-06-11 11:42:50 -04003333bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003334 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3335 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003336{
Geoff Lang975af372013-06-12 11:19:22 -04003337 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3338 // it should never be the case that both color and depth/stencil need to be blitted at
3339 // at the same time.
3340 ASSERT(colorBlit != (depthBlit || stencilBlit));
3341
Geoff Langc1f51be2013-06-11 11:49:14 -04003342 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003343
Geoff Lang4d782732013-07-22 10:44:18 -04003344 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3345 if (!drawRenderTarget)
3346 {
3347 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3348 return gl::error(GL_OUT_OF_MEMORY, false);
3349 }
3350
3351 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3352 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3353 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3354 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3355
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003356 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3357 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003358 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003359 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003360 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003361 }
3362
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003363 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003364 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003365 unsigned int readSubresource = 0;
3366 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003367 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003368 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3369 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003370
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003371 if (unresolvedTexture)
3372 {
3373 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3374 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003375
Geoff Langea228632013-07-30 15:17:12 -04003376 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003377
Geoff Lang5c9a29a2014-02-11 10:26:24 -05003378 HRESULT hresult = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3379 if (FAILED(hresult))
Geoff Langc1f51be2013-06-11 11:49:14 -04003380 {
Geoff Langea228632013-07-30 15:17:12 -04003381 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003382 return gl::error(GL_OUT_OF_MEMORY, false);
3383 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003384 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003385 }
3386 else
3387 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003388 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003389 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003390 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003391 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003392 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003393 }
3394
Geoff Lang4d782732013-07-22 10:44:18 -04003395 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003396 {
Geoff Lang4d782732013-07-22 10:44:18 -04003397 SafeRelease(readTexture);
3398 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003399 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003400 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003401 }
3402
Geoff Lang125deab2013-08-09 13:34:16 -04003403 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3404 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3405
3406 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3407
3408 bool wholeBufferCopy = !scissorNeeded &&
3409 readRect.x == 0 && readRect.width == readSize.width &&
3410 readRect.y == 0 && readRect.height == readSize.height &&
3411 drawRect.x == 0 && drawRect.width == drawSize.width &&
3412 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003413
Geoff Langc1f51be2013-06-11 11:49:14 -04003414 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003415
Geoff Lang1cd1b212014-02-11 09:42:27 -05003416 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003417
3418 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3419 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3420 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3421 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3422
3423 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3424 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3425 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3426
Geoff Langc1f51be2013-06-11 11:49:14 -04003427 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003428 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3429 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003430 {
Geoff Lang125deab2013-08-09 13:34:16 -04003431 UINT dstX = drawRect.x;
3432 UINT dstY = drawRect.y;
3433
Geoff Langc1f51be2013-06-11 11:49:14 -04003434 D3D11_BOX readBox;
3435 readBox.left = readRect.x;
3436 readBox.right = readRect.x + readRect.width;
3437 readBox.top = readRect.y;
3438 readBox.bottom = readRect.y + readRect.height;
3439 readBox.front = 0;
3440 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003441
Geoff Lang125deab2013-08-09 13:34:16 -04003442 if (scissorNeeded)
3443 {
3444 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3445 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3446
3447 if (drawRect.x < scissor->x)
3448 {
3449 dstX = scissor->x;
3450 readBox.left += (scissor->x - drawRect.x);
3451 }
3452 if (drawRect.y < scissor->y)
3453 {
3454 dstY = scissor->y;
3455 readBox.top += (scissor->y - drawRect.y);
3456 }
3457 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3458 {
3459 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3460 }
3461 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3462 {
3463 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3464 }
3465 }
3466
Geoff Langc1f51be2013-06-11 11:49:14 -04003467 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3468 // We also require complete framebuffer copies for depth-stencil blit.
3469 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003470
Geoff Lang125deab2013-08-09 13:34:16 -04003471 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003472 readTexture, readSubresource, pSrcBox);
3473 result = true;
3474 }
3475 else
3476 {
3477 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003478 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003479
Geoff Lang975af372013-06-12 11:19:22 -04003480 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003481 {
Geoff Lang975af372013-06-12 11:19:22 -04003482 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003483 drawTexture, drawSubresource, drawArea, drawSize,
3484 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003485 }
3486 else if (depthBlit)
3487 {
Geoff Lang125deab2013-08-09 13:34:16 -04003488 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3489 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003490 }
3491 else if (stencilBlit)
3492 {
3493 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003494 drawTexture, drawSubresource, drawArea, drawSize,
3495 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003496 }
3497 else
3498 {
Geoff Lang685806d2013-06-12 11:16:36 -04003499 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003500 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3501 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003502 }
3503 }
3504
3505 SafeRelease(readTexture);
3506 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003507
3508 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003509}
3510
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003511ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3512{
3513 D3D11_TEXTURE2D_DESC textureDesc;
3514 source->GetDesc(&textureDesc);
3515
3516 if (textureDesc.SampleDesc.Count > 1)
3517 {
3518 D3D11_TEXTURE2D_DESC resolveDesc;
3519 resolveDesc.Width = textureDesc.Width;
3520 resolveDesc.Height = textureDesc.Height;
3521 resolveDesc.MipLevels = 1;
3522 resolveDesc.ArraySize = 1;
3523 resolveDesc.Format = textureDesc.Format;
3524 resolveDesc.SampleDesc.Count = 1;
3525 resolveDesc.SampleDesc.Quality = 0;
3526 resolveDesc.Usage = textureDesc.Usage;
3527 resolveDesc.BindFlags = textureDesc.BindFlags;
3528 resolveDesc.CPUAccessFlags = 0;
3529 resolveDesc.MiscFlags = 0;
3530
3531 ID3D11Texture2D *resolveTexture = NULL;
3532 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3533 if (FAILED(result))
3534 {
3535 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3536 return NULL;
3537 }
3538
3539 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3540 return resolveTexture;
3541 }
3542 else
3543 {
3544 source->AddRef();
3545 return source;
3546 }
3547}
3548
Geoff Lang42477a42013-09-17 17:07:02 -04003549void Renderer11::invalidateRenderbufferSwizzles(gl::Renderbuffer *renderBuffer, int mipLevel)
3550{
3551 TextureStorage *texStorage = renderBuffer->getTextureStorage();
3552 if (texStorage)
3553 {
3554 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3555 if (!texStorage11)
3556 {
3557 ERR("texture storage pointer unexpectedly null.");
3558 return;
3559 }
3560
3561 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3562 }
3563}
3564
3565void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3566{
3567 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3568 {
3569 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
3570 if (colorbuffer)
3571 {
3572 invalidateRenderbufferSwizzles(colorbuffer, framebuffer->getColorbufferMipLevel(colorAttachment));
3573 }
3574 }
3575
3576 gl::Renderbuffer *depthBuffer = framebuffer->getDepthbuffer();
3577 if (depthBuffer)
3578 {
3579 invalidateRenderbufferSwizzles(depthBuffer, framebuffer->getDepthbufferMipLevel());
3580 }
3581
3582 gl::Renderbuffer *stencilBuffer = framebuffer->getStencilbuffer();
3583 if (stencilBuffer)
3584 {
3585 invalidateRenderbufferSwizzles(stencilBuffer, framebuffer->getStencilbufferMipLevel());
3586 }
3587}
3588
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003589bool Renderer11::getLUID(LUID *adapterLuid) const
3590{
3591 adapterLuid->HighPart = 0;
3592 adapterLuid->LowPart = 0;
3593
3594 if (!mDxgiAdapter)
3595 {
3596 return false;
3597 }
3598
3599 DXGI_ADAPTER_DESC adapterDesc;
3600 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3601 {
3602 return false;
3603 }
3604
3605 *adapterLuid = adapterDesc.AdapterLuid;
3606 return true;
3607}
3608
Geoff Lang005df412013-10-16 14:12:50 -04003609GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003610{
3611 int clientVersion = getCurrentClientVersion();
3612 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3613}
3614
Jamie Madill95ffb862014-01-29 09:26:59 -05003615rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3616{
3617 return gl_d3d11::GetVertexConversionType(vertexFormat);
3618}
3619
3620GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3621{
3622 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3623}
3624
Geoff Lang61e49a52013-05-29 10:22:58 -04003625Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3626{
3627 MultisampleSupportInfo supportInfo = { 0 };
3628
3629 UINT formatSupport;
3630 HRESULT result;
3631
3632 result = mDevice->CheckFormatSupport(format, &formatSupport);
3633 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3634 {
3635 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3636 {
3637 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3638 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3639 {
3640 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3641 }
3642 else
3643 {
3644 supportInfo.qualityLevels[i - 1] = 0;
3645 }
3646 }
3647 }
3648
3649 return supportInfo;
3650}
3651
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003652}