blob: be3af98a15005132ac7b75036b755231ad65e169 [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//
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00003// Copyright (c) 2012-2013 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 {
486 UINT formatSupport = 0;
487 result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
488 depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
489 }
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
1183void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1184{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001185 if (mode == GL_LINE_LOOP)
1186 {
1187 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1188 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001189 else if (mode == GL_TRIANGLE_FAN)
1190 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001191 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001192 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001193 else if (instances > 0)
1194 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001195 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001196 }
1197 else
1198 {
1199 mDeviceContext->Draw(count, 0);
1200 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001201}
1202
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001203void 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 +00001204{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001205 if (mode == GL_LINE_LOOP)
1206 {
1207 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1208 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001209 else if (mode == GL_TRIANGLE_FAN)
1210 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001211 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1212 }
1213 else if (instances > 0)
1214 {
1215 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001216 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001217 else
1218 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001219 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001220 }
1221}
1222
1223void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1224{
1225 // Get the raw indices for an indexed draw
1226 if (type != GL_NONE && elementArrayBuffer)
1227 {
1228 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001229 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001230 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001231 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001232 }
1233
1234 if (!mLineLoopIB)
1235 {
1236 mLineLoopIB = new StreamingIndexBufferInterface(this);
1237 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1238 {
1239 delete mLineLoopIB;
1240 mLineLoopIB = NULL;
1241
1242 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001243 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001244 }
1245 }
1246
Geoff Lang57e713e2013-07-31 17:01:58 -04001247 // Checked by Renderer11::applyPrimitiveType
1248 ASSERT(count >= 0);
1249
1250 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001251 {
1252 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1253 return gl::error(GL_OUT_OF_MEMORY);
1254 }
1255
Geoff Lang57e713e2013-07-31 17:01:58 -04001256 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001257 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1258 {
1259 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001260 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001261 }
1262
1263 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001264 unsigned int offset;
1265 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001266 {
1267 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001268 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001269 }
1270
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001271 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001272 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001273
1274 switch (type)
1275 {
1276 case GL_NONE: // Non-indexed draw
1277 for (int i = 0; i < count; i++)
1278 {
1279 data[i] = i;
1280 }
1281 data[count] = 0;
1282 break;
1283 case GL_UNSIGNED_BYTE:
1284 for (int i = 0; i < count; i++)
1285 {
1286 data[i] = static_cast<const GLubyte*>(indices)[i];
1287 }
1288 data[count] = static_cast<const GLubyte*>(indices)[0];
1289 break;
1290 case GL_UNSIGNED_SHORT:
1291 for (int i = 0; i < count; i++)
1292 {
1293 data[i] = static_cast<const GLushort*>(indices)[i];
1294 }
1295 data[count] = static_cast<const GLushort*>(indices)[0];
1296 break;
1297 case GL_UNSIGNED_INT:
1298 for (int i = 0; i < count; i++)
1299 {
1300 data[i] = static_cast<const GLuint*>(indices)[i];
1301 }
1302 data[count] = static_cast<const GLuint*>(indices)[0];
1303 break;
1304 default: UNREACHABLE();
1305 }
1306
1307 if (!mLineLoopIB->unmapBuffer())
1308 {
1309 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001310 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001311 }
1312
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001313 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001314 {
1315 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1316
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001317 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001318 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001319 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001320 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001321 }
1322
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001323 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001324}
1325
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001326void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001327{
1328 // Get the raw indices for an indexed draw
1329 if (type != GL_NONE && elementArrayBuffer)
1330 {
1331 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001332 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001333 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001334 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001335 }
1336
1337 if (!mTriangleFanIB)
1338 {
1339 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1340 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1341 {
1342 delete mTriangleFanIB;
1343 mTriangleFanIB = NULL;
1344
1345 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001346 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001347 }
1348 }
1349
Geoff Lang57e713e2013-07-31 17:01:58 -04001350 // Checked by Renderer11::applyPrimitiveType
1351 ASSERT(count >= 3);
1352
Geoff Langeadfd572013-07-09 15:55:07 -04001353 const unsigned int numTris = count - 2;
1354
Geoff Lang57e713e2013-07-31 17:01:58 -04001355 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001356 {
1357 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1358 return gl::error(GL_OUT_OF_MEMORY);
1359 }
1360
1361 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001362 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1363 {
1364 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001365 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001366 }
1367
1368 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001369 unsigned int offset;
1370 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001371 {
1372 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001373 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001374 }
1375
1376 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001377 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001378
1379 switch (type)
1380 {
1381 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001382 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001383 {
1384 data[i*3 + 0] = 0;
1385 data[i*3 + 1] = i + 1;
1386 data[i*3 + 2] = i + 2;
1387 }
1388 break;
1389 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001390 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001391 {
1392 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1393 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1394 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1395 }
1396 break;
1397 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001398 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001399 {
1400 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1401 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1402 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1403 }
1404 break;
1405 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001406 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001407 {
1408 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1409 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1410 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1411 }
1412 break;
1413 default: UNREACHABLE();
1414 }
1415
1416 if (!mTriangleFanIB->unmapBuffer())
1417 {
1418 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001419 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001420 }
1421
1422 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1423 {
1424 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1425
1426 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1427 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001428 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001429 mAppliedIBOffset = indexBufferOffset;
1430 }
1431
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001432 if (instances > 0)
1433 {
1434 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1435 }
1436 else
1437 {
1438 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1439 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001440}
1441
Jamie Madillc5a83002014-02-14 16:41:25 -05001442void Renderer11::applyShaders(gl::ProgramBinary *programBinary, bool rasterizerDiscard, const gl::VertexFormat inputLayout[])
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001443{
Jamie Madillc5a83002014-02-14 16:41:25 -05001444 ShaderExecutable *vertexExe = programBinary->getVertexExecutableForInputLayout(inputLayout);
Jamie Madill6246dc82014-01-29 09:26:47 -05001445 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
1446 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001447
Jamie Madill6246dc82014-01-29 09:26:47 -05001448 ID3D11VertexShader *vertexShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader() : NULL);
1449 ID3D11PixelShader *pixelShader = (pixelExe ? ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader() : NULL);
1450 ID3D11GeometryShader *geometryShader = (geometryExe ? ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader() : NULL);
1451
1452 // Skip GS if we aren't drawing points
1453 if (!mCurRasterState.pointDrawMode)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001454 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001455 geometryShader = NULL;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001456 }
1457
Geoff Lang0550d032014-01-30 11:29:07 -05001458 // Skip pixel shader if we're doing rasterizer discard.
1459 if (rasterizerDiscard)
1460 {
1461 pixelShader = NULL;
1462 }
1463
Jamie Madill6246dc82014-01-29 09:26:47 -05001464 bool dirtyUniforms = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001465
Jamie Madill6246dc82014-01-29 09:26:47 -05001466 if (vertexShader != mAppliedVertexShader)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001467 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001468 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1469 mAppliedVertexShader = vertexShader;
1470 dirtyUniforms = true;
1471 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001472
Jamie Madill6246dc82014-01-29 09:26:47 -05001473 if (geometryShader != mAppliedGeometryShader)
1474 {
1475 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1476 mAppliedGeometryShader = geometryShader;
1477 dirtyUniforms = true;
1478 }
1479
1480 if (pixelShader != mAppliedPixelShader)
1481 {
1482 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1483 mAppliedPixelShader = pixelShader;
1484 dirtyUniforms = true;
1485 }
1486
1487 if (dirtyUniforms)
1488 {
1489 programBinary->dirtyAllUniforms();
daniel@transgaming.come4991412012-12-20 20:55:34 +00001490 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001491}
1492
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001493void Renderer11::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001494{
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001495 const gl::UniformArray &uniformArray = programBinary.getUniforms();
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001496
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001497 unsigned int totalRegisterCountVS = 0;
1498 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001499
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001500 bool vertexUniformsDirty = false;
1501 bool pixelUniformsDirty = false;
1502
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001503 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001504 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001505 const gl::Uniform &uniform = *uniformArray[uniformIndex];
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001506
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001507 if (uniform.isReferencedByVertexShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001508 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001509 totalRegisterCountVS += uniform.registerCount;
1510 vertexUniformsDirty = (vertexUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001511 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001512
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001513 if (uniform.isReferencedByFragmentShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001514 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001515 totalRegisterCountPS += uniform.registerCount;
1516 pixelUniformsDirty = (pixelUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001517 }
1518 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001519
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001520 const UniformStorage11 *vertexUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getVertexUniformStorage());
1521 const UniformStorage11 *fragmentUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getFragmentUniformStorage());
1522 ASSERT(vertexUniformStorage);
1523 ASSERT(fragmentUniformStorage);
1524
1525 ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
1526 ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001527
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001528 float (*mapVS)[4] = NULL;
1529 float (*mapPS)[4] = NULL;
1530
Shannon Woods5ab33c82013-06-26 15:31:09 -04001531 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1532 {
1533 D3D11_MAPPED_SUBRESOURCE map = {0};
1534 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1535 ASSERT(SUCCEEDED(result));
1536 mapVS = (float(*)[4])map.pData;
1537 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001538
Shannon Woods5ab33c82013-06-26 15:31:09 -04001539 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1540 {
1541 D3D11_MAPPED_SUBRESOURCE map = {0};
1542 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1543 ASSERT(SUCCEEDED(result));
1544 mapPS = (float(*)[4])map.pData;
1545 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001546
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001547 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001548 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001549 gl::Uniform *uniform = uniformArray[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001550
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001551 if (!uniform->isSampler())
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001552 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001553 unsigned int componentCount = (4 - uniform->registerElement);
1554
Jamie Madill71cc91f2013-09-18 12:51:22 -04001555 // 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 -04001556 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001557
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001558 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001559 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001560 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001561 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001562
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001563 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001564 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001565 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001566 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001567 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001568 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001569
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001570 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001571 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001572 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001573 }
1574
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001575 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001576 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001577 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001578 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001579
1580 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1581 {
1582 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1583 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1584 }
1585
1586 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1587 {
1588 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1589 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1590 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001591
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001592 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001593 if (!mDriverConstantBufferVS)
1594 {
1595 D3D11_BUFFER_DESC constantBufferDescription = {0};
1596 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1597 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1598 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1599 constantBufferDescription.CPUAccessFlags = 0;
1600 constantBufferDescription.MiscFlags = 0;
1601 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001602
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001603 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001604 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001605
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001606 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1607 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001608
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001609 if (!mDriverConstantBufferPS)
1610 {
1611 D3D11_BUFFER_DESC constantBufferDescription = {0};
1612 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1613 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1614 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1615 constantBufferDescription.CPUAccessFlags = 0;
1616 constantBufferDescription.MiscFlags = 0;
1617 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001618
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001619 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001620 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001621
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001622 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1623 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001624
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001625 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1626 {
1627 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1628 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1629 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001630
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001631 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1632 {
1633 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1634 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1635 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001636
1637 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001638 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1639 {
1640 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1641 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1642 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001643}
1644
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001645void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001646{
Geoff Langda507fe2013-08-20 12:01:42 -04001647 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001648 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001649}
1650
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001651void Renderer11::markAllStateDirty()
1652{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001653 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1654 {
1655 mAppliedRenderTargetSerials[rtIndex] = 0;
1656 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001657 mAppliedDepthbufferSerial = 0;
1658 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001659 mDepthStencilInitialized = false;
1660 mRenderTargetDescInitialized = false;
1661
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001662 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001663 {
1664 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001665 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001666 }
1667 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1668 {
1669 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001670 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001671 }
1672
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001673 mForceSetBlendState = true;
1674 mForceSetRasterState = true;
1675 mForceSetDepthStencilState = true;
1676 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001677 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001678
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001679 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001680 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001681 mAppliedIBOffset = 0;
1682
Jamie Madill6246dc82014-01-29 09:26:47 -05001683 mAppliedVertexShader = NULL;
1684 mAppliedGeometryShader = NULL;
1685 mAppliedPixelShader = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001686 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1687 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001688
1689 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001690
1691 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1692 {
1693 mCurrentConstantBufferVS[i] = -1;
1694 mCurrentConstantBufferPS[i] = -1;
1695 }
1696
1697 mCurrentVertexConstantBuffer = NULL;
1698 mCurrentPixelConstantBuffer = NULL;
1699 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001700
1701 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001702}
1703
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001704void Renderer11::releaseDeviceResources()
1705{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001706 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001707 mInputLayoutCache.clear();
1708
Geoff Langea228632013-07-30 15:17:12 -04001709 SafeDelete(mVertexDataManager);
1710 SafeDelete(mIndexDataManager);
1711 SafeDelete(mLineLoopIB);
1712 SafeDelete(mTriangleFanIB);
1713 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001714 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001715 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001716
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001717 SafeRelease(mDriverConstantBufferVS);
1718 SafeRelease(mDriverConstantBufferPS);
1719 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001720}
1721
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001722void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001723{
1724 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001725 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001726}
1727
1728bool Renderer11::isDeviceLost()
1729{
1730 return mDeviceLost;
1731}
1732
1733// set notify to true to broadcast a message to all contexts of the device loss
1734bool Renderer11::testDeviceLost(bool notify)
1735{
1736 bool isLost = false;
1737
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001738 // GetRemovedReason is used to test if the device is removed
1739 HRESULT result = mDevice->GetDeviceRemovedReason();
1740 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001741
1742 if (isLost)
1743 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001744 // Log error if this is a new device lost event
1745 if (mDeviceLost == false)
1746 {
1747 ERR("The D3D11 device was removed: 0x%08X", result);
1748 }
1749
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001750 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001751 // we'll probably get this done again by notifyDeviceLost
1752 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001753 // Note that we don't want to clear the device loss status here
1754 // -- this needs to be done by resetDevice
1755 mDeviceLost = true;
1756 if (notify)
1757 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001758 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001759 }
1760 }
1761
1762 return isLost;
1763}
1764
1765bool Renderer11::testDeviceResettable()
1766{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001767 // determine if the device is resettable by creating a dummy device
1768 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001769
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001770 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001771 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001772 return false;
1773 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001774
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001775 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001776 {
1777 D3D_FEATURE_LEVEL_11_0,
1778 D3D_FEATURE_LEVEL_10_1,
1779 D3D_FEATURE_LEVEL_10_0,
1780 };
1781
1782 ID3D11Device* dummyDevice;
1783 D3D_FEATURE_LEVEL dummyFeatureLevel;
1784 ID3D11DeviceContext* dummyContext;
1785
1786 HRESULT result = D3D11CreateDevice(NULL,
1787 D3D_DRIVER_TYPE_HARDWARE,
1788 NULL,
1789 #if defined(_DEBUG)
1790 D3D11_CREATE_DEVICE_DEBUG,
1791 #else
1792 0,
1793 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001794 featureLevels,
1795 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001796 D3D11_SDK_VERSION,
1797 &dummyDevice,
1798 &dummyFeatureLevel,
1799 &dummyContext);
1800
1801 if (!mDevice || FAILED(result))
1802 {
1803 return false;
1804 }
1805
Geoff Langea228632013-07-30 15:17:12 -04001806 SafeRelease(dummyContext);
1807 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001808
1809 return true;
1810}
1811
1812void Renderer11::release()
1813{
1814 releaseDeviceResources();
1815
Geoff Langea228632013-07-30 15:17:12 -04001816 SafeRelease(mDxgiFactory);
1817 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001818
1819 if (mDeviceContext)
1820 {
1821 mDeviceContext->ClearState();
1822 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001823 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001824 }
1825
Geoff Langea228632013-07-30 15:17:12 -04001826 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001827
1828 if (mD3d11Module)
1829 {
1830 FreeLibrary(mD3d11Module);
1831 mD3d11Module = NULL;
1832 }
1833
1834 if (mDxgiModule)
1835 {
1836 FreeLibrary(mDxgiModule);
1837 mDxgiModule = NULL;
1838 }
Geoff Langdad5ed32014-02-10 12:59:17 -05001839
1840 mCompiler.release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001841}
1842
1843bool Renderer11::resetDevice()
1844{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001845 // recreate everything
1846 release();
1847 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001848
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001849 if (result != EGL_SUCCESS)
1850 {
1851 ERR("Could not reinitialize D3D11 device: %08X", result);
1852 return false;
1853 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001854
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001855 mDeviceLost = false;
1856
1857 return true;
1858}
1859
1860DWORD Renderer11::getAdapterVendor() const
1861{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001862 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001863}
1864
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001865std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001866{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001867 std::ostringstream rendererString;
1868
1869 rendererString << mDescription;
1870 rendererString << " Direct3D11";
1871
1872 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1873 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1874
1875 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001876}
1877
1878GUID Renderer11::getAdapterIdentifier() const
1879{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001880 // Use the adapter LUID as our adapter ID
1881 // This number is local to a machine is only guaranteed to be unique between restarts
1882 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1883 GUID adapterId = {0};
1884 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1885 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001886}
1887
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001888bool Renderer11::getBGRATextureSupport() const
1889{
1890 return mBGRATextureSupport;
1891}
1892
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001893bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001894{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001895 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001896}
1897
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001898bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001899{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001900 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001901}
1902
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001903bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001904{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001905 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001906}
1907
1908bool Renderer11::getDepthTextureSupport() const
1909{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001910 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001911}
1912
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001913bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001914{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001915 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001916}
1917
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001918bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001919{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001920 return mFloat32FilterSupport;
1921}
1922
1923bool Renderer11::getFloat32TextureRenderingSupport() const
1924{
1925 return mFloat32RenderSupport;
1926}
1927
1928bool Renderer11::getFloat16TextureSupport() const
1929{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001930 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001931}
1932
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001933bool Renderer11::getFloat16TextureFilteringSupport() const
1934{
1935 return mFloat16FilterSupport;
1936}
1937
1938bool Renderer11::getFloat16TextureRenderingSupport() const
1939{
1940 return mFloat16RenderSupport;
1941}
1942
Geoff Langd42cf4e2013-06-05 16:09:17 -04001943bool Renderer11::getRGB565TextureSupport() const
1944{
1945 return false;
1946}
1947
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001948bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001949{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001950 return false;
1951}
1952
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001953bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001954{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001955 return false;
1956}
1957
Geoff Lang632192d2013-10-04 13:40:46 -04001958bool Renderer11::getRGTextureSupport() const
1959{
1960 return mRGTextureSupport;
1961}
1962
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001963bool Renderer11::getTextureFilterAnisotropySupport() const
1964{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001965 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001966}
1967
1968float Renderer11::getTextureMaxAnisotropy() const
1969{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001970 switch (mFeatureLevel)
1971 {
1972 case D3D_FEATURE_LEVEL_11_0:
1973 return D3D11_MAX_MAXANISOTROPY;
1974 case D3D_FEATURE_LEVEL_10_1:
1975 case D3D_FEATURE_LEVEL_10_0:
1976 return D3D10_MAX_MAXANISOTROPY;
1977 default: UNREACHABLE();
1978 return 0;
1979 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001980}
1981
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001982bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001983{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00001984 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001985}
1986
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001987Range Renderer11::getViewportBounds() const
1988{
1989 switch (mFeatureLevel)
1990 {
1991 case D3D_FEATURE_LEVEL_11_0:
1992 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1993 case D3D_FEATURE_LEVEL_10_1:
1994 case D3D_FEATURE_LEVEL_10_0:
1995 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1996 default: UNREACHABLE();
1997 return Range(0, 0);
1998 }
1999}
2000
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002001unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002002{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002003 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2004 switch (mFeatureLevel)
2005 {
2006 case D3D_FEATURE_LEVEL_11_0:
2007 case D3D_FEATURE_LEVEL_10_1:
2008 case D3D_FEATURE_LEVEL_10_0:
2009 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
2010 default: UNREACHABLE();
2011 return 0;
2012 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002013}
2014
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002015unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
2016{
2017 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2018}
2019
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002020unsigned int Renderer11::getReservedVertexUniformVectors() const
2021{
Shannon Woods5ab33c82013-06-26 15:31:09 -04002022 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002023}
2024
2025unsigned int Renderer11::getReservedFragmentUniformVectors() const
2026{
Shannon Woods5ab33c82013-06-26 15:31:09 -04002027 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002028}
2029
2030unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002031{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002032 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2033 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2034 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002035}
2036
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002037unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002038{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002039 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2040 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2041 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002042}
2043
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002044unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002045{
2046 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00002047 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
2048 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002049 switch (mFeatureLevel)
2050 {
2051 case D3D_FEATURE_LEVEL_11_0:
2052 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2053 case D3D_FEATURE_LEVEL_10_1:
2054 case D3D_FEATURE_LEVEL_10_0:
2055 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2056 default: UNREACHABLE();
2057 return 0;
2058 }
2059}
2060
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002061unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2062{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002063 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2064 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2065
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002066 switch (mFeatureLevel)
2067 {
2068 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002069 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002070 case D3D_FEATURE_LEVEL_10_1:
2071 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002072 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002073 default: UNREACHABLE();
2074 return 0;
2075 }
2076}
2077
2078unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2079{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002080 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2081 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2082
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002083 switch (mFeatureLevel)
2084 {
2085 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002086 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002087 case D3D_FEATURE_LEVEL_10_1:
2088 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002089 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002090 default: UNREACHABLE();
2091 return 0;
2092 }
2093}
2094
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002095unsigned int Renderer11::getReservedVertexUniformBuffers() const
2096{
2097 // we reserve one buffer for the application uniforms, and one for driver uniforms
2098 return 2;
2099}
2100
2101unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2102{
2103 // we reserve one buffer for the application uniforms, and one for driver uniforms
2104 return 2;
2105}
2106
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002107unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2108{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002109 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2110 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2111
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002112 switch (mFeatureLevel)
2113 {
2114 case D3D_FEATURE_LEVEL_11_0:
2115 return D3D11_SO_BUFFER_SLOT_COUNT;
2116 case D3D_FEATURE_LEVEL_10_1:
2117 case D3D_FEATURE_LEVEL_10_0:
2118 return D3D10_SO_BUFFER_SLOT_COUNT;
2119 default: UNREACHABLE();
2120 return 0;
2121 }
2122}
2123
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002124unsigned int Renderer11::getMaxUniformBufferSize() const
2125{
2126 // Each component is a 4-element vector of 4-byte units (floats)
2127 const unsigned int bytesPerComponent = 4 * sizeof(float);
2128
2129 switch (mFeatureLevel)
2130 {
2131 case D3D_FEATURE_LEVEL_11_0:
2132 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2133 case D3D_FEATURE_LEVEL_10_1:
2134 case D3D_FEATURE_LEVEL_10_0:
2135 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2136 default: UNREACHABLE();
2137 return 0;
2138 }
2139}
2140
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002141bool Renderer11::getNonPower2TextureSupport() const
2142{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002143 switch (mFeatureLevel)
2144 {
2145 case D3D_FEATURE_LEVEL_11_0:
2146 case D3D_FEATURE_LEVEL_10_1:
2147 case D3D_FEATURE_LEVEL_10_0:
2148 return true;
2149 default: UNREACHABLE();
2150 return false;
2151 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002152}
2153
2154bool Renderer11::getOcclusionQuerySupport() const
2155{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002156 switch (mFeatureLevel)
2157 {
2158 case D3D_FEATURE_LEVEL_11_0:
2159 case D3D_FEATURE_LEVEL_10_1:
2160 case D3D_FEATURE_LEVEL_10_0:
2161 return true;
2162 default: UNREACHABLE();
2163 return false;
2164 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002165}
2166
2167bool Renderer11::getInstancingSupport() const
2168{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002169 switch (mFeatureLevel)
2170 {
2171 case D3D_FEATURE_LEVEL_11_0:
2172 case D3D_FEATURE_LEVEL_10_1:
2173 case D3D_FEATURE_LEVEL_10_0:
2174 return true;
2175 default: UNREACHABLE();
2176 return false;
2177 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002178}
2179
2180bool Renderer11::getShareHandleSupport() const
2181{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002182 // We only currently support share handles with BGRA surfaces, because
2183 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002184 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002185 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002186}
2187
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002188bool Renderer11::getDerivativeInstructionSupport() const
2189{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002190 switch (mFeatureLevel)
2191 {
2192 case D3D_FEATURE_LEVEL_11_0:
2193 case D3D_FEATURE_LEVEL_10_1:
2194 case D3D_FEATURE_LEVEL_10_0:
2195 return true;
2196 default: UNREACHABLE();
2197 return false;
2198 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002199}
2200
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002201bool Renderer11::getPostSubBufferSupport() const
2202{
2203 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2204 return false;
2205}
2206
Jamie Madill13a2f852013-12-11 16:35:08 -05002207int Renderer11::getMaxRecommendedElementsIndices() const
2208{
2209 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2210 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2211
2212 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2213 return std::numeric_limits<GLint>::max();
2214}
2215
2216int Renderer11::getMaxRecommendedElementsVertices() const
2217{
2218 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2219 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2220
2221 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2222 return std::numeric_limits<GLint>::max();
2223}
2224
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002225int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002226{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002227 switch (mFeatureLevel)
2228 {
2229 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002230 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002231 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2232 default: UNREACHABLE(); return 0;
2233 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002234}
2235
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002236int Renderer11::getMinorShaderModel() const
2237{
2238 switch (mFeatureLevel)
2239 {
2240 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2241 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2242 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2243 default: UNREACHABLE(); return 0;
2244 }
2245}
2246
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002247float Renderer11::getMaxPointSize() const
2248{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002249 // choose a reasonable maximum. we enforce this in the shader.
2250 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2251 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002252}
2253
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002254int Renderer11::getMaxViewportDimension() const
2255{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002256 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2257 // In our case return the maximum texture size, which is the maximum render buffer size.
2258 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2259 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2260
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002261 switch (mFeatureLevel)
2262 {
2263 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002264 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002265 case D3D_FEATURE_LEVEL_10_1:
2266 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002267 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002268 default: UNREACHABLE();
2269 return 0;
2270 }
2271}
2272
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002273int Renderer11::getMaxTextureWidth() const
2274{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002275 switch (mFeatureLevel)
2276 {
2277 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2278 case D3D_FEATURE_LEVEL_10_1:
2279 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2280 default: UNREACHABLE(); return 0;
2281 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002282}
2283
2284int Renderer11::getMaxTextureHeight() const
2285{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002286 switch (mFeatureLevel)
2287 {
2288 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2289 case D3D_FEATURE_LEVEL_10_1:
2290 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2291 default: UNREACHABLE(); return 0;
2292 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002293}
2294
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002295int Renderer11::getMaxTextureDepth() const
2296{
2297 switch (mFeatureLevel)
2298 {
2299 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2300 case D3D_FEATURE_LEVEL_10_1:
2301 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2302 default: UNREACHABLE(); return 0;
2303 }
2304}
2305
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002306int Renderer11::getMaxTextureArrayLayers() const
2307{
2308 switch (mFeatureLevel)
2309 {
2310 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2311 case D3D_FEATURE_LEVEL_10_1:
2312 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2313 default: UNREACHABLE(); return 0;
2314 }
2315}
2316
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002317bool Renderer11::get32BitIndexSupport() const
2318{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002319 switch (mFeatureLevel)
2320 {
2321 case D3D_FEATURE_LEVEL_11_0:
2322 case D3D_FEATURE_LEVEL_10_1:
2323 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2324 default: UNREACHABLE(); return false;
2325 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002326}
2327
2328int Renderer11::getMinSwapInterval() const
2329{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002330 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002331}
2332
2333int Renderer11::getMaxSwapInterval() const
2334{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002335 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002336}
2337
2338int Renderer11::getMaxSupportedSamples() const
2339{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002340 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002341}
2342
Geoff Lang005df412013-10-16 14:12:50 -04002343GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002344{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002345 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002346 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2347 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2348}
2349
Geoff Lang005df412013-10-16 14:12:50 -04002350GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002351{
2352 unsigned int numCounts = 0;
2353
2354 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002355 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2356 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002357 {
2358 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2359 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2360
2361 if (iter != mMultisampleSupportMap.end())
2362 {
2363 const MultisampleSupportInfo& info = iter->second;
2364 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2365 {
2366 if (info.qualityLevels[i] > 0)
2367 {
2368 numCounts++;
2369 }
2370 }
2371 }
2372 }
2373
2374 return numCounts;
2375}
2376
Geoff Lang005df412013-10-16 14:12:50 -04002377void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002378{
2379 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002380 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2381 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2382 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002383 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002384 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002385
2386 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2387 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2388
2389 if (iter != mMultisampleSupportMap.end())
2390 {
2391 const MultisampleSupportInfo& info = iter->second;
2392 int bufPos = 0;
2393 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2394 {
2395 if (info.qualityLevels[i] > 0)
2396 {
2397 params[bufPos++] = i + 1;
2398 }
2399 }
2400 }
2401}
2402
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002403int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2404{
2405 if (requested == 0)
2406 {
2407 return 0;
2408 }
2409
2410 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2411 if (iter != mMultisampleSupportMap.end())
2412 {
2413 const MultisampleSupportInfo& info = iter->second;
2414 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2415 {
2416 if (info.qualityLevels[i] > 0)
2417 {
2418 return i + 1;
2419 }
2420 }
2421 }
2422
2423 return -1;
2424}
2425
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002426unsigned int Renderer11::getMaxRenderTargets() const
2427{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002428 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2429 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2430
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002431 switch (mFeatureLevel)
2432 {
2433 case D3D_FEATURE_LEVEL_11_0:
2434 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2435 case D3D_FEATURE_LEVEL_10_1:
2436 case D3D_FEATURE_LEVEL_10_0:
2437 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2438 default:
2439 UNREACHABLE();
2440 return 1;
2441 }
2442}
2443
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002444bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002445{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002446 if (source && dest)
2447 {
2448 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2449 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2450
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002451 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002452
2453 dest11->invalidateSwizzleCache();
2454
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002455 return true;
2456 }
2457
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002458 return false;
2459}
2460
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002461bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002462{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002463 if (source && dest)
2464 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002465 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2466 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002467
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002468 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002469
2470 dest11->invalidateSwizzleCache();
2471
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002472 return true;
2473 }
2474
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002475 return false;
2476}
2477
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002478bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2479{
2480 if (source && dest)
2481 {
2482 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2483 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2484
2485 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002486
2487 dest11->invalidateSwizzleCache();
2488
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002489 return true;
2490 }
2491
2492 return false;
2493}
2494
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002495bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2496{
2497 if (source && dest)
2498 {
2499 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2500 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2501
2502 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002503
2504 dest11->invalidateSwizzleCache();
2505
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002506 return true;
2507 }
2508
2509 return false;
2510}
2511
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002512bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002513 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002514{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002515 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002516 if (!colorbuffer)
2517 {
2518 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002519 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002520 }
2521
2522 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2523 if (!sourceRenderTarget)
2524 {
2525 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002526 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002527 }
2528
2529 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2530 if (!source)
2531 {
2532 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002533 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002534 }
2535
2536 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2537 if (!storage11)
2538 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002539 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002540 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002541 }
2542
2543 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2544 if (!destRenderTarget)
2545 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002546 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002547 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002548 }
2549
2550 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2551 if (!dest)
2552 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002553 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002554 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002555 }
2556
Geoff Langb86b9792013-06-04 16:32:05 -04002557 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2558 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002559
Geoff Langb86b9792013-06-04 16:32:05 -04002560 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2561 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002562
Geoff Langb86b9792013-06-04 16:32:05 -04002563 // Use nearest filtering because source and destination are the same size for the direct
2564 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002565 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002566 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002567
Geoff Lang42477a42013-09-17 17:07:02 -04002568 storage11->invalidateSwizzleCacheLevel(level);
2569
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002570 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002571}
2572
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002573bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002574 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002575{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002576 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002577 if (!colorbuffer)
2578 {
2579 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002580 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002581 }
2582
2583 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2584 if (!sourceRenderTarget)
2585 {
2586 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002587 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002588 }
2589
2590 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2591 if (!source)
2592 {
2593 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002594 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002595 }
2596
2597 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2598 if (!storage11)
2599 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002600 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002601 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002602 }
2603
Nicolas Capensb13f8662013-06-04 13:30:19 -04002604 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002605 if (!destRenderTarget)
2606 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002607 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002608 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002609 }
2610
2611 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2612 if (!dest)
2613 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002614 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002615 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002616 }
2617
Geoff Langb86b9792013-06-04 16:32:05 -04002618 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2619 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002620
Geoff Langb86b9792013-06-04 16:32:05 -04002621 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2622 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002623
Geoff Langb86b9792013-06-04 16:32:05 -04002624 // Use nearest filtering because source and destination are the same size for the direct
2625 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002626 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002627 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002628
Geoff Lang42477a42013-09-17 17:07:02 -04002629 storage11->invalidateSwizzleCacheLevel(level);
2630
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002631 return ret;
2632}
2633
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002634bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2635 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2636{
2637 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2638 if (!colorbuffer)
2639 {
2640 ERR("Failed to retrieve the color buffer from the frame buffer.");
2641 return gl::error(GL_OUT_OF_MEMORY, false);
2642 }
2643
2644 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2645 if (!sourceRenderTarget)
2646 {
2647 ERR("Failed to retrieve the render target from the frame buffer.");
2648 return gl::error(GL_OUT_OF_MEMORY, false);
2649 }
2650
2651 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2652 if (!source)
2653 {
2654 ERR("Failed to retrieve the render target view from the render target.");
2655 return gl::error(GL_OUT_OF_MEMORY, false);
2656 }
2657
2658 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2659 if (!storage11)
2660 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002661 ERR("Failed to retrieve the texture storage from the destination.");
2662 return gl::error(GL_OUT_OF_MEMORY, false);
2663 }
2664
2665 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2666 if (!destRenderTarget)
2667 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002668 ERR("Failed to retrieve the render target from the destination storage.");
2669 return gl::error(GL_OUT_OF_MEMORY, false);
2670 }
2671
2672 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2673 if (!dest)
2674 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002675 ERR("Failed to retrieve the render target view from the destination render target.");
2676 return gl::error(GL_OUT_OF_MEMORY, false);
2677 }
2678
Geoff Langb86b9792013-06-04 16:32:05 -04002679 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2680 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002681
Geoff Langb86b9792013-06-04 16:32:05 -04002682 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2683 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002684
Geoff Langb86b9792013-06-04 16:32:05 -04002685 // Use nearest filtering because source and destination are the same size for the direct
2686 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002687 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002688 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002689
Geoff Lang42477a42013-09-17 17:07:02 -04002690 storage11->invalidateSwizzleCacheLevel(level);
2691
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002692 return ret;
2693}
2694
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002695bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2696 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2697{
2698 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2699 if (!colorbuffer)
2700 {
2701 ERR("Failed to retrieve the color buffer from the frame buffer.");
2702 return gl::error(GL_OUT_OF_MEMORY, false);
2703 }
2704
2705 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2706 if (!sourceRenderTarget)
2707 {
2708 ERR("Failed to retrieve the render target from the frame buffer.");
2709 return gl::error(GL_OUT_OF_MEMORY, false);
2710 }
2711
2712 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2713 if (!source)
2714 {
2715 ERR("Failed to retrieve the render target view from the render target.");
2716 return gl::error(GL_OUT_OF_MEMORY, false);
2717 }
2718
2719 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2720 if (!storage11)
2721 {
Geoff Langea228632013-07-30 15:17:12 -04002722 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002723 ERR("Failed to retrieve the texture storage from the destination.");
2724 return gl::error(GL_OUT_OF_MEMORY, false);
2725 }
2726
2727 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2728 if (!destRenderTarget)
2729 {
Geoff Langea228632013-07-30 15:17:12 -04002730 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002731 ERR("Failed to retrieve the render target from the destination storage.");
2732 return gl::error(GL_OUT_OF_MEMORY, false);
2733 }
2734
2735 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2736 if (!dest)
2737 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002738 ERR("Failed to retrieve the render target view from the destination render target.");
2739 return gl::error(GL_OUT_OF_MEMORY, false);
2740 }
2741
Geoff Langb86b9792013-06-04 16:32:05 -04002742 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2743 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002744
Geoff Langb86b9792013-06-04 16:32:05 -04002745 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2746 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002747
Geoff Langb86b9792013-06-04 16:32:05 -04002748 // Use nearest filtering because source and destination are the same size for the direct
2749 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002750 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002751 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002752
Geoff Lang42477a42013-09-17 17:07:02 -04002753 storage11->invalidateSwizzleCacheLevel(level);
2754
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002755 return ret;
2756}
2757
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002758void Renderer11::unapplyRenderTargets()
2759{
2760 setOneTimeRenderTarget(NULL);
2761}
2762
2763void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2764{
2765 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2766
2767 rtvArray[0] = renderTargetView;
2768
2769 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2770
2771 // Do not preserve the serial for this one-time-use render target
2772 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2773 {
2774 mAppliedRenderTargetSerials[rtIndex] = 0;
2775 }
2776}
2777
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002778RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2779{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002780 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002781 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002782
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002783 if (depth)
2784 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002785 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002786 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002787 swapChain11->getDepthStencilTexture(),
2788 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002789 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002790 }
2791 else
2792 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002793 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002794 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002795 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002796 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002797 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002798 }
2799 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002800}
2801
Geoff Langa2d97f12013-06-11 11:44:02 -04002802RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002803{
Geoff Langa2d97f12013-06-11 11:44:02 -04002804 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002805 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002806}
2807
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002808ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002809{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002810 ShaderExecutable11 *executable = NULL;
2811
2812 switch (type)
2813 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002814 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002815 {
2816 ID3D11VertexShader *vshader = NULL;
2817 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2818 ASSERT(SUCCEEDED(result));
2819
2820 if (vshader)
2821 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002822 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002823 }
2824 }
2825 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002826 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002827 {
2828 ID3D11PixelShader *pshader = NULL;
2829 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2830 ASSERT(SUCCEEDED(result));
2831
2832 if (pshader)
2833 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002834 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002835 }
2836 }
2837 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002838 case rx::SHADER_GEOMETRY:
2839 {
2840 ID3D11GeometryShader *gshader = NULL;
2841 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2842 ASSERT(SUCCEEDED(result));
2843
2844 if (gshader)
2845 {
2846 executable = new ShaderExecutable11(function, length, gshader);
2847 }
2848 }
2849 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002850 default:
2851 UNREACHABLE();
2852 break;
2853 }
2854
2855 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002856}
2857
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002858ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002859{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002860 const char *profile = NULL;
2861
2862 switch (type)
2863 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002864 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002865 profile = "vs_4_0";
2866 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002867 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002868 profile = "ps_4_0";
2869 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002870 case rx::SHADER_GEOMETRY:
2871 profile = "gs_4_0";
2872 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002873 default:
2874 UNREACHABLE();
2875 return NULL;
2876 }
2877
Geoff Langdad5ed32014-02-10 12:59:17 -05002878 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002879 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002880 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002881 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002882 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002883
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002884 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002885 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002886
2887 return executable;
2888}
2889
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002890rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2891{
2892 return new UniformStorage11(this, storageSize);
2893}
2894
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002895VertexBuffer *Renderer11::createVertexBuffer()
2896{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002897 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002898}
2899
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002900IndexBuffer *Renderer11::createIndexBuffer()
2901{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002902 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002903}
2904
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002905BufferStorage *Renderer11::createBufferStorage()
2906{
2907 return new BufferStorage11(this);
2908}
2909
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002910QueryImpl *Renderer11::createQuery(GLenum type)
2911{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002912 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002913}
2914
2915FenceImpl *Renderer11::createFence()
2916{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002917 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002918}
2919
Geoff Lang005df412013-10-16 14:12:50 -04002920bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002921{
Jamie Madill4461f092013-10-10 15:10:39 -04002922 int clientVersion = getCurrentClientVersion();
2923
2924 // We only support buffer to texture copies in ES3
2925 if (clientVersion <= 2)
2926 {
2927 return false;
2928 }
2929
2930 // sRGB formats do not work with D3D11 buffer SRVs
2931 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2932 {
2933 return false;
2934 }
2935
2936 // We cannot support direct copies to non-color-renderable formats
2937 if (!gl::IsColorRenderingSupported(internalFormat, this))
2938 {
2939 return false;
2940 }
2941
2942 // We skip all 3-channel formats since sometimes format support is missing
2943 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2944 {
2945 return false;
2946 }
2947
2948 // We don't support formats which we can't represent without conversion
2949 if (getNativeTextureFormat(internalFormat) != internalFormat)
2950 {
2951 return false;
2952 }
2953
2954 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002955}
2956
Jamie Madilla21eea32013-09-18 14:36:25 -04002957bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2958 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2959{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002960 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002961 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2962}
2963
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002964bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002965{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002966 ASSERT(colorbuffer != NULL);
2967
2968 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2969 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002970 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002971 *subresourceIndex = renderTarget->getSubresourceIndex();
2972
2973 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2974 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002975 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002976 ID3D11Resource *textureResource = NULL;
2977 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002978
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002979 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002980 {
Geoff Lang8e328842014-02-10 13:11:20 -05002981 HRESULT result = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002982 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002983
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002984 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002985 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002986 return true;
2987 }
2988 else
2989 {
2990 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2991 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002992 }
2993 }
2994 }
2995 }
2996
2997 return false;
2998}
2999
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003000bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04003001 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003002{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003003 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003004 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003005 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003006
3007 if (!readBuffer)
3008 {
3009 ERR("Failed to retrieve the read buffer from the read framebuffer.");
3010 return gl::error(GL_OUT_OF_MEMORY, false);
3011 }
3012
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003013 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003014
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003015 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003016 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003017 if (drawTarget->isEnabledColorAttachment(colorAttachment))
3018 {
3019 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
3020
3021 if (!drawBuffer)
3022 {
3023 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
3024 return gl::error(GL_OUT_OF_MEMORY, false);
3025 }
3026
3027 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
3028
Geoff Lang125deab2013-08-09 13:34:16 -04003029 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04003030 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003031 {
3032 return false;
3033 }
3034 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003035 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003036 }
3037
Geoff Lang685806d2013-06-12 11:16:36 -04003038 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003039 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003040 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
3041 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
3042
3043 if (!readBuffer)
3044 {
3045 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
3046 return gl::error(GL_OUT_OF_MEMORY, false);
3047 }
3048
3049 if (!drawBuffer)
3050 {
3051 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
3052 return gl::error(GL_OUT_OF_MEMORY, false);
3053 }
3054
3055 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3056 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3057
Geoff Lang125deab2013-08-09 13:34:16 -04003058 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04003059 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003060 {
3061 return false;
3062 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003063 }
3064
Geoff Lang42477a42013-09-17 17:07:02 -04003065 invalidateFramebufferSwizzles(drawTarget);
3066
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003067 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003068}
3069
3070void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3071 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3072{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003073 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003074 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003075
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003076 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3077
3078 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003079 {
3080 gl::Rectangle area;
3081 area.x = x;
3082 area.y = y;
3083 area.width = width;
3084 area.height = height;
3085
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003086 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3087 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003088
Geoff Langea228632013-07-30 15:17:12 -04003089 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003090 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003091}
3092
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003093Image *Renderer11::createImage()
3094{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003095 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003096}
3097
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003098void Renderer11::generateMipmap(Image *dest, Image *src)
3099{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003100 Image11 *dest11 = Image11::makeImage11(dest);
3101 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003102 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003103}
3104
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003105TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3106{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003107 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3108 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003109}
3110
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003111TextureStorage *Renderer11::createTextureStorage2D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003112{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003113 return new TextureStorage11_2D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003114}
3115
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003116TextureStorage *Renderer11::createTextureStorageCube(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, int size)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003117{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003118 return new TextureStorage11_Cube(this, baseLevel, maxLevel, internalformat, renderTarget, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003119}
3120
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003121TextureStorage *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 +00003122{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003123 return new TextureStorage11_3D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003124}
3125
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003126TextureStorage *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 +00003127{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003128 return new TextureStorage11_2DArray(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003129}
3130
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003131void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3132 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3133 GLint packAlignment, void *pixels)
3134{
3135 D3D11_TEXTURE2D_DESC textureDesc;
3136 texture->GetDesc(&textureDesc);
3137
3138 D3D11_TEXTURE2D_DESC stagingDesc;
3139 stagingDesc.Width = area.width;
3140 stagingDesc.Height = area.height;
3141 stagingDesc.MipLevels = 1;
3142 stagingDesc.ArraySize = 1;
3143 stagingDesc.Format = textureDesc.Format;
3144 stagingDesc.SampleDesc.Count = 1;
3145 stagingDesc.SampleDesc.Quality = 0;
3146 stagingDesc.Usage = D3D11_USAGE_STAGING;
3147 stagingDesc.BindFlags = 0;
3148 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3149 stagingDesc.MiscFlags = 0;
3150
3151 ID3D11Texture2D* stagingTex = NULL;
3152 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3153 if (FAILED(result))
3154 {
3155 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3156 return;
3157 }
3158
3159 ID3D11Texture2D* srcTex = NULL;
3160 if (textureDesc.SampleDesc.Count > 1)
3161 {
3162 D3D11_TEXTURE2D_DESC resolveDesc;
3163 resolveDesc.Width = textureDesc.Width;
3164 resolveDesc.Height = textureDesc.Height;
3165 resolveDesc.MipLevels = 1;
3166 resolveDesc.ArraySize = 1;
3167 resolveDesc.Format = textureDesc.Format;
3168 resolveDesc.SampleDesc.Count = 1;
3169 resolveDesc.SampleDesc.Quality = 0;
3170 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3171 resolveDesc.BindFlags = 0;
3172 resolveDesc.CPUAccessFlags = 0;
3173 resolveDesc.MiscFlags = 0;
3174
3175 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3176 if (FAILED(result))
3177 {
3178 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003179 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003180 return;
3181 }
3182
3183 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3184 subResource = 0;
3185 }
3186 else
3187 {
3188 srcTex = texture;
3189 srcTex->AddRef();
3190 }
3191
3192 D3D11_BOX srcBox;
3193 srcBox.left = area.x;
3194 srcBox.right = area.x + area.width;
3195 srcBox.top = area.y;
3196 srcBox.bottom = area.y + area.height;
3197 srcBox.front = 0;
3198 srcBox.back = 1;
3199
3200 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3201
Geoff Langea228632013-07-30 15:17:12 -04003202 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003203
3204 D3D11_MAPPED_SUBRESOURCE mapping;
3205 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3206
3207 unsigned char *source;
3208 int inputPitch;
3209 if (packReverseRowOrder)
3210 {
3211 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3212 inputPitch = -static_cast<int>(mapping.RowPitch);
3213 }
3214 else
3215 {
3216 source = static_cast<unsigned char*>(mapping.pData);
3217 inputPitch = static_cast<int>(mapping.RowPitch);
3218 }
3219
Geoff Lang697ad3e2013-06-04 10:11:28 -04003220 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003221
Geoff Lang005df412013-10-16 14:12:50 -04003222 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003223 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3224 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3225
3226 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3227
3228 if (sourceFormat == format && sourceType == type)
3229 {
3230 // Direct copy possible
3231 unsigned char *dest = static_cast<unsigned char*>(pixels);
3232 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003233 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003234 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003235 }
3236 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003237 else
3238 {
Geoff Lang005df412013-10-16 14:12:50 -04003239 GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003240 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3241
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003242 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003243 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003244 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003245 // Fast copy is possible through some special function
3246 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003247 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003248 for (int x = 0; x < area.width; x++)
3249 {
3250 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3251 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3252
3253 fastCopyFunc(src, dest);
3254 }
3255 }
3256 }
3257 else
3258 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003259 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003260 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3261
3262 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3263 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3264 sizeof(temp) >= sizeof(gl::ColorUI) &&
3265 sizeof(temp) >= sizeof(gl::ColorI));
3266
3267 for (int y = 0; y < area.height; y++)
3268 {
3269 for (int x = 0; x < area.width; x++)
3270 {
3271 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3272 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3273
3274 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3275 // will not allow the copy otherwise.
3276 readFunc(src, temp);
3277 writeFunc(temp, dest);
3278 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003279 }
3280 }
3281 }
3282
3283 mDeviceContext->Unmap(stagingTex, 0);
3284
Geoff Langea228632013-07-30 15:17:12 -04003285 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003286}
3287
Geoff Lang758d5b22013-06-11 11:42:50 -04003288bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003289 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3290 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003291{
Geoff Lang975af372013-06-12 11:19:22 -04003292 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3293 // it should never be the case that both color and depth/stencil need to be blitted at
3294 // at the same time.
3295 ASSERT(colorBlit != (depthBlit || stencilBlit));
3296
Geoff Langc1f51be2013-06-11 11:49:14 -04003297 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003298
Geoff Lang4d782732013-07-22 10:44:18 -04003299 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3300 if (!drawRenderTarget)
3301 {
3302 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3303 return gl::error(GL_OUT_OF_MEMORY, false);
3304 }
3305
3306 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3307 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3308 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3309 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3310
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003311 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3312 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003313 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003314 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003315 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003316 }
3317
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003318 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003319 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003320 unsigned int readSubresource = 0;
3321 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003322 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003323 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3324 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003325
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003326 if (unresolvedTexture)
3327 {
3328 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3329 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003330
Geoff Langea228632013-07-30 15:17:12 -04003331 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003332
3333 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3334 if (FAILED(result))
3335 {
Geoff Langea228632013-07-30 15:17:12 -04003336 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003337 return gl::error(GL_OUT_OF_MEMORY, false);
3338 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003339 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003340 }
3341 else
3342 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003343 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003344 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003345 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003346 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003347 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003348 }
3349
Geoff Lang4d782732013-07-22 10:44:18 -04003350 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003351 {
Geoff Lang4d782732013-07-22 10:44:18 -04003352 SafeRelease(readTexture);
3353 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003354 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003355 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003356 }
3357
Geoff Lang125deab2013-08-09 13:34:16 -04003358 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3359 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3360
3361 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3362
3363 bool wholeBufferCopy = !scissorNeeded &&
3364 readRect.x == 0 && readRect.width == readSize.width &&
3365 readRect.y == 0 && readRect.height == readSize.height &&
3366 drawRect.x == 0 && drawRect.width == drawSize.width &&
3367 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003368
Geoff Langc1f51be2013-06-11 11:49:14 -04003369 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003370
Geoff Lang1cd1b212014-02-11 09:42:27 -05003371 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003372
3373 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3374 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3375 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3376 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3377
3378 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3379 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3380 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3381
Geoff Langc1f51be2013-06-11 11:49:14 -04003382 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003383 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3384 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003385 {
Geoff Lang125deab2013-08-09 13:34:16 -04003386 UINT dstX = drawRect.x;
3387 UINT dstY = drawRect.y;
3388
Geoff Langc1f51be2013-06-11 11:49:14 -04003389 D3D11_BOX readBox;
3390 readBox.left = readRect.x;
3391 readBox.right = readRect.x + readRect.width;
3392 readBox.top = readRect.y;
3393 readBox.bottom = readRect.y + readRect.height;
3394 readBox.front = 0;
3395 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003396
Geoff Lang125deab2013-08-09 13:34:16 -04003397 if (scissorNeeded)
3398 {
3399 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3400 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3401
3402 if (drawRect.x < scissor->x)
3403 {
3404 dstX = scissor->x;
3405 readBox.left += (scissor->x - drawRect.x);
3406 }
3407 if (drawRect.y < scissor->y)
3408 {
3409 dstY = scissor->y;
3410 readBox.top += (scissor->y - drawRect.y);
3411 }
3412 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3413 {
3414 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3415 }
3416 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3417 {
3418 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3419 }
3420 }
3421
Geoff Langc1f51be2013-06-11 11:49:14 -04003422 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3423 // We also require complete framebuffer copies for depth-stencil blit.
3424 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003425
Geoff Lang125deab2013-08-09 13:34:16 -04003426 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003427 readTexture, readSubresource, pSrcBox);
3428 result = true;
3429 }
3430 else
3431 {
3432 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003433 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003434
Geoff Lang975af372013-06-12 11:19:22 -04003435 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003436 {
Geoff Lang975af372013-06-12 11:19:22 -04003437 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003438 drawTexture, drawSubresource, drawArea, drawSize,
3439 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003440 }
3441 else if (depthBlit)
3442 {
Geoff Lang125deab2013-08-09 13:34:16 -04003443 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3444 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003445 }
3446 else if (stencilBlit)
3447 {
3448 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003449 drawTexture, drawSubresource, drawArea, drawSize,
3450 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003451 }
3452 else
3453 {
Geoff Lang685806d2013-06-12 11:16:36 -04003454 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003455 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3456 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003457 }
3458 }
3459
3460 SafeRelease(readTexture);
3461 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003462
3463 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003464}
3465
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003466ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3467{
3468 D3D11_TEXTURE2D_DESC textureDesc;
3469 source->GetDesc(&textureDesc);
3470
3471 if (textureDesc.SampleDesc.Count > 1)
3472 {
3473 D3D11_TEXTURE2D_DESC resolveDesc;
3474 resolveDesc.Width = textureDesc.Width;
3475 resolveDesc.Height = textureDesc.Height;
3476 resolveDesc.MipLevels = 1;
3477 resolveDesc.ArraySize = 1;
3478 resolveDesc.Format = textureDesc.Format;
3479 resolveDesc.SampleDesc.Count = 1;
3480 resolveDesc.SampleDesc.Quality = 0;
3481 resolveDesc.Usage = textureDesc.Usage;
3482 resolveDesc.BindFlags = textureDesc.BindFlags;
3483 resolveDesc.CPUAccessFlags = 0;
3484 resolveDesc.MiscFlags = 0;
3485
3486 ID3D11Texture2D *resolveTexture = NULL;
3487 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3488 if (FAILED(result))
3489 {
3490 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3491 return NULL;
3492 }
3493
3494 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3495 return resolveTexture;
3496 }
3497 else
3498 {
3499 source->AddRef();
3500 return source;
3501 }
3502}
3503
Geoff Lang42477a42013-09-17 17:07:02 -04003504void Renderer11::invalidateRenderbufferSwizzles(gl::Renderbuffer *renderBuffer, int mipLevel)
3505{
3506 TextureStorage *texStorage = renderBuffer->getTextureStorage();
3507 if (texStorage)
3508 {
3509 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3510 if (!texStorage11)
3511 {
3512 ERR("texture storage pointer unexpectedly null.");
3513 return;
3514 }
3515
3516 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3517 }
3518}
3519
3520void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3521{
3522 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3523 {
3524 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
3525 if (colorbuffer)
3526 {
3527 invalidateRenderbufferSwizzles(colorbuffer, framebuffer->getColorbufferMipLevel(colorAttachment));
3528 }
3529 }
3530
3531 gl::Renderbuffer *depthBuffer = framebuffer->getDepthbuffer();
3532 if (depthBuffer)
3533 {
3534 invalidateRenderbufferSwizzles(depthBuffer, framebuffer->getDepthbufferMipLevel());
3535 }
3536
3537 gl::Renderbuffer *stencilBuffer = framebuffer->getStencilbuffer();
3538 if (stencilBuffer)
3539 {
3540 invalidateRenderbufferSwizzles(stencilBuffer, framebuffer->getStencilbufferMipLevel());
3541 }
3542}
3543
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003544bool Renderer11::getLUID(LUID *adapterLuid) const
3545{
3546 adapterLuid->HighPart = 0;
3547 adapterLuid->LowPart = 0;
3548
3549 if (!mDxgiAdapter)
3550 {
3551 return false;
3552 }
3553
3554 DXGI_ADAPTER_DESC adapterDesc;
3555 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3556 {
3557 return false;
3558 }
3559
3560 *adapterLuid = adapterDesc.AdapterLuid;
3561 return true;
3562}
3563
Geoff Lang005df412013-10-16 14:12:50 -04003564GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003565{
3566 int clientVersion = getCurrentClientVersion();
3567 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3568}
3569
Jamie Madill95ffb862014-01-29 09:26:59 -05003570rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3571{
3572 return gl_d3d11::GetVertexConversionType(vertexFormat);
3573}
3574
3575GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3576{
3577 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3578}
3579
Geoff Lang61e49a52013-05-29 10:22:58 -04003580Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3581{
3582 MultisampleSupportInfo supportInfo = { 0 };
3583
3584 UINT formatSupport;
3585 HRESULT result;
3586
3587 result = mDevice->CheckFormatSupport(format, &formatSupport);
3588 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3589 {
3590 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3591 {
3592 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3593 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3594 {
3595 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3596 }
3597 else
3598 {
3599 supportInfo.qualityLevels[i - 1] = 0;
3600 }
3601 }
3602 }
3603
3604 return supportInfo;
3605}
3606
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003607}