blob: 89885cf5817cde91525cea6a06f8388e2284231f [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002//
Geoff Langeeba6e12014-02-03 13:12:30 -05003// Copyright (c) 2012-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// Renderer11.cpp: Implements a back-end specific class for the D3D11 renderer.
9
daniel@transgaming.com5503fd02012-11-28 19:38:57 +000010#include "libGLESv2/main.h"
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +000011#include "common/utilities.h"
daniel@transgaming.com18adad02012-11-28 21:04:03 +000012#include "libGLESv2/Buffer.h"
daniel@transgaming.com53670042012-11-28 20:55:51 +000013#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com80fc3322012-11-28 21:02:13 +000014#include "libGLESv2/Framebuffer.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000015#include "libGLESv2/RenderBuffer.h"
Geoff Langd47e0fc2013-08-29 11:40:43 -040016#include "libGLESv2/renderer/d3d11/Renderer11.h"
17#include "libGLESv2/renderer/d3d11/RenderTarget11.h"
18#include "libGLESv2/renderer/d3d11/renderer11_utils.h"
19#include "libGLESv2/renderer/d3d11/formatutils11.h"
20#include "libGLESv2/renderer/d3d11/ShaderExecutable11.h"
21#include "libGLESv2/renderer/d3d11/SwapChain11.h"
22#include "libGLESv2/renderer/d3d11/Image11.h"
23#include "libGLESv2/renderer/d3d11/VertexBuffer11.h"
24#include "libGLESv2/renderer/d3d11/IndexBuffer11.h"
25#include "libGLESv2/renderer/d3d11/BufferStorage11.h"
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000026#include "libGLESv2/renderer/VertexDataManager.h"
27#include "libGLESv2/renderer/IndexDataManager.h"
Geoff Langd47e0fc2013-08-29 11:40:43 -040028#include "libGLESv2/renderer/d3d11/TextureStorage11.h"
29#include "libGLESv2/renderer/d3d11/Query11.h"
30#include "libGLESv2/renderer/d3d11/Fence11.h"
31#include "libGLESv2/renderer/d3d11/Blit11.h"
32#include "libGLESv2/renderer/d3d11/Clear11.h"
33#include "libGLESv2/renderer/d3d11/PixelTransfer11.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000034#include "libEGL/Display.h"
35
Geoff Lang94a90892014-02-18 17:14:19 -050036// Enable ANGLE_SKIP_DXGI_1_2_CHECK if there is not a possibility of using cross-process
37// HWNDs or the Windows 7 Platform Update (KB2670838) is expected to be installed.
38#ifndef ANGLE_SKIP_DXGI_1_2_CHECK
39#define ANGLE_SKIP_DXGI_1_2_CHECK 0
40#endif
41
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000042#ifdef _DEBUG
43// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
44// and conformance tests. to enable all warnings, remove this define.
45#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
46#endif
47
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000048namespace rx
49{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000050static const DXGI_FORMAT RenderTargetFormats[] =
51 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000052 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000053 DXGI_FORMAT_R8G8B8A8_UNORM
54 };
55
56static const DXGI_FORMAT DepthStencilFormats[] =
57 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000058 DXGI_FORMAT_UNKNOWN,
59 DXGI_FORMAT_D24_UNORM_S8_UINT,
60 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000061 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000062
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000063enum
64{
65 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
66};
67
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000068Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
69{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000070 mVertexDataManager = NULL;
71 mIndexDataManager = NULL;
72
daniel@transgaming.comc5114302012-12-20 21:11:36 +000073 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000074 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000075
Geoff Langb86b9792013-06-04 16:32:05 -040076 mBlit = NULL;
Jamie Madilla21eea32013-09-18 14:36:25 -040077 mPixelTransfer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000078
Geoff Langda507fe2013-08-20 12:01:42 -040079 mClear = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000080
daniel@transgaming.combdf787f2013-02-01 03:20:36 +000081 mSyncQuery = NULL;
82
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000083 mD3d11Module = NULL;
84 mDxgiModule = NULL;
85
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000086 mDeviceLost = false;
87
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +000088 mMaxSupportedSamples = 0;
89
daniel@transgaming.com25072f62012-11-28 19:31:32 +000090 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000091 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000092 mDxgiAdapter = NULL;
93 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +000094
95 mDriverConstantBufferVS = NULL;
96 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +000097
Jamie Madill6246dc82014-01-29 09:26:47 -050098 mAppliedVertexShader = NULL;
99 mAppliedGeometryShader = NULL;
Geoff Lang4c5c6bb2014-02-05 16:32:46 -0500100 mCurPointGeometryShader = NULL;
Jamie Madill6246dc82014-01-29 09:26:47 -0500101 mAppliedPixelShader = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000102}
103
104Renderer11::~Renderer11()
105{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000106 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000107}
108
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000109Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
110{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000111 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000112 return static_cast<rx::Renderer11*>(renderer);
113}
114
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000115#ifndef __d3d11_1_h__
116#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
117#endif
118
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000119EGLint Renderer11::initialize()
120{
Geoff Langdad5ed32014-02-10 12:59:17 -0500121 if (!mCompiler.initialize())
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000122 {
123 return EGL_NOT_INITIALIZED;
124 }
125
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000126 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
127 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000128
129 if (mD3d11Module == NULL || mDxgiModule == NULL)
130 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000131 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000132 return EGL_NOT_INITIALIZED;
133 }
134
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000135 // create the D3D11 device
136 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000137 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000138
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000139 if (D3D11CreateDevice == NULL)
140 {
141 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
142 return EGL_NOT_INITIALIZED;
143 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000144
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000145 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000146 {
147 D3D_FEATURE_LEVEL_11_0,
148 D3D_FEATURE_LEVEL_10_1,
149 D3D_FEATURE_LEVEL_10_0,
150 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000151
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000152 HRESULT result = S_OK;
153
154#ifdef _DEBUG
155 result = D3D11CreateDevice(NULL,
156 D3D_DRIVER_TYPE_HARDWARE,
157 NULL,
158 D3D11_CREATE_DEVICE_DEBUG,
159 featureLevels,
160 ArraySize(featureLevels),
161 D3D11_SDK_VERSION,
162 &mDevice,
163 &mFeatureLevel,
164 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000165
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000166 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000167 {
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000168 ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n");
169 }
170
171 if (!mDevice || FAILED(result))
172#endif
173 {
174 result = D3D11CreateDevice(NULL,
175 D3D_DRIVER_TYPE_HARDWARE,
176 NULL,
177 0,
178 featureLevels,
179 ArraySize(featureLevels),
180 D3D11_SDK_VERSION,
181 &mDevice,
182 &mFeatureLevel,
183 &mDeviceContext);
184
185 if (!mDevice || FAILED(result))
186 {
187 ERR("Could not create D3D11 device - aborting!\n");
188 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
189 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000190 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000191
Geoff Lang94a90892014-02-18 17:14:19 -0500192#if !ANGLE_SKIP_DXGI_1_2_CHECK
193 // In order to create a swap chain for an HWND owned by another process, DXGI 1.2 is required.
194 // The easiest way to check is to query for a IDXGIDevice2.
195 bool requireDXGI1_2 = false;
196 HWND hwnd = WindowFromDC(mDc);
197 if (hwnd)
198 {
199 DWORD currentProcessId = GetCurrentProcessId();
200 DWORD wndProcessId;
201 GetWindowThreadProcessId(hwnd, &wndProcessId);
202 requireDXGI1_2 = (currentProcessId != wndProcessId);
203 }
204 else
205 {
206 requireDXGI1_2 = true;
207 }
208
209 if (requireDXGI1_2)
210 {
211 IDXGIDevice2 *dxgiDevice2 = NULL;
212 result = mDevice->QueryInterface(__uuidof(IDXGIDevice2), (void**)&dxgiDevice2);
213 if (FAILED(result))
214 {
215 ERR("DXGI 1.2 required to present to HWNDs owned by another process.\n");
216 return EGL_NOT_INITIALIZED;
217 }
218 SafeRelease(dxgiDevice2);
219 }
220#endif
221
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000222 IDXGIDevice *dxgiDevice = NULL;
223 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
224
225 if (FAILED(result))
226 {
227 ERR("Could not query DXGI device - aborting!\n");
228 return EGL_NOT_INITIALIZED;
229 }
230
231 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
232
233 if (FAILED(result))
234 {
235 ERR("Could not retrieve DXGI adapter - aborting!\n");
236 return EGL_NOT_INITIALIZED;
237 }
238
Geoff Langea228632013-07-30 15:17:12 -0400239 SafeRelease(dxgiDevice);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000240
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000241 mDxgiAdapter->GetDesc(&mAdapterDescription);
242 memset(mDescription, 0, sizeof(mDescription));
243 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
244
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000245 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
246
247 if (!mDxgiFactory || FAILED(result))
248 {
249 ERR("Could not create DXGI factory - aborting!\n");
250 return EGL_NOT_INITIALIZED;
251 }
252
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000253 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
254#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
255 ID3D11InfoQueue *infoQueue;
256 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
257
258 if (SUCCEEDED(result))
259 {
260 D3D11_MESSAGE_ID hideMessages[] =
261 {
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000262 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000263 };
264
265 D3D11_INFO_QUEUE_FILTER filter = {0};
266 filter.DenyList.NumIDs = ArraySize(hideMessages);
267 filter.DenyList.pIDList = hideMessages;
268
269 infoQueue->AddStorageFilterEntries(&filter);
Geoff Langea228632013-07-30 15:17:12 -0400270 SafeRelease(infoQueue);
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000271 }
272#endif
273
Geoff Lang61e49a52013-05-29 10:22:58 -0400274 mMaxSupportedSamples = 0;
275
276 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
277 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000278 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400279 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
280 mMultisampleSupportMap.insert(std::make_pair(*i, support));
281 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000282 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000283
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000284 initializeDevice();
285
286 return EGL_SUCCESS;
287}
288
289// do any one-time device initialization
290// NOTE: this is also needed after a device lost/reset
291// to reset the scene status and ensure the default states are reset.
292void Renderer11::initializeDevice()
293{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000294 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000295 mInputLayoutCache.initialize(mDevice, mDeviceContext);
296
297 ASSERT(!mVertexDataManager && !mIndexDataManager);
298 mVertexDataManager = new VertexDataManager(this);
299 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000300
Geoff Langb86b9792013-06-04 16:32:05 -0400301 ASSERT(!mBlit);
302 mBlit = new Blit11(this);
303
Geoff Langda507fe2013-08-20 12:01:42 -0400304 ASSERT(!mClear);
305 mClear = new Clear11(this);
306
Jamie Madilla21eea32013-09-18 14:36:25 -0400307 ASSERT(!mPixelTransfer);
308 mPixelTransfer = new PixelTransfer11(this);
309
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000310 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000311}
312
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000313int Renderer11::generateConfigs(ConfigDesc **configDescList)
314{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000315 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
316 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000317 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
318 int numConfigs = 0;
319
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000320 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000321 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000322 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000323 {
324 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
325
326 UINT formatSupport = 0;
327 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000328
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000329 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
330 {
331 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
332
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000333 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000334
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000335 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
336 {
Geoff Lang5c9a29a2014-02-11 10:26:24 -0500337 UINT depthStencilSupport = 0;
338 result = mDevice->CheckFormatSupport(depthStencilFormat, &depthStencilSupport);
339 depthStencilFormatOK = SUCCEEDED(result) && (depthStencilSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000340 }
341
342 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000343 {
Jamie Madilld6cb2442013-07-10 15:13:38 -0400344 // FIXME: parse types from context version
345 ASSERT(d3d11_gl::GetInternalFormat(renderTargetFormat, 2) == d3d11_gl::GetInternalFormat(renderTargetFormat, 3));
346 ASSERT(d3d11_gl::GetInternalFormat(depthStencilFormat, 2) == d3d11_gl::GetInternalFormat(depthStencilFormat, 3));
347
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000348 ConfigDesc newConfig;
Jamie Madilld6cb2442013-07-10 15:13:38 -0400349 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat, getCurrentClientVersion());
350 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat, getCurrentClientVersion());
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000351 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
352 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000353 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000354
355 (*configDescList)[numConfigs++] = newConfig;
356 }
357 }
358 }
359 }
360
361 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000362}
363
364void Renderer11::deleteConfigs(ConfigDesc *configDescList)
365{
366 delete [] (configDescList);
367}
368
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000369void Renderer11::sync(bool block)
370{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000371 if (block)
372 {
373 HRESULT result;
374
375 if (!mSyncQuery)
376 {
377 D3D11_QUERY_DESC queryDesc;
378 queryDesc.Query = D3D11_QUERY_EVENT;
379 queryDesc.MiscFlags = 0;
380
381 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
382 ASSERT(SUCCEEDED(result));
383 }
384
385 mDeviceContext->End(mSyncQuery);
386 mDeviceContext->Flush();
387
388 do
389 {
390 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
391
392 // Keep polling, but allow other threads to do something useful first
393 Sleep(0);
394
395 if (testDeviceLost(true))
396 {
397 return;
398 }
399 }
400 while (result == S_FALSE);
401 }
402 else
403 {
404 mDeviceContext->Flush();
405 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000406}
407
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000408SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
409{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000410 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000411}
412
Geoff Lange2e0ce02013-09-17 17:05:08 -0400413void Renderer11::generateSwizzle(gl::Texture *texture)
414{
Geoff Lang42477a42013-09-17 17:07:02 -0400415 if (texture)
416 {
417 TextureStorageInterface *texStorage = texture->getNativeTexture();
418 if (texStorage)
419 {
420 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
421
422 storage11->generateSwizzles(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
423 texture->getSwizzleAlpha());
424 }
425 }
Geoff Lange2e0ce02013-09-17 17:05:08 -0400426}
427
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000428void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
429{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000430 if (type == gl::SAMPLER_PIXEL)
431 {
432 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
433 {
434 ERR("Pixel shader sampler index %i is not valid.", index);
435 return;
436 }
437
438 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
439 {
440 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
441
442 if (!dxSamplerState)
443 {
444 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
445 "sampler state for pixel shaders at slot %i.", index);
446 }
447
448 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
449
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000450 mCurPixelSamplerStates[index] = samplerState;
451 }
452
453 mForceSetPixelSamplerStates[index] = false;
454 }
455 else if (type == gl::SAMPLER_VERTEX)
456 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000457 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000458 {
459 ERR("Vertex shader sampler index %i is not valid.", index);
460 return;
461 }
462
463 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
464 {
465 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
466
467 if (!dxSamplerState)
468 {
469 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
470 "sampler state for vertex shaders at slot %i.", index);
471 }
472
473 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
474
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000475 mCurVertexSamplerStates[index] = samplerState;
476 }
477
478 mForceSetVertexSamplerStates[index] = false;
479 }
480 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000481}
482
483void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
484{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000485 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000486 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000487
488 if (texture)
489 {
490 TextureStorageInterface *texStorage = texture->getNativeTexture();
491 if (texStorage)
492 {
493 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
Nicolas Capensa10c1c72014-03-27 11:20:52 -0400494 gl::SamplerState samplerState;
495 texture->getSamplerState(&samplerState);
496 textureSRV = storage11->getSRV(samplerState);
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000497 }
498
499 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
500 // missing the shader resource view
501 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000502
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000503 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000504 }
505
506 if (type == gl::SAMPLER_PIXEL)
507 {
508 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
509 {
510 ERR("Pixel shader sampler index %i is not valid.", index);
511 return;
512 }
513
Geoff Lang91382e52014-01-07 16:16:30 -0500514 if (forceSetTexture || mCurPixelSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000515 {
516 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
517 }
518
Geoff Lang91382e52014-01-07 16:16:30 -0500519 mCurPixelSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000520 }
521 else if (type == gl::SAMPLER_VERTEX)
522 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000523 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000524 {
525 ERR("Vertex shader sampler index %i is not valid.", index);
526 return;
527 }
528
Geoff Lang91382e52014-01-07 16:16:30 -0500529 if (forceSetTexture || mCurVertexSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000530 {
531 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
532 }
533
Geoff Lang91382e52014-01-07 16:16:30 -0500534 mCurVertexSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000535 }
536 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000537}
538
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000539bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
540{
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000541 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
542 {
543 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
544 if (uniformBuffer)
545 {
546 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500547 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000548
549 if (!constantBuffer)
550 {
551 return false;
552 }
553
Geoff Langc6354ee2013-07-22 10:40:07 -0400554 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
555 {
556 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
557 1, &constantBuffer);
558 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
559 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000560 }
561 }
562
563 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
564 {
565 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
566 if (uniformBuffer)
567 {
568 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500569 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000570
571 if (!constantBuffer)
572 {
573 return false;
574 }
575
Geoff Langc6354ee2013-07-22 10:40:07 -0400576 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
577 {
578 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
579 1, &constantBuffer);
580 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
581 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000582 }
583 }
584
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000585 return true;
586}
587
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000588void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000589{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000590 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000591 {
Nicolas Capensaea8e942014-05-09 19:14:08 -0400592 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000593 if (!dxRasterState)
594 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000595 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000596 "rasterizer state.");
597 }
598
599 mDeviceContext->RSSetState(dxRasterState);
600
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000601 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000602 }
603
604 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000605}
606
Geoff Langc142e9d2013-09-30 15:19:47 -0400607void Renderer11::setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000608 unsigned int sampleMask)
609{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000610 if (mForceSetBlendState ||
611 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400612 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000613 sampleMask != mCurSampleMask)
614 {
Geoff Langc142e9d2013-09-30 15:19:47 -0400615 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(framebuffer, blendState);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000616 if (!dxBlendState)
617 {
618 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
619 "blend state.");
620 }
621
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000622 float blendColors[4] = {0.0f};
623 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
624 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
625 {
626 blendColors[0] = blendColor.red;
627 blendColors[1] = blendColor.green;
628 blendColors[2] = blendColor.blue;
629 blendColors[3] = blendColor.alpha;
630 }
631 else
632 {
633 blendColors[0] = blendColor.alpha;
634 blendColors[1] = blendColor.alpha;
635 blendColors[2] = blendColor.alpha;
636 blendColors[3] = blendColor.alpha;
637 }
638
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000639 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
640
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000641 mCurBlendState = blendState;
642 mCurBlendColor = blendColor;
643 mCurSampleMask = sampleMask;
644 }
645
646 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000647}
648
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000649void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000650 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000651{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000652 if (mForceSetDepthStencilState ||
653 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
654 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
655 {
656 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
657 stencilRef != stencilBackRef ||
658 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
659 {
660 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
661 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000662 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000663 }
664
665 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
666 if (!dxDepthStencilState)
667 {
668 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
669 "setting the default depth stencil state.");
670 }
671
Jamie Madillec91cd32014-01-21 16:38:12 -0500672 // Max D3D11 stencil reference value is 0xFF, corresponding to the max 8 bits in a stencil buffer
673 // GL specifies we should clamp the ref value to the nearest bit depth when doing stencil ops
674 META_ASSERT(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF);
675 META_ASSERT(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF);
Geoff Lang0bf3a982014-02-11 09:40:48 -0500676 UINT dxStencilRef = std::min<UINT>(stencilRef, 0xFFu);
Jamie Madillec91cd32014-01-21 16:38:12 -0500677
Geoff Lang0bf3a982014-02-11 09:40:48 -0500678 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, dxStencilRef);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000679
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000680 mCurDepthStencilState = depthStencilState;
681 mCurStencilRef = stencilRef;
682 mCurStencilBackRef = stencilBackRef;
683 }
684
685 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000686}
687
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000688void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000689{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000690 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
691 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000692 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000693 if (enabled)
694 {
695 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000696 rect.left = std::max(0, scissor.x);
697 rect.top = std::max(0, scissor.y);
698 rect.right = scissor.x + std::max(0, scissor.width);
699 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000700
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000701 mDeviceContext->RSSetScissorRects(1, &rect);
702 }
703
704 if (enabled != mScissorEnabled)
705 {
706 mForceSetRasterState = true;
707 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000708
709 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000710 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000711 }
712
713 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000714}
715
daniel@transgaming.com12985182012-12-20 20:56:31 +0000716bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000717 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000718{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000719 gl::Rectangle actualViewport = viewport;
720 float actualZNear = gl::clamp01(zNear);
721 float actualZFar = gl::clamp01(zFar);
722 if (ignoreViewport)
723 {
724 actualViewport.x = 0;
725 actualViewport.y = 0;
726 actualViewport.width = mRenderTargetDesc.width;
727 actualViewport.height = mRenderTargetDesc.height;
728 actualZNear = 0.0f;
729 actualZFar = 1.0f;
730 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000731
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000732 // Get D3D viewport bounds, which depends on the feature level
733 const Range& viewportBounds = getViewportBounds();
734
735 // 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 +0000736 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000737 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
738 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
739 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
740 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
741 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
742 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000743 dxViewport.MinDepth = actualZNear;
744 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000745
746 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
747 {
748 return false; // Nothing to render
749 }
750
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000751 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
752 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000753
daniel@transgaming.com53670042012-11-28 20:55:51 +0000754 if (viewportChanged)
755 {
756 mDeviceContext->RSSetViewports(1, &dxViewport);
757
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000758 mCurViewport = actualViewport;
759 mCurNear = actualZNear;
760 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000761
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000762 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
763 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
764 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
765 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000766
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000767 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
768 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000769
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000770 mVertexConstants.depthRange[0] = actualZNear;
771 mVertexConstants.depthRange[1] = actualZFar;
772 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000773
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000774 mPixelConstants.depthRange[0] = actualZNear;
775 mPixelConstants.depthRange[1] = actualZFar;
776 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000777 }
778
779 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000780 return true;
781}
782
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000783bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
784{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000785 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000786
Geoff Lang57e713e2013-07-31 17:01:58 -0400787 GLsizei minCount = 0;
788
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000789 switch (mode)
790 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400791 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
792 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
793 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
794 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
795 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
796 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000797 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400798 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000799 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000800 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000801 }
802
Geoff Lang4c095862013-07-22 10:43:36 -0400803 if (primitiveTopology != mCurrentPrimitiveTopology)
804 {
805 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
806 mCurrentPrimitiveTopology = primitiveTopology;
807 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000808
Geoff Lang57e713e2013-07-31 17:01:58 -0400809 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000810}
811
812bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000813{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000814 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000815 // Also extract the render target dimensions and view
816 unsigned int renderTargetWidth = 0;
817 unsigned int renderTargetHeight = 0;
818 GLenum renderTargetFormat = 0;
819 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
820 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
821 bool missingColorRenderTarget = true;
822
823 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000824 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000825 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
826
827 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000828 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000829 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
830 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
831
Jamie Madill3c7fa222014-06-05 13:08:51 -0400832 gl::FramebufferAttachment *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000833
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000834 if (!colorbuffer)
835 {
836 ERR("render target pointer unexpectedly null.");
837 return false;
838 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000839
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000840 // check for zero-sized default framebuffer, which is a special case.
841 // in this case we do not wish to modify any state and just silently return false.
842 // this will not report any gl error but will cause the calling method to return.
843 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
844 {
845 return false;
846 }
847
848 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
849
850 // Extract the render target dimensions and view
851 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
852 if (!renderTarget)
853 {
854 ERR("render target pointer unexpectedly null.");
855 return false;
856 }
857
858 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
859 if (!framebufferRTVs[colorAttachment])
860 {
861 ERR("render target view pointer unexpectedly null.");
862 return false;
863 }
864
865 if (missingColorRenderTarget)
866 {
867 renderTargetWidth = colorbuffer->getWidth();
868 renderTargetHeight = colorbuffer->getHeight();
869 renderTargetFormat = colorbuffer->getActualFormat();
870 missingColorRenderTarget = false;
871 }
Jamie Madillba597af2013-10-22 13:12:15 -0400872
Geoff Lang91382e52014-01-07 16:16:30 -0500873 // TODO: Detect if this color buffer is already bound as a texture and unbind it first to prevent
874 // D3D11 warnings.
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000875 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000876 }
877
878 // Get the depth stencil render buffer and serials
Jamie Madill3c7fa222014-06-05 13:08:51 -0400879 gl::FramebufferAttachment *depthStencil = NULL;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000880 unsigned int depthbufferSerial = 0;
881 unsigned int stencilbufferSerial = 0;
882 if (framebuffer->getDepthbufferType() != GL_NONE)
883 {
884 depthStencil = framebuffer->getDepthbuffer();
885 if (!depthStencil)
886 {
887 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000888 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000889 return false;
890 }
891
892 depthbufferSerial = depthStencil->getSerial();
893 }
894 else if (framebuffer->getStencilbufferType() != GL_NONE)
895 {
896 depthStencil = framebuffer->getStencilbuffer();
897 if (!depthStencil)
898 {
899 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000900 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000901 return false;
902 }
903
904 stencilbufferSerial = depthStencil->getSerial();
905 }
906
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000907 ID3D11DepthStencilView* framebufferDSV = NULL;
908 if (depthStencil)
909 {
910 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
911 if (!depthStencilRenderTarget)
912 {
913 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000914 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000915 return false;
916 }
917
918 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
919 if (!framebufferDSV)
920 {
921 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000922 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000923 return false;
924 }
925
926 // If there is no render buffer, the width, height and format values come from
927 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000928 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000929 {
930 renderTargetWidth = depthStencil->getWidth();
931 renderTargetHeight = depthStencil->getHeight();
932 renderTargetFormat = depthStencil->getActualFormat();
933 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000934 }
935
936 // Apply the render target and depth stencil
937 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000938 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000939 depthbufferSerial != mAppliedDepthbufferSerial ||
940 stencilbufferSerial != mAppliedStencilbufferSerial)
941 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000942 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000943
944 mRenderTargetDesc.width = renderTargetWidth;
945 mRenderTargetDesc.height = renderTargetHeight;
946 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000947 mForceSetViewport = true;
948 mForceSetScissor = true;
Geoff Langc142e9d2013-09-30 15:19:47 -0400949 mForceSetBlendState = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000950
Nicolas Capensaea8e942014-05-09 19:14:08 -0400951 if (!mDepthStencilInitialized)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000952 {
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000953 mForceSetRasterState = true;
954 }
955
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000956 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
957 {
958 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
959 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000960 mAppliedDepthbufferSerial = depthbufferSerial;
961 mAppliedStencilbufferSerial = stencilbufferSerial;
962 mRenderTargetDescInitialized = true;
963 mDepthStencilInitialized = true;
964 }
965
Geoff Lang42477a42013-09-17 17:07:02 -0400966 invalidateFramebufferSwizzles(framebuffer);
967
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000968 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000969}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000970
Jamie Madill57a89722013-07-02 11:57:03 -0400971GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -0400972 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000973{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000974 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -0400975 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000976 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000977 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000978 return err;
979 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000980
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000981 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000982}
983
daniel@transgaming.com31240482012-11-28 21:06:41 +0000984GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000985{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000986 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000987
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000988 if (err == GL_NO_ERROR)
989 {
Geoff Lang7840b172014-03-13 11:20:44 -0400990 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
991
992 ID3D11Buffer *buffer = NULL;
993 DXGI_FORMAT bufferFormat = indexBuffer->getIndexFormat();
994
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000995 if (indexInfo->storage)
996 {
Geoff Lang7840b172014-03-13 11:20:44 -0400997 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
998 buffer = storage->getBuffer(BUFFER_USAGE_INDEX);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000999 }
Geoff Lang7840b172014-03-13 11:20:44 -04001000 else
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001001 {
Geoff Lang7840b172014-03-13 11:20:44 -04001002 buffer = indexBuffer->getBuffer();
1003 }
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001004
Geoff Lang7840b172014-03-13 11:20:44 -04001005 if (buffer != mAppliedIB || bufferFormat != mAppliedIBFormat || indexInfo->startOffset != mAppliedIBOffset)
1006 {
1007 mDeviceContext->IASetIndexBuffer(buffer, bufferFormat, indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001008
Geoff Lang7840b172014-03-13 11:20:44 -04001009 mAppliedIB = buffer;
1010 mAppliedIBFormat = bufferFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001011 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001012 }
1013 }
1014
1015 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001016}
1017
Geoff Langeeba6e12014-02-03 13:12:30 -05001018void Renderer11::applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[])
1019{
1020 ID3D11Buffer* d3dBuffers[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1021 UINT d3dOffsets[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1022 bool requiresUpdate = false;
1023 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1024 {
1025 if (transformFeedbackBuffers[i])
1026 {
1027 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(transformFeedbackBuffers[i]->getStorage());
1028 ID3D11Buffer *buffer = storage->getBuffer(BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK);
1029
1030 d3dBuffers[i] = buffer;
1031 d3dOffsets[i] = (mAppliedTFBuffers[i] != buffer) ? static_cast<UINT>(offsets[i]) : -1;
1032 }
1033 else
1034 {
1035 d3dBuffers[i] = NULL;
1036 d3dOffsets[i] = 0;
1037 }
1038
1039 if (d3dBuffers[i] != mAppliedTFBuffers[i] || offsets[i] != mAppliedTFOffsets[i])
1040 {
1041 requiresUpdate = true;
1042 }
1043 }
1044
1045 if (requiresUpdate)
1046 {
1047 mDeviceContext->SOSetTargets(ArraySize(d3dBuffers), d3dBuffers, d3dOffsets);
1048 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1049 {
1050 mAppliedTFBuffers[i] = d3dBuffers[i];
1051 mAppliedTFOffsets[i] = offsets[i];
1052 }
1053 }
1054}
1055
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001056void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001057{
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001058 if (mode == GL_POINTS && transformFeedbackActive)
1059 {
1060 // Since point sprites are generated with a geometry shader, too many vertices will
1061 // be written if transform feedback is active. To work around this, draw only the points
1062 // with the stream out shader and no pixel shader to feed the stream out buffers and then
1063 // draw again with the point sprite geometry shader to rasterize the point sprites.
1064
1065 mDeviceContext->PSSetShader(NULL, NULL, 0);
1066
1067 if (instances > 0)
1068 {
1069 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1070 }
1071 else
1072 {
1073 mDeviceContext->Draw(count, 0);
1074 }
1075
1076 mDeviceContext->GSSetShader(mCurPointGeometryShader, NULL, 0);
1077 mDeviceContext->PSSetShader(mAppliedPixelShader, NULL, 0);
1078
1079 if (instances > 0)
1080 {
1081 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1082 }
1083 else
1084 {
1085 mDeviceContext->Draw(count, 0);
1086 }
1087
1088 mDeviceContext->GSSetShader(mAppliedGeometryShader, NULL, 0);
1089 }
1090 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001091 {
1092 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1093 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001094 else if (mode == GL_TRIANGLE_FAN)
1095 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001096 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001097 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001098 else if (instances > 0)
1099 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001100 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001101 }
1102 else
1103 {
1104 mDeviceContext->Draw(count, 0);
1105 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001106}
1107
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001108void Renderer11::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices,
1109 gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001110{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001111 if (mode == GL_LINE_LOOP)
1112 {
1113 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1114 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001115 else if (mode == GL_TRIANGLE_FAN)
1116 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001117 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1118 }
1119 else if (instances > 0)
1120 {
1121 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001122 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001123 else
1124 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001125 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001126 }
1127}
1128
1129void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1130{
1131 // Get the raw indices for an indexed draw
1132 if (type != GL_NONE && elementArrayBuffer)
1133 {
1134 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001135 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001136 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001137 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001138 }
1139
1140 if (!mLineLoopIB)
1141 {
1142 mLineLoopIB = new StreamingIndexBufferInterface(this);
1143 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1144 {
1145 delete mLineLoopIB;
1146 mLineLoopIB = NULL;
1147
1148 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001149 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001150 }
1151 }
1152
Geoff Lang57e713e2013-07-31 17:01:58 -04001153 // Checked by Renderer11::applyPrimitiveType
1154 ASSERT(count >= 0);
1155
1156 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001157 {
1158 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1159 return gl::error(GL_OUT_OF_MEMORY);
1160 }
1161
Geoff Lang57e713e2013-07-31 17:01:58 -04001162 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001163 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1164 {
1165 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001166 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001167 }
1168
1169 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001170 unsigned int offset;
1171 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001172 {
1173 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001174 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001175 }
1176
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001177 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001178 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001179
1180 switch (type)
1181 {
1182 case GL_NONE: // Non-indexed draw
1183 for (int i = 0; i < count; i++)
1184 {
1185 data[i] = i;
1186 }
1187 data[count] = 0;
1188 break;
1189 case GL_UNSIGNED_BYTE:
1190 for (int i = 0; i < count; i++)
1191 {
1192 data[i] = static_cast<const GLubyte*>(indices)[i];
1193 }
1194 data[count] = static_cast<const GLubyte*>(indices)[0];
1195 break;
1196 case GL_UNSIGNED_SHORT:
1197 for (int i = 0; i < count; i++)
1198 {
1199 data[i] = static_cast<const GLushort*>(indices)[i];
1200 }
1201 data[count] = static_cast<const GLushort*>(indices)[0];
1202 break;
1203 case GL_UNSIGNED_INT:
1204 for (int i = 0; i < count; i++)
1205 {
1206 data[i] = static_cast<const GLuint*>(indices)[i];
1207 }
1208 data[count] = static_cast<const GLuint*>(indices)[0];
1209 break;
1210 default: UNREACHABLE();
1211 }
1212
1213 if (!mLineLoopIB->unmapBuffer())
1214 {
1215 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001216 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001217 }
1218
Geoff Lang7840b172014-03-13 11:20:44 -04001219 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1220 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1221 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001222
Geoff Lang7840b172014-03-13 11:20:44 -04001223 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1224 {
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001225 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001226 mAppliedIB = d3dIndexBuffer;
1227 mAppliedIBFormat = indexFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001228 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001229 }
1230
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001231 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001232}
1233
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001234void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001235{
1236 // Get the raw indices for an indexed draw
1237 if (type != GL_NONE && elementArrayBuffer)
1238 {
1239 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001240 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001241 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001242 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001243 }
1244
1245 if (!mTriangleFanIB)
1246 {
1247 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1248 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1249 {
1250 delete mTriangleFanIB;
1251 mTriangleFanIB = NULL;
1252
1253 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001254 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001255 }
1256 }
1257
Geoff Lang57e713e2013-07-31 17:01:58 -04001258 // Checked by Renderer11::applyPrimitiveType
1259 ASSERT(count >= 3);
1260
Geoff Langeadfd572013-07-09 15:55:07 -04001261 const unsigned int numTris = count - 2;
1262
Geoff Lang57e713e2013-07-31 17:01:58 -04001263 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001264 {
1265 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1266 return gl::error(GL_OUT_OF_MEMORY);
1267 }
1268
1269 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001270 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1271 {
1272 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001273 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001274 }
1275
1276 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001277 unsigned int offset;
1278 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001279 {
1280 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001281 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001282 }
1283
1284 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001285 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001286
1287 switch (type)
1288 {
1289 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001290 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001291 {
1292 data[i*3 + 0] = 0;
1293 data[i*3 + 1] = i + 1;
1294 data[i*3 + 2] = i + 2;
1295 }
1296 break;
1297 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001298 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001299 {
1300 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1301 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1302 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1303 }
1304 break;
1305 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001306 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001307 {
1308 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1309 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1310 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1311 }
1312 break;
1313 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001314 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001315 {
1316 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1317 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1318 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1319 }
1320 break;
1321 default: UNREACHABLE();
1322 }
1323
1324 if (!mTriangleFanIB->unmapBuffer())
1325 {
1326 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001327 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001328 }
1329
Geoff Lang7840b172014-03-13 11:20:44 -04001330 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1331 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1332 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001333
Geoff Lang7840b172014-03-13 11:20:44 -04001334 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1335 {
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001336 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001337 mAppliedIB = d3dIndexBuffer;
1338 mAppliedIBFormat = indexFormat;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001339 mAppliedIBOffset = indexBufferOffset;
1340 }
1341
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001342 if (instances > 0)
1343 {
1344 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1345 }
1346 else
1347 {
1348 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1349 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001350}
1351
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001352void Renderer11::applyShaders(gl::ProgramBinary *programBinary, bool rasterizerDiscard, bool transformFeedbackActive, const gl::VertexFormat inputLayout[])
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001353{
Jamie Madillc5a83002014-02-14 16:41:25 -05001354 ShaderExecutable *vertexExe = programBinary->getVertexExecutableForInputLayout(inputLayout);
Jamie Madill6246dc82014-01-29 09:26:47 -05001355 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
1356 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001357
Jamie Madill6246dc82014-01-29 09:26:47 -05001358 ID3D11VertexShader *vertexShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader() : NULL);
Jamie Madill6246dc82014-01-29 09:26:47 -05001359
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001360 ID3D11PixelShader *pixelShader = NULL;
1361 // Skip pixel shader if we're doing rasterizer discard.
1362 if (!rasterizerDiscard)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001363 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001364 pixelShader = (pixelExe ? ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader() : NULL);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001365 }
1366
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001367 ID3D11GeometryShader *geometryShader = NULL;
1368 if (transformFeedbackActive)
Geoff Lang0550d032014-01-30 11:29:07 -05001369 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001370 geometryShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getStreamOutShader() : NULL);
1371 }
1372 else if (mCurRasterState.pointDrawMode)
1373 {
1374 geometryShader = (geometryExe ? ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader() : NULL);
Geoff Lang0550d032014-01-30 11:29:07 -05001375 }
1376
Jamie Madill6246dc82014-01-29 09:26:47 -05001377 bool dirtyUniforms = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001378
Jamie Madill6246dc82014-01-29 09:26:47 -05001379 if (vertexShader != mAppliedVertexShader)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001380 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001381 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1382 mAppliedVertexShader = vertexShader;
1383 dirtyUniforms = true;
1384 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001385
Jamie Madill6246dc82014-01-29 09:26:47 -05001386 if (geometryShader != mAppliedGeometryShader)
1387 {
1388 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1389 mAppliedGeometryShader = geometryShader;
1390 dirtyUniforms = true;
1391 }
1392
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001393 if (geometryExe && mCurRasterState.pointDrawMode)
1394 {
1395 mCurPointGeometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
1396 }
1397 else
1398 {
1399 mCurPointGeometryShader = NULL;
1400 }
1401
Jamie Madill6246dc82014-01-29 09:26:47 -05001402 if (pixelShader != mAppliedPixelShader)
1403 {
1404 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1405 mAppliedPixelShader = pixelShader;
1406 dirtyUniforms = true;
1407 }
1408
1409 if (dirtyUniforms)
1410 {
1411 programBinary->dirtyAllUniforms();
daniel@transgaming.come4991412012-12-20 20:55:34 +00001412 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001413}
1414
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001415void Renderer11::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001416{
Jamie Madill834e8b72014-04-11 13:33:58 -04001417 const std::vector<gl::LinkedUniform*> &uniformArray = programBinary.getUniforms();
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001418
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001419 unsigned int totalRegisterCountVS = 0;
1420 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001421
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001422 bool vertexUniformsDirty = false;
1423 bool pixelUniformsDirty = false;
1424
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001425 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001426 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001427 const gl::LinkedUniform &uniform = *uniformArray[uniformIndex];
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001428
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001429 if (uniform.isReferencedByVertexShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001430 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001431 totalRegisterCountVS += uniform.registerCount;
1432 vertexUniformsDirty = (vertexUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001433 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001434
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001435 if (uniform.isReferencedByFragmentShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001436 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001437 totalRegisterCountPS += uniform.registerCount;
1438 pixelUniformsDirty = (pixelUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001439 }
1440 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001441
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001442 const UniformStorage11 *vertexUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getVertexUniformStorage());
1443 const UniformStorage11 *fragmentUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getFragmentUniformStorage());
1444 ASSERT(vertexUniformStorage);
1445 ASSERT(fragmentUniformStorage);
1446
1447 ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
1448 ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001449
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001450 float (*mapVS)[4] = NULL;
1451 float (*mapPS)[4] = NULL;
1452
Shannon Woods5ab33c82013-06-26 15:31:09 -04001453 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1454 {
1455 D3D11_MAPPED_SUBRESOURCE map = {0};
1456 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001457 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001458 ASSERT(SUCCEEDED(result));
1459 mapVS = (float(*)[4])map.pData;
1460 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001461
Shannon Woods5ab33c82013-06-26 15:31:09 -04001462 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1463 {
1464 D3D11_MAPPED_SUBRESOURCE map = {0};
1465 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001466 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001467 ASSERT(SUCCEEDED(result));
1468 mapPS = (float(*)[4])map.pData;
1469 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001470
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001471 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001472 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001473 gl::LinkedUniform *uniform = uniformArray[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001474
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001475 if (!uniform->isSampler())
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001476 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001477 unsigned int componentCount = (4 - uniform->registerElement);
1478
Jamie Madill71cc91f2013-09-18 12:51:22 -04001479 // 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 -04001480 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001481
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001482 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001483 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001484 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001485 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001486
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001487 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001488 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001489 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001490 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001491 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001492 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001493
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001494 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001495 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001496 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001497 }
1498
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001499 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001500 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001501 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001502 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001503
1504 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1505 {
1506 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1507 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1508 }
1509
1510 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1511 {
1512 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1513 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1514 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001515
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001516 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001517 if (!mDriverConstantBufferVS)
1518 {
1519 D3D11_BUFFER_DESC constantBufferDescription = {0};
1520 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1521 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1522 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1523 constantBufferDescription.CPUAccessFlags = 0;
1524 constantBufferDescription.MiscFlags = 0;
1525 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001526
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001527 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001528 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001529 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001530
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001531 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1532 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001533
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001534 if (!mDriverConstantBufferPS)
1535 {
1536 D3D11_BUFFER_DESC constantBufferDescription = {0};
1537 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1538 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1539 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1540 constantBufferDescription.CPUAccessFlags = 0;
1541 constantBufferDescription.MiscFlags = 0;
1542 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001543
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001544 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001545 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001546 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001547
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001548 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1549 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001550
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001551 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1552 {
1553 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1554 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1555 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001556
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001557 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1558 {
1559 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1560 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1561 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001562
1563 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001564 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1565 {
1566 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1567 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1568 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001569}
1570
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001571void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001572{
Geoff Langda507fe2013-08-20 12:01:42 -04001573 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001574 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001575}
1576
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001577void Renderer11::markAllStateDirty()
1578{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001579 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1580 {
1581 mAppliedRenderTargetSerials[rtIndex] = 0;
1582 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001583 mAppliedDepthbufferSerial = 0;
1584 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001585 mDepthStencilInitialized = false;
1586 mRenderTargetDescInitialized = false;
1587
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001588 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001589 {
1590 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001591 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001592 }
1593 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1594 {
1595 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001596 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001597 }
1598
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001599 mForceSetBlendState = true;
1600 mForceSetRasterState = true;
1601 mForceSetDepthStencilState = true;
1602 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001603 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001604
Geoff Lang7840b172014-03-13 11:20:44 -04001605 mAppliedIB = NULL;
1606 mAppliedIBFormat = DXGI_FORMAT_UNKNOWN;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001607 mAppliedIBOffset = 0;
1608
Jamie Madill6246dc82014-01-29 09:26:47 -05001609 mAppliedVertexShader = NULL;
1610 mAppliedGeometryShader = NULL;
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001611 mCurPointGeometryShader = NULL;
Jamie Madill6246dc82014-01-29 09:26:47 -05001612 mAppliedPixelShader = NULL;
Geoff Langeeba6e12014-02-03 13:12:30 -05001613
1614 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1615 {
1616 mAppliedTFBuffers[i] = NULL;
1617 mAppliedTFOffsets[i] = 0;
1618 }
1619
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001620 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1621 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001622
1623 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001624
1625 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1626 {
1627 mCurrentConstantBufferVS[i] = -1;
1628 mCurrentConstantBufferPS[i] = -1;
1629 }
1630
1631 mCurrentVertexConstantBuffer = NULL;
1632 mCurrentPixelConstantBuffer = NULL;
1633 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001634
1635 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001636}
1637
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001638void Renderer11::releaseDeviceResources()
1639{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001640 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001641 mInputLayoutCache.clear();
1642
Geoff Langea228632013-07-30 15:17:12 -04001643 SafeDelete(mVertexDataManager);
1644 SafeDelete(mIndexDataManager);
1645 SafeDelete(mLineLoopIB);
1646 SafeDelete(mTriangleFanIB);
1647 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001648 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001649 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001650
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001651 SafeRelease(mDriverConstantBufferVS);
1652 SafeRelease(mDriverConstantBufferPS);
1653 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001654}
1655
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001656void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001657{
1658 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001659 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001660}
1661
1662bool Renderer11::isDeviceLost()
1663{
1664 return mDeviceLost;
1665}
1666
1667// set notify to true to broadcast a message to all contexts of the device loss
1668bool Renderer11::testDeviceLost(bool notify)
1669{
1670 bool isLost = false;
1671
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001672 // GetRemovedReason is used to test if the device is removed
1673 HRESULT result = mDevice->GetDeviceRemovedReason();
1674 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001675
1676 if (isLost)
1677 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001678 // Log error if this is a new device lost event
1679 if (mDeviceLost == false)
1680 {
1681 ERR("The D3D11 device was removed: 0x%08X", result);
1682 }
1683
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001684 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001685 // we'll probably get this done again by notifyDeviceLost
1686 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001687 // Note that we don't want to clear the device loss status here
1688 // -- this needs to be done by resetDevice
1689 mDeviceLost = true;
1690 if (notify)
1691 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001692 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001693 }
1694 }
1695
1696 return isLost;
1697}
1698
1699bool Renderer11::testDeviceResettable()
1700{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001701 // determine if the device is resettable by creating a dummy device
1702 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001703
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001704 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001705 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001706 return false;
1707 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001708
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001709 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001710 {
1711 D3D_FEATURE_LEVEL_11_0,
1712 D3D_FEATURE_LEVEL_10_1,
1713 D3D_FEATURE_LEVEL_10_0,
1714 };
1715
1716 ID3D11Device* dummyDevice;
1717 D3D_FEATURE_LEVEL dummyFeatureLevel;
1718 ID3D11DeviceContext* dummyContext;
1719
1720 HRESULT result = D3D11CreateDevice(NULL,
1721 D3D_DRIVER_TYPE_HARDWARE,
1722 NULL,
1723 #if defined(_DEBUG)
1724 D3D11_CREATE_DEVICE_DEBUG,
1725 #else
1726 0,
1727 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001728 featureLevels,
1729 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001730 D3D11_SDK_VERSION,
1731 &dummyDevice,
1732 &dummyFeatureLevel,
1733 &dummyContext);
1734
1735 if (!mDevice || FAILED(result))
1736 {
1737 return false;
1738 }
1739
Geoff Langea228632013-07-30 15:17:12 -04001740 SafeRelease(dummyContext);
1741 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001742
1743 return true;
1744}
1745
1746void Renderer11::release()
1747{
1748 releaseDeviceResources();
1749
Geoff Langea228632013-07-30 15:17:12 -04001750 SafeRelease(mDxgiFactory);
1751 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001752
1753 if (mDeviceContext)
1754 {
1755 mDeviceContext->ClearState();
1756 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001757 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001758 }
1759
Geoff Langea228632013-07-30 15:17:12 -04001760 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001761
1762 if (mD3d11Module)
1763 {
1764 FreeLibrary(mD3d11Module);
1765 mD3d11Module = NULL;
1766 }
1767
1768 if (mDxgiModule)
1769 {
1770 FreeLibrary(mDxgiModule);
1771 mDxgiModule = NULL;
1772 }
Geoff Langdad5ed32014-02-10 12:59:17 -05001773
1774 mCompiler.release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001775}
1776
1777bool Renderer11::resetDevice()
1778{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001779 // recreate everything
1780 release();
1781 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001782
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001783 if (result != EGL_SUCCESS)
1784 {
1785 ERR("Could not reinitialize D3D11 device: %08X", result);
1786 return false;
1787 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001788
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001789 mDeviceLost = false;
1790
1791 return true;
1792}
1793
1794DWORD Renderer11::getAdapterVendor() const
1795{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001796 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001797}
1798
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001799std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001800{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001801 std::ostringstream rendererString;
1802
1803 rendererString << mDescription;
1804 rendererString << " Direct3D11";
1805
1806 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1807 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1808
1809 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001810}
1811
1812GUID Renderer11::getAdapterIdentifier() const
1813{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001814 // Use the adapter LUID as our adapter ID
1815 // This number is local to a machine is only guaranteed to be unique between restarts
1816 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1817 GUID adapterId = {0};
1818 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1819 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001820}
1821
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001822Range Renderer11::getViewportBounds() const
1823{
1824 switch (mFeatureLevel)
1825 {
1826 case D3D_FEATURE_LEVEL_11_0:
1827 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1828 case D3D_FEATURE_LEVEL_10_1:
1829 case D3D_FEATURE_LEVEL_10_0:
1830 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1831 default: UNREACHABLE();
1832 return Range(0, 0);
1833 }
1834}
1835
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001836unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001837{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001838 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1839 switch (mFeatureLevel)
1840 {
1841 case D3D_FEATURE_LEVEL_11_0:
1842 case D3D_FEATURE_LEVEL_10_1:
1843 case D3D_FEATURE_LEVEL_10_0:
1844 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1845 default: UNREACHABLE();
1846 return 0;
1847 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001848}
1849
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001850unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1851{
1852 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1853}
1854
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001855unsigned int Renderer11::getReservedVertexUniformVectors() const
1856{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001857 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001858}
1859
1860unsigned int Renderer11::getReservedFragmentUniformVectors() const
1861{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001862 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001863}
1864
1865unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001866{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001867 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1868 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1869 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001870}
1871
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001872unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001873{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001874 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1875 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1876 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001877}
1878
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001879unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001880{
1881 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001882 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1883 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001884 switch (mFeatureLevel)
1885 {
1886 case D3D_FEATURE_LEVEL_11_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001887 return D3D11_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001888 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001889 return D3D10_1_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001890 case D3D_FEATURE_LEVEL_10_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001891 return D3D10_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001892 default: UNREACHABLE();
1893 return 0;
1894 }
1895}
1896
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001897unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
1898{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001899 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1900 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1901
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001902 switch (mFeatureLevel)
1903 {
1904 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001905 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001906 case D3D_FEATURE_LEVEL_10_1:
1907 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001908 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001909 default: UNREACHABLE();
1910 return 0;
1911 }
1912}
1913
1914unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
1915{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001916 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1917 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1918
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001919 switch (mFeatureLevel)
1920 {
1921 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001922 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001923 case D3D_FEATURE_LEVEL_10_1:
1924 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001925 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001926 default: UNREACHABLE();
1927 return 0;
1928 }
1929}
1930
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001931unsigned int Renderer11::getReservedVertexUniformBuffers() const
1932{
1933 // we reserve one buffer for the application uniforms, and one for driver uniforms
1934 return 2;
1935}
1936
1937unsigned int Renderer11::getReservedFragmentUniformBuffers() const
1938{
1939 // we reserve one buffer for the application uniforms, and one for driver uniforms
1940 return 2;
1941}
1942
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001943unsigned int Renderer11::getReservedVaryings() const
1944{
1945 // We potentially reserve varyings for gl_Position, _dx_Position, gl_FragCoord and gl_PointSize
1946 return 4;
1947}
1948
1949
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001950unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
1951{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001952 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
1953 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
1954
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001955 switch (mFeatureLevel)
1956 {
1957 case D3D_FEATURE_LEVEL_11_0:
1958 return D3D11_SO_BUFFER_SLOT_COUNT;
1959 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001960 return D3D10_1_SO_BUFFER_SLOT_COUNT;
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001961 case D3D_FEATURE_LEVEL_10_0:
1962 return D3D10_SO_BUFFER_SLOT_COUNT;
1963 default: UNREACHABLE();
1964 return 0;
1965 }
1966}
1967
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001968unsigned int Renderer11::getMaxTransformFeedbackSeparateComponents() const
1969{
1970 switch (mFeatureLevel)
1971 {
1972 case D3D_FEATURE_LEVEL_11_0:
1973 return getMaxTransformFeedbackInterleavedComponents() / getMaxTransformFeedbackBuffers();
1974 case D3D_FEATURE_LEVEL_10_1:
1975 case D3D_FEATURE_LEVEL_10_0:
1976 // D3D 10 and 10.1 only allow one output per output slot if an output slot other than zero
1977 // is used.
1978 return 4;
1979 default: UNREACHABLE();
1980 return 0;
1981 }
1982}
1983
1984unsigned int Renderer11::getMaxTransformFeedbackInterleavedComponents() const
1985{
1986 return (getMaxVaryingVectors() * 4);
1987}
1988
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00001989unsigned int Renderer11::getMaxUniformBufferSize() const
1990{
1991 // Each component is a 4-element vector of 4-byte units (floats)
1992 const unsigned int bytesPerComponent = 4 * sizeof(float);
1993
1994 switch (mFeatureLevel)
1995 {
1996 case D3D_FEATURE_LEVEL_11_0:
1997 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1998 case D3D_FEATURE_LEVEL_10_1:
1999 case D3D_FEATURE_LEVEL_10_0:
2000 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2001 default: UNREACHABLE();
2002 return 0;
2003 }
2004}
2005
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002006bool Renderer11::getShareHandleSupport() const
2007{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002008 // We only currently support share handles with BGRA surfaces, because
2009 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002010 // PIX doesn't seem to support using share handles, so disable them.
Geoff Langcec35902014-04-16 10:52:36 -04002011 return getCaps().extensions.textureFormatBGRA8888 && !gl::perfActive();
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002012}
2013
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002014bool Renderer11::getPostSubBufferSupport() const
2015{
2016 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2017 return false;
2018}
2019
Jamie Madill13a2f852013-12-11 16:35:08 -05002020int Renderer11::getMaxRecommendedElementsIndices() const
2021{
2022 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2023 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2024
2025 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2026 return std::numeric_limits<GLint>::max();
2027}
2028
2029int Renderer11::getMaxRecommendedElementsVertices() const
2030{
2031 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2032 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2033
2034 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2035 return std::numeric_limits<GLint>::max();
2036}
2037
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002038int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002039{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002040 switch (mFeatureLevel)
2041 {
2042 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002043 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002044 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2045 default: UNREACHABLE(); return 0;
2046 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002047}
2048
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002049int Renderer11::getMinorShaderModel() const
2050{
2051 switch (mFeatureLevel)
2052 {
2053 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2054 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2055 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2056 default: UNREACHABLE(); return 0;
2057 }
2058}
2059
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002060float Renderer11::getMaxPointSize() const
2061{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002062 // choose a reasonable maximum. we enforce this in the shader.
2063 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2064 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002065}
2066
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002067int Renderer11::getMaxViewportDimension() const
2068{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002069 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2070 // In our case return the maximum texture size, which is the maximum render buffer size.
2071 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2072 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2073
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002074 switch (mFeatureLevel)
2075 {
2076 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002077 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002078 case D3D_FEATURE_LEVEL_10_1:
2079 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002080 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002081 default: UNREACHABLE();
2082 return 0;
2083 }
2084}
2085
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002086int Renderer11::getMaxTextureWidth() const
2087{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002088 switch (mFeatureLevel)
2089 {
2090 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2091 case D3D_FEATURE_LEVEL_10_1:
2092 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2093 default: UNREACHABLE(); return 0;
2094 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002095}
2096
2097int Renderer11::getMaxTextureHeight() const
2098{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002099 switch (mFeatureLevel)
2100 {
2101 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2102 case D3D_FEATURE_LEVEL_10_1:
2103 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2104 default: UNREACHABLE(); return 0;
2105 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002106}
2107
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002108int Renderer11::getMaxTextureDepth() const
2109{
2110 switch (mFeatureLevel)
2111 {
2112 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2113 case D3D_FEATURE_LEVEL_10_1:
2114 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2115 default: UNREACHABLE(); return 0;
2116 }
2117}
2118
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002119int Renderer11::getMaxTextureArrayLayers() const
2120{
2121 switch (mFeatureLevel)
2122 {
2123 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2124 case D3D_FEATURE_LEVEL_10_1:
2125 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2126 default: UNREACHABLE(); return 0;
2127 }
2128}
2129
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002130int Renderer11::getMinSwapInterval() const
2131{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002132 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002133}
2134
2135int Renderer11::getMaxSwapInterval() const
2136{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002137 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002138}
2139
2140int Renderer11::getMaxSupportedSamples() const
2141{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002142 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002143}
2144
Geoff Lang005df412013-10-16 14:12:50 -04002145GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002146{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002147 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002148 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2149 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2150}
2151
Geoff Lang005df412013-10-16 14:12:50 -04002152GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002153{
2154 unsigned int numCounts = 0;
2155
2156 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002157 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2158 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002159 {
2160 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2161 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2162
2163 if (iter != mMultisampleSupportMap.end())
2164 {
2165 const MultisampleSupportInfo& info = iter->second;
2166 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2167 {
2168 if (info.qualityLevels[i] > 0)
2169 {
2170 numCounts++;
2171 }
2172 }
2173 }
2174 }
2175
2176 return numCounts;
2177}
2178
Geoff Lang005df412013-10-16 14:12:50 -04002179void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002180{
2181 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002182 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2183 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2184 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002185 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002186 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002187
2188 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2189 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2190
2191 if (iter != mMultisampleSupportMap.end())
2192 {
2193 const MultisampleSupportInfo& info = iter->second;
2194 int bufPos = 0;
2195 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2196 {
2197 if (info.qualityLevels[i] > 0)
2198 {
2199 params[bufPos++] = i + 1;
2200 }
2201 }
2202 }
2203}
2204
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002205int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2206{
2207 if (requested == 0)
2208 {
2209 return 0;
2210 }
2211
2212 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2213 if (iter != mMultisampleSupportMap.end())
2214 {
2215 const MultisampleSupportInfo& info = iter->second;
2216 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2217 {
2218 if (info.qualityLevels[i] > 0)
2219 {
2220 return i + 1;
2221 }
2222 }
2223 }
2224
2225 return -1;
2226}
2227
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002228unsigned int Renderer11::getMaxRenderTargets() const
2229{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002230 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2231 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2232
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002233 switch (mFeatureLevel)
2234 {
2235 case D3D_FEATURE_LEVEL_11_0:
2236 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2237 case D3D_FEATURE_LEVEL_10_1:
2238 case D3D_FEATURE_LEVEL_10_0:
Geoff Lang626d54e2014-02-07 14:24:12 -05002239 // Feature level 10.0 and 10.1 cards perform very poorly when the pixel shader
2240 // outputs to multiple RTs that are not bound.
2241 // TODO: Remove pixel shader outputs for render targets that are not bound.
2242 return 1;
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002243 default:
2244 UNREACHABLE();
2245 return 1;
2246 }
2247}
2248
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002249bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002250{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002251 if (source && dest)
2252 {
2253 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2254 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2255
Nicolas Capens76b258f2014-04-03 10:59:42 -04002256 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002257
2258 dest11->invalidateSwizzleCache();
2259
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002260 return true;
2261 }
2262
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002263 return false;
2264}
2265
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002266bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002267{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002268 if (source && dest)
2269 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002270 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2271 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002272
Nicolas Capens76b258f2014-04-03 10:59:42 -04002273 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002274
2275 dest11->invalidateSwizzleCache();
2276
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002277 return true;
2278 }
2279
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002280 return false;
2281}
2282
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002283bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2284{
2285 if (source && dest)
2286 {
2287 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2288 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2289
Nicolas Capens76b258f2014-04-03 10:59:42 -04002290 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002291
2292 dest11->invalidateSwizzleCache();
2293
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002294 return true;
2295 }
2296
2297 return false;
2298}
2299
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002300bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2301{
2302 if (source && dest)
2303 {
2304 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2305 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2306
Nicolas Capens76b258f2014-04-03 10:59:42 -04002307 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002308
2309 dest11->invalidateSwizzleCache();
2310
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002311 return true;
2312 }
2313
2314 return false;
2315}
2316
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002317bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002318 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002319{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002320 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002321 if (!colorbuffer)
2322 {
2323 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002324 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002325 }
2326
2327 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2328 if (!sourceRenderTarget)
2329 {
2330 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002331 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002332 }
2333
2334 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2335 if (!source)
2336 {
2337 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002338 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002339 }
2340
2341 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2342 if (!storage11)
2343 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002344 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002345 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002346 }
2347
2348 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2349 if (!destRenderTarget)
2350 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002351 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002352 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002353 }
2354
2355 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2356 if (!dest)
2357 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002358 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002359 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002360 }
2361
Geoff Langb86b9792013-06-04 16:32:05 -04002362 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2363 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002364
Geoff Langb86b9792013-06-04 16:32:05 -04002365 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2366 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002367
Geoff Langb86b9792013-06-04 16:32:05 -04002368 // Use nearest filtering because source and destination are the same size for the direct
2369 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002370 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002371 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002372
Geoff Lang42477a42013-09-17 17:07:02 -04002373 storage11->invalidateSwizzleCacheLevel(level);
2374
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002375 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002376}
2377
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002378bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002379 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002380{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002381 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002382 if (!colorbuffer)
2383 {
2384 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002385 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002386 }
2387
2388 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2389 if (!sourceRenderTarget)
2390 {
2391 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002392 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002393 }
2394
2395 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2396 if (!source)
2397 {
2398 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002399 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002400 }
2401
2402 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2403 if (!storage11)
2404 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002405 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002406 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002407 }
2408
Nicolas Capensb13f8662013-06-04 13:30:19 -04002409 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002410 if (!destRenderTarget)
2411 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002412 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002413 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002414 }
2415
2416 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2417 if (!dest)
2418 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002419 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002420 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002421 }
2422
Geoff Langb86b9792013-06-04 16:32:05 -04002423 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2424 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002425
Geoff Langb86b9792013-06-04 16:32:05 -04002426 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2427 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002428
Geoff Langb86b9792013-06-04 16:32:05 -04002429 // Use nearest filtering because source and destination are the same size for the direct
2430 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002431 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002432 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002433
Geoff Lang42477a42013-09-17 17:07:02 -04002434 storage11->invalidateSwizzleCacheLevel(level);
2435
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002436 return ret;
2437}
2438
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002439bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2440 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2441{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002442 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002443 if (!colorbuffer)
2444 {
2445 ERR("Failed to retrieve the color buffer from the frame buffer.");
2446 return gl::error(GL_OUT_OF_MEMORY, false);
2447 }
2448
2449 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2450 if (!sourceRenderTarget)
2451 {
2452 ERR("Failed to retrieve the render target from the frame buffer.");
2453 return gl::error(GL_OUT_OF_MEMORY, false);
2454 }
2455
2456 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2457 if (!source)
2458 {
2459 ERR("Failed to retrieve the render target view from the render target.");
2460 return gl::error(GL_OUT_OF_MEMORY, false);
2461 }
2462
2463 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2464 if (!storage11)
2465 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002466 ERR("Failed to retrieve the texture storage from the destination.");
2467 return gl::error(GL_OUT_OF_MEMORY, false);
2468 }
2469
2470 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2471 if (!destRenderTarget)
2472 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002473 ERR("Failed to retrieve the render target from the destination storage.");
2474 return gl::error(GL_OUT_OF_MEMORY, false);
2475 }
2476
2477 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2478 if (!dest)
2479 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002480 ERR("Failed to retrieve the render target view from the destination render target.");
2481 return gl::error(GL_OUT_OF_MEMORY, false);
2482 }
2483
Geoff Langb86b9792013-06-04 16:32:05 -04002484 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2485 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002486
Geoff Langb86b9792013-06-04 16:32:05 -04002487 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2488 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002489
Geoff Langb86b9792013-06-04 16:32:05 -04002490 // Use nearest filtering because source and destination are the same size for the direct
2491 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002492 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002493 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002494
Geoff Lang42477a42013-09-17 17:07:02 -04002495 storage11->invalidateSwizzleCacheLevel(level);
2496
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002497 return ret;
2498}
2499
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002500bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2501 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2502{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002503 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002504 if (!colorbuffer)
2505 {
2506 ERR("Failed to retrieve the color buffer from the frame buffer.");
2507 return gl::error(GL_OUT_OF_MEMORY, false);
2508 }
2509
2510 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2511 if (!sourceRenderTarget)
2512 {
2513 ERR("Failed to retrieve the render target from the frame buffer.");
2514 return gl::error(GL_OUT_OF_MEMORY, false);
2515 }
2516
2517 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2518 if (!source)
2519 {
2520 ERR("Failed to retrieve the render target view from the render target.");
2521 return gl::error(GL_OUT_OF_MEMORY, false);
2522 }
2523
2524 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2525 if (!storage11)
2526 {
Geoff Langea228632013-07-30 15:17:12 -04002527 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002528 ERR("Failed to retrieve the texture storage from the destination.");
2529 return gl::error(GL_OUT_OF_MEMORY, false);
2530 }
2531
2532 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2533 if (!destRenderTarget)
2534 {
Geoff Langea228632013-07-30 15:17:12 -04002535 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002536 ERR("Failed to retrieve the render target from the destination storage.");
2537 return gl::error(GL_OUT_OF_MEMORY, false);
2538 }
2539
2540 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2541 if (!dest)
2542 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002543 ERR("Failed to retrieve the render target view from the destination render target.");
2544 return gl::error(GL_OUT_OF_MEMORY, false);
2545 }
2546
Geoff Langb86b9792013-06-04 16:32:05 -04002547 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2548 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002549
Geoff Langb86b9792013-06-04 16:32:05 -04002550 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2551 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002552
Geoff Langb86b9792013-06-04 16:32:05 -04002553 // Use nearest filtering because source and destination are the same size for the direct
2554 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002555 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002556 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002557
Geoff Lang42477a42013-09-17 17:07:02 -04002558 storage11->invalidateSwizzleCacheLevel(level);
2559
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002560 return ret;
2561}
2562
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002563void Renderer11::unapplyRenderTargets()
2564{
2565 setOneTimeRenderTarget(NULL);
2566}
2567
2568void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2569{
2570 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2571
2572 rtvArray[0] = renderTargetView;
2573
2574 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2575
2576 // Do not preserve the serial for this one-time-use render target
2577 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2578 {
2579 mAppliedRenderTargetSerials[rtIndex] = 0;
2580 }
2581}
2582
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002583RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2584{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002585 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002586 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002587
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002588 if (depth)
2589 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002590 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002591 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002592 swapChain11->getDepthStencilTexture(),
2593 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002594 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002595 }
2596 else
2597 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002598 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002599 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002600 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002601 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002602 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002603 }
2604 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002605}
2606
Geoff Langa2d97f12013-06-11 11:44:02 -04002607RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002608{
Geoff Langa2d97f12013-06-11 11:44:02 -04002609 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002610 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002611}
2612
Geoff Lang48dcae72014-02-05 16:28:24 -05002613ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type,
2614 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2615 bool separatedOutputBuffers)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002616{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002617 ShaderExecutable11 *executable = NULL;
Geoff Lang48dcae72014-02-05 16:28:24 -05002618 HRESULT result;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002619
2620 switch (type)
2621 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002622 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002623 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002624 ID3D11VertexShader *vertexShader = NULL;
2625 ID3D11GeometryShader *streamOutShader = NULL;
2626
2627 result = mDevice->CreateVertexShader(function, length, NULL, &vertexShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002628 ASSERT(SUCCEEDED(result));
2629
Geoff Lang48dcae72014-02-05 16:28:24 -05002630 if (transformFeedbackVaryings.size() > 0)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002631 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002632 std::vector<D3D11_SO_DECLARATION_ENTRY> soDeclaration;
2633 for (size_t i = 0; i < transformFeedbackVaryings.size(); i++)
2634 {
2635 const gl::LinkedVarying &varying = transformFeedbackVaryings[i];
Geoff Langcebb5aa2014-04-07 14:13:40 -04002636 for (size_t j = 0; j < varying.semanticIndexCount; j++)
Geoff Lang48dcae72014-02-05 16:28:24 -05002637 {
2638 D3D11_SO_DECLARATION_ENTRY entry = { 0 };
2639 entry.Stream = 0;
Geoff Langcebb5aa2014-04-07 14:13:40 -04002640 entry.SemanticName = varying.semanticName.c_str();
2641 entry.SemanticIndex = varying.semanticIndex + j;
Geoff Lang48dcae72014-02-05 16:28:24 -05002642 entry.StartComponent = 0;
2643 entry.ComponentCount = gl::VariableRowCount(type);
2644 entry.OutputSlot = (separatedOutputBuffers ? i : 0);
2645 soDeclaration.push_back(entry);
2646 }
2647 }
2648
2649 result = mDevice->CreateGeometryShaderWithStreamOutput(function, length, soDeclaration.data(), soDeclaration.size(),
2650 NULL, 0, 0, NULL, &streamOutShader);
2651 ASSERT(SUCCEEDED(result));
2652 }
2653
2654 if (vertexShader)
2655 {
2656 executable = new ShaderExecutable11(function, length, vertexShader, streamOutShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002657 }
2658 }
2659 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002660 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002661 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002662 ID3D11PixelShader *pixelShader = NULL;
2663
2664 result = mDevice->CreatePixelShader(function, length, NULL, &pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002665 ASSERT(SUCCEEDED(result));
2666
Geoff Lang48dcae72014-02-05 16:28:24 -05002667 if (pixelShader)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002668 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002669 executable = new ShaderExecutable11(function, length, pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002670 }
2671 }
2672 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002673 case rx::SHADER_GEOMETRY:
2674 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002675 ID3D11GeometryShader *geometryShader = NULL;
2676
2677 result = mDevice->CreateGeometryShader(function, length, NULL, &geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002678 ASSERT(SUCCEEDED(result));
2679
Geoff Lang48dcae72014-02-05 16:28:24 -05002680 if (geometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002681 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002682 executable = new ShaderExecutable11(function, length, geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002683 }
2684 }
2685 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002686 default:
2687 UNREACHABLE();
2688 break;
2689 }
2690
2691 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002692}
2693
Geoff Lang48dcae72014-02-05 16:28:24 -05002694ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
2695 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2696 bool separatedOutputBuffers, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002697{
Geoff Lang6e05c272014-03-17 10:46:54 -07002698 const char *profileType = NULL;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002699 switch (type)
2700 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002701 case rx::SHADER_VERTEX:
Geoff Lang6e05c272014-03-17 10:46:54 -07002702 profileType = "vs";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002703 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002704 case rx::SHADER_PIXEL:
Geoff Lang6e05c272014-03-17 10:46:54 -07002705 profileType = "ps";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002706 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002707 case rx::SHADER_GEOMETRY:
Geoff Lang6e05c272014-03-17 10:46:54 -07002708 profileType = "gs";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002709 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002710 default:
2711 UNREACHABLE();
2712 return NULL;
2713 }
2714
Geoff Lang6e05c272014-03-17 10:46:54 -07002715 const char *profileVersion = NULL;
2716 switch (mFeatureLevel)
2717 {
2718 case D3D_FEATURE_LEVEL_11_0:
2719 profileVersion = "5_0";
2720 break;
2721 case D3D_FEATURE_LEVEL_10_1:
2722 profileVersion = "4_1";
2723 break;
2724 case D3D_FEATURE_LEVEL_10_0:
2725 profileVersion = "4_0";
2726 break;
2727 default:
2728 UNREACHABLE();
2729 return NULL;
2730 }
2731
2732 char profile[32];
2733 snprintf(profile, ArraySize(profile), "%s_%s", profileType, profileVersion);
2734
Nicolas Capens93faad92014-05-10 12:14:13 -04002735 UINT flags = D3DCOMPILE_OPTIMIZATION_LEVEL0;
2736
2737 if (gl::perfActive())
2738 {
2739#ifndef NDEBUG
2740 flags = D3DCOMPILE_SKIP_OPTIMIZATION;
2741#endif
2742
2743 flags |= D3DCOMPILE_DEBUG;
2744
2745 std::string sourcePath = getTempPath();
2746 std::string sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(shaderHLSL);
2747 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
2748 }
2749
2750 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
2751 // Try the default flags first and if compilation fails, try some alternatives.
2752 const UINT extraFlags[] =
2753 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002754 flags,
Nicolas Capens2c27db62014-05-10 12:21:11 -04002755 flags | D3DCOMPILE_SKIP_VALIDATION,
2756 flags | D3DCOMPILE_SKIP_OPTIMIZATION
Nicolas Capens93faad92014-05-10 12:14:13 -04002757 };
2758
2759 const static char *extraFlagNames[] =
2760 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002761 "default",
Nicolas Capens2c27db62014-05-10 12:21:11 -04002762 "skip validation",
2763 "skip optimization"
Nicolas Capens93faad92014-05-10 12:14:13 -04002764 };
2765
2766 int attempts = ArraySize(extraFlags);
2767
2768 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, extraFlags, extraFlagNames, attempts);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002769 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002770 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002771 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002772 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002773
Geoff Lang48dcae72014-02-05 16:28:24 -05002774 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type,
2775 transformFeedbackVaryings, separatedOutputBuffers);
Geoff Langea228632013-07-30 15:17:12 -04002776 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002777
2778 return executable;
2779}
2780
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002781rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2782{
2783 return new UniformStorage11(this, storageSize);
2784}
2785
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002786VertexBuffer *Renderer11::createVertexBuffer()
2787{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002788 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002789}
2790
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002791IndexBuffer *Renderer11::createIndexBuffer()
2792{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002793 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002794}
2795
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002796BufferStorage *Renderer11::createBufferStorage()
2797{
2798 return new BufferStorage11(this);
2799}
2800
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002801QueryImpl *Renderer11::createQuery(GLenum type)
2802{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002803 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002804}
2805
2806FenceImpl *Renderer11::createFence()
2807{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002808 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002809}
2810
Geoff Lang005df412013-10-16 14:12:50 -04002811bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002812{
Jamie Madill4461f092013-10-10 15:10:39 -04002813 int clientVersion = getCurrentClientVersion();
2814
2815 // We only support buffer to texture copies in ES3
2816 if (clientVersion <= 2)
2817 {
2818 return false;
2819 }
2820
2821 // sRGB formats do not work with D3D11 buffer SRVs
2822 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2823 {
2824 return false;
2825 }
2826
2827 // We cannot support direct copies to non-color-renderable formats
Geoff Langcec35902014-04-16 10:52:36 -04002828 if (!getCaps().textureCaps.get(internalFormat).colorRendering)
Jamie Madill4461f092013-10-10 15:10:39 -04002829 {
2830 return false;
2831 }
2832
2833 // We skip all 3-channel formats since sometimes format support is missing
2834 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2835 {
2836 return false;
2837 }
2838
2839 // We don't support formats which we can't represent without conversion
2840 if (getNativeTextureFormat(internalFormat) != internalFormat)
2841 {
2842 return false;
2843 }
2844
2845 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002846}
2847
Jamie Madilla21eea32013-09-18 14:36:25 -04002848bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2849 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2850{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002851 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002852 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2853}
2854
Jamie Madill3c7fa222014-06-05 13:08:51 -04002855bool Renderer11::getRenderTargetResource(gl::FramebufferAttachment *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002856{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002857 ASSERT(colorbuffer != NULL);
2858
2859 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2860 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002861 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002862 *subresourceIndex = renderTarget->getSubresourceIndex();
2863
2864 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2865 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002866 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002867 ID3D11Resource *textureResource = NULL;
2868 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002869
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002870 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002871 {
Geoff Lang8e328842014-02-10 13:11:20 -05002872 HRESULT result = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002873 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002874
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002875 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002876 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002877 return true;
2878 }
2879 else
2880 {
2881 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2882 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002883 }
2884 }
2885 }
2886 }
2887
2888 return false;
2889}
2890
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002891bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002892 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002893{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002894 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002895 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002896 gl::FramebufferAttachment *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002897
2898 if (!readBuffer)
2899 {
2900 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2901 return gl::error(GL_OUT_OF_MEMORY, false);
2902 }
2903
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002904 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002905
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002906 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002907 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002908 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2909 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002910 gl::FramebufferAttachment *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002911
2912 if (!drawBuffer)
2913 {
2914 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2915 return gl::error(GL_OUT_OF_MEMORY, false);
2916 }
2917
2918 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2919
Geoff Lang125deab2013-08-09 13:34:16 -04002920 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002921 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002922 {
2923 return false;
2924 }
2925 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002926 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002927 }
2928
Geoff Lang685806d2013-06-12 11:16:36 -04002929 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002930 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002931 gl::FramebufferAttachment *readBuffer = readTarget->getDepthOrStencilbuffer();
2932 gl::FramebufferAttachment *drawBuffer = drawTarget->getDepthOrStencilbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002933
2934 if (!readBuffer)
2935 {
2936 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2937 return gl::error(GL_OUT_OF_MEMORY, false);
2938 }
2939
2940 if (!drawBuffer)
2941 {
2942 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2943 return gl::error(GL_OUT_OF_MEMORY, false);
2944 }
2945
2946 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2947 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2948
Geoff Lang125deab2013-08-09 13:34:16 -04002949 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002950 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002951 {
2952 return false;
2953 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002954 }
2955
Geoff Lang42477a42013-09-17 17:07:02 -04002956 invalidateFramebufferSwizzles(drawTarget);
2957
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002958 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002959}
2960
Jamie Madilleb9baab2014-03-24 13:19:43 -04002961void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
2962 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002963{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002964 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002965 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002966
Jamie Madill3c7fa222014-06-05 13:08:51 -04002967 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002968
2969 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002970 {
2971 gl::Rectangle area;
2972 area.x = x;
2973 area.y = y;
2974 area.width = width;
2975 area.height = height;
2976
Jamie Madill1ef6fe72014-05-01 14:51:05 -04002977 if (pack.pixelBuffer.get() != NULL)
2978 {
2979 rx::BufferStorage11 *packBufferStorage = BufferStorage11::makeBufferStorage11(pack.pixelBuffer.get()->getStorage());
2980 PackPixelsParams packParams(area, format, type, outputPitch, pack, reinterpret_cast<ptrdiff_t>(pixels));
2981 packBufferStorage->packPixels(colorBufferTexture, subresourceIndex, packParams);
2982 }
2983 else
2984 {
2985 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch, pack, pixels);
2986 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002987
Geoff Langea228632013-07-30 15:17:12 -04002988 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002989 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002990}
2991
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002992Image *Renderer11::createImage()
2993{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002994 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002995}
2996
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002997void Renderer11::generateMipmap(Image *dest, Image *src)
2998{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002999 Image11 *dest11 = Image11::makeImage11(dest);
3000 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003001 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003002}
3003
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003004TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3005{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003006 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3007 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003008}
3009
Nicolas Capensbf712d02014-03-31 14:23:35 -04003010TextureStorage *Renderer11::createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003011{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003012 return new TextureStorage11_2D(this, internalformat, renderTarget, width, height, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003013}
3014
Nicolas Capensbf712d02014-03-31 14:23:35 -04003015TextureStorage *Renderer11::createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003016{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003017 return new TextureStorage11_Cube(this, internalformat, renderTarget, size, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003018}
3019
Nicolas Capensbf712d02014-03-31 14:23:35 -04003020TextureStorage *Renderer11::createTextureStorage3D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels)
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003021{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003022 return new TextureStorage11_3D(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003023}
3024
Nicolas Capensbf712d02014-03-31 14:23:35 -04003025TextureStorage *Renderer11::createTextureStorage2DArray(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels)
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003026{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003027 return new TextureStorage11_2DArray(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003028}
3029
Jamie Madilleb9baab2014-03-24 13:19:43 -04003030void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format,
3031 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void *pixels)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003032{
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003033 ASSERT(area.width >= 0);
3034 ASSERT(area.height >= 0);
3035
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003036 D3D11_TEXTURE2D_DESC textureDesc;
3037 texture->GetDesc(&textureDesc);
3038
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003039 // Clamp read region to the defined texture boundaries, preventing out of bounds reads
3040 // and reads of uninitialized data.
3041 gl::Rectangle safeArea;
3042 safeArea.x = gl::clamp(area.x, 0, static_cast<int>(textureDesc.Width));
3043 safeArea.y = gl::clamp(area.y, 0, static_cast<int>(textureDesc.Height));
3044 safeArea.width = gl::clamp(area.width + std::min(area.x, 0), 0,
3045 static_cast<int>(textureDesc.Width) - safeArea.x);
3046 safeArea.height = gl::clamp(area.height + std::min(area.y, 0), 0,
3047 static_cast<int>(textureDesc.Height) - safeArea.y);
3048
3049 ASSERT(safeArea.x >= 0 && safeArea.y >= 0);
3050 ASSERT(safeArea.x + safeArea.width <= static_cast<int>(textureDesc.Width));
3051 ASSERT(safeArea.y + safeArea.height <= static_cast<int>(textureDesc.Height));
3052
3053 if (safeArea.width == 0 || safeArea.height == 0)
3054 {
3055 // no work to do
3056 return;
3057 }
3058
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003059 D3D11_TEXTURE2D_DESC stagingDesc;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003060 stagingDesc.Width = safeArea.width;
3061 stagingDesc.Height = safeArea.height;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003062 stagingDesc.MipLevels = 1;
3063 stagingDesc.ArraySize = 1;
3064 stagingDesc.Format = textureDesc.Format;
3065 stagingDesc.SampleDesc.Count = 1;
3066 stagingDesc.SampleDesc.Quality = 0;
3067 stagingDesc.Usage = D3D11_USAGE_STAGING;
3068 stagingDesc.BindFlags = 0;
3069 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3070 stagingDesc.MiscFlags = 0;
3071
3072 ID3D11Texture2D* stagingTex = NULL;
3073 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3074 if (FAILED(result))
3075 {
3076 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3077 return;
3078 }
3079
3080 ID3D11Texture2D* srcTex = NULL;
3081 if (textureDesc.SampleDesc.Count > 1)
3082 {
3083 D3D11_TEXTURE2D_DESC resolveDesc;
3084 resolveDesc.Width = textureDesc.Width;
3085 resolveDesc.Height = textureDesc.Height;
3086 resolveDesc.MipLevels = 1;
3087 resolveDesc.ArraySize = 1;
3088 resolveDesc.Format = textureDesc.Format;
3089 resolveDesc.SampleDesc.Count = 1;
3090 resolveDesc.SampleDesc.Quality = 0;
3091 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3092 resolveDesc.BindFlags = 0;
3093 resolveDesc.CPUAccessFlags = 0;
3094 resolveDesc.MiscFlags = 0;
3095
3096 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3097 if (FAILED(result))
3098 {
3099 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003100 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003101 return;
3102 }
3103
3104 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3105 subResource = 0;
3106 }
3107 else
3108 {
3109 srcTex = texture;
3110 srcTex->AddRef();
3111 }
3112
3113 D3D11_BOX srcBox;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003114 srcBox.left = static_cast<UINT>(safeArea.x);
3115 srcBox.right = static_cast<UINT>(safeArea.x + safeArea.width);
3116 srcBox.top = static_cast<UINT>(safeArea.y);
3117 srcBox.bottom = static_cast<UINT>(safeArea.y + safeArea.height);
3118 srcBox.front = 0;
3119 srcBox.back = 1;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003120
3121 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3122
Geoff Langea228632013-07-30 15:17:12 -04003123 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003124
Jamie Madill7538f7f2014-04-17 11:53:39 -04003125 PackPixelsParams packParams(safeArea, format, type, outputPitch, pack, 0);
3126 packPixels(stagingTex, packParams, pixels);
3127
3128 SafeRelease(stagingTex);
3129}
3130
3131void Renderer11::packPixels(ID3D11Texture2D *readTexture, const PackPixelsParams &params, void *pixelsOut)
3132{
3133 D3D11_TEXTURE2D_DESC textureDesc;
3134 readTexture->GetDesc(&textureDesc);
3135
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003136 D3D11_MAPPED_SUBRESOURCE mapping;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003137 HRESULT hr = mDeviceContext->Map(readTexture, 0, D3D11_MAP_READ, 0, &mapping);
Geoff Lang9cd19152014-05-28 15:54:34 -04003138 UNUSED_ASSERTION_VARIABLE(hr);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003139 ASSERT(SUCCEEDED(hr));
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003140
3141 unsigned char *source;
3142 int inputPitch;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003143 if (params.pack.reverseRowOrder)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003144 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003145 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (params.area.height - 1);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003146 inputPitch = -static_cast<int>(mapping.RowPitch);
3147 }
3148 else
3149 {
3150 source = static_cast<unsigned char*>(mapping.pData);
3151 inputPitch = static_cast<int>(mapping.RowPitch);
3152 }
3153
Geoff Lang697ad3e2013-06-04 10:11:28 -04003154 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003155
Geoff Lang005df412013-10-16 14:12:50 -04003156 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003157 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3158 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3159
3160 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3161
Jamie Madill7538f7f2014-04-17 11:53:39 -04003162 if (sourceFormat == params.format && sourceType == params.type)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003163 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003164 unsigned char *dest = static_cast<unsigned char*>(pixelsOut) + params.offset;
3165 for (int y = 0; y < params.area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003166 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003167 memcpy(dest + y * params.outputPitch, source + y * inputPitch, params.area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003168 }
3169 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003170 else
3171 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003172 GLenum destInternalFormat = gl::GetSizedInternalFormat(params.format, params.type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003173 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3174
Jamie Madill7538f7f2014-04-17 11:53:39 -04003175 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, params.format, params.type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003176 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003177 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003178 // Fast copy is possible through some special function
Jamie Madill7538f7f2014-04-17 11:53:39 -04003179 for (int y = 0; y < params.area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003180 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003181 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003182 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003183 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003184 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3185
3186 fastCopyFunc(src, dest);
3187 }
3188 }
3189 }
3190 else
3191 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003192 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003193 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(params.format, params.type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003194
3195 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3196 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3197 sizeof(temp) >= sizeof(gl::ColorUI) &&
3198 sizeof(temp) >= sizeof(gl::ColorI));
3199
Jamie Madill7538f7f2014-04-17 11:53:39 -04003200 for (int y = 0; y < params.area.height; y++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003201 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003202 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003203 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003204 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003205 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3206
3207 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3208 // will not allow the copy otherwise.
3209 readFunc(src, temp);
3210 writeFunc(temp, dest);
3211 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003212 }
3213 }
3214 }
3215
Jamie Madill7538f7f2014-04-17 11:53:39 -04003216 mDeviceContext->Unmap(readTexture, 0);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003217}
3218
Geoff Lang758d5b22013-06-11 11:42:50 -04003219bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003220 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3221 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003222{
Geoff Lang975af372013-06-12 11:19:22 -04003223 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3224 // it should never be the case that both color and depth/stencil need to be blitted at
3225 // at the same time.
3226 ASSERT(colorBlit != (depthBlit || stencilBlit));
3227
Geoff Langc1f51be2013-06-11 11:49:14 -04003228 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003229
Geoff Lang4d782732013-07-22 10:44:18 -04003230 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3231 if (!drawRenderTarget)
3232 {
3233 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3234 return gl::error(GL_OUT_OF_MEMORY, false);
3235 }
3236
3237 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3238 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3239 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3240 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3241
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003242 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3243 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003244 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003245 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003246 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003247 }
3248
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003249 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003250 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003251 unsigned int readSubresource = 0;
3252 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003253 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003254 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3255 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003256
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003257 if (unresolvedTexture)
3258 {
3259 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3260 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003261
Geoff Langea228632013-07-30 15:17:12 -04003262 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003263
Geoff Lang5c9a29a2014-02-11 10:26:24 -05003264 HRESULT hresult = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3265 if (FAILED(hresult))
Geoff Langc1f51be2013-06-11 11:49:14 -04003266 {
Geoff Langea228632013-07-30 15:17:12 -04003267 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003268 return gl::error(GL_OUT_OF_MEMORY, false);
3269 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003270 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003271 }
3272 else
3273 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003274 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003275 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003276 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003277 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003278 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003279 }
3280
Geoff Lang4d782732013-07-22 10:44:18 -04003281 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003282 {
Geoff Lang4d782732013-07-22 10:44:18 -04003283 SafeRelease(readTexture);
3284 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003285 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003286 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003287 }
3288
Geoff Lang125deab2013-08-09 13:34:16 -04003289 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3290 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3291
3292 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3293
3294 bool wholeBufferCopy = !scissorNeeded &&
3295 readRect.x == 0 && readRect.width == readSize.width &&
3296 readRect.y == 0 && readRect.height == readSize.height &&
3297 drawRect.x == 0 && drawRect.width == drawSize.width &&
3298 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003299
Geoff Langc1f51be2013-06-11 11:49:14 -04003300 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003301
Geoff Lang1cd1b212014-02-11 09:42:27 -05003302 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003303
3304 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3305 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3306 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3307 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3308
3309 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3310 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3311 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3312
Geoff Langc1f51be2013-06-11 11:49:14 -04003313 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003314 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3315 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003316 {
Geoff Lang125deab2013-08-09 13:34:16 -04003317 UINT dstX = drawRect.x;
3318 UINT dstY = drawRect.y;
3319
Geoff Langc1f51be2013-06-11 11:49:14 -04003320 D3D11_BOX readBox;
3321 readBox.left = readRect.x;
3322 readBox.right = readRect.x + readRect.width;
3323 readBox.top = readRect.y;
3324 readBox.bottom = readRect.y + readRect.height;
3325 readBox.front = 0;
3326 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003327
Geoff Lang125deab2013-08-09 13:34:16 -04003328 if (scissorNeeded)
3329 {
3330 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3331 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3332
3333 if (drawRect.x < scissor->x)
3334 {
3335 dstX = scissor->x;
3336 readBox.left += (scissor->x - drawRect.x);
3337 }
3338 if (drawRect.y < scissor->y)
3339 {
3340 dstY = scissor->y;
3341 readBox.top += (scissor->y - drawRect.y);
3342 }
3343 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3344 {
3345 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3346 }
3347 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3348 {
3349 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3350 }
3351 }
3352
Geoff Langc1f51be2013-06-11 11:49:14 -04003353 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3354 // We also require complete framebuffer copies for depth-stencil blit.
3355 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003356
Geoff Lang125deab2013-08-09 13:34:16 -04003357 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003358 readTexture, readSubresource, pSrcBox);
3359 result = true;
3360 }
3361 else
3362 {
3363 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003364 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003365
Geoff Lang975af372013-06-12 11:19:22 -04003366 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003367 {
Geoff Lang975af372013-06-12 11:19:22 -04003368 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003369 drawTexture, drawSubresource, drawArea, drawSize,
3370 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003371 }
3372 else if (depthBlit)
3373 {
Geoff Lang125deab2013-08-09 13:34:16 -04003374 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3375 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003376 }
3377 else if (stencilBlit)
3378 {
3379 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003380 drawTexture, drawSubresource, drawArea, drawSize,
3381 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003382 }
3383 else
3384 {
Geoff Lang685806d2013-06-12 11:16:36 -04003385 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003386 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3387 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003388 }
3389 }
3390
3391 SafeRelease(readTexture);
3392 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003393
3394 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003395}
3396
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003397ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3398{
3399 D3D11_TEXTURE2D_DESC textureDesc;
3400 source->GetDesc(&textureDesc);
3401
3402 if (textureDesc.SampleDesc.Count > 1)
3403 {
3404 D3D11_TEXTURE2D_DESC resolveDesc;
3405 resolveDesc.Width = textureDesc.Width;
3406 resolveDesc.Height = textureDesc.Height;
3407 resolveDesc.MipLevels = 1;
3408 resolveDesc.ArraySize = 1;
3409 resolveDesc.Format = textureDesc.Format;
3410 resolveDesc.SampleDesc.Count = 1;
3411 resolveDesc.SampleDesc.Quality = 0;
3412 resolveDesc.Usage = textureDesc.Usage;
3413 resolveDesc.BindFlags = textureDesc.BindFlags;
3414 resolveDesc.CPUAccessFlags = 0;
3415 resolveDesc.MiscFlags = 0;
3416
3417 ID3D11Texture2D *resolveTexture = NULL;
3418 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3419 if (FAILED(result))
3420 {
3421 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3422 return NULL;
3423 }
3424
3425 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3426 return resolveTexture;
3427 }
3428 else
3429 {
3430 source->AddRef();
3431 return source;
3432 }
3433}
3434
Jamie Madill3c7fa222014-06-05 13:08:51 -04003435void Renderer11::invalidateFBOAttachmentSwizzles(gl::FramebufferAttachment *attachment, int mipLevel)
Geoff Lang42477a42013-09-17 17:07:02 -04003436{
Jamie Madill3c7fa222014-06-05 13:08:51 -04003437 ASSERT(attachment->isTexture());
3438 TextureStorage *texStorage = attachment->getTextureStorage();
Geoff Lang42477a42013-09-17 17:07:02 -04003439 if (texStorage)
3440 {
3441 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3442 if (!texStorage11)
3443 {
3444 ERR("texture storage pointer unexpectedly null.");
3445 return;
3446 }
3447
3448 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3449 }
3450}
3451
3452void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3453{
3454 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3455 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003456 gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(colorAttachment);
3457 if (attachment && attachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003458 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003459 invalidateFBOAttachmentSwizzles(attachment, framebuffer->getColorbufferMipLevel(colorAttachment));
Geoff Lang42477a42013-09-17 17:07:02 -04003460 }
3461 }
3462
Jamie Madill3c7fa222014-06-05 13:08:51 -04003463 gl::FramebufferAttachment *depthAttachment = framebuffer->getDepthbuffer();
3464 if (depthAttachment && depthAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003465 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003466 invalidateFBOAttachmentSwizzles(depthAttachment, framebuffer->getDepthbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003467 }
3468
Jamie Madill3c7fa222014-06-05 13:08:51 -04003469 gl::FramebufferAttachment *stencilAttachment = framebuffer->getStencilbuffer();
3470 if (stencilAttachment && stencilAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003471 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003472 invalidateFBOAttachmentSwizzles(stencilAttachment, framebuffer->getStencilbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003473 }
3474}
3475
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003476bool Renderer11::getLUID(LUID *adapterLuid) const
3477{
3478 adapterLuid->HighPart = 0;
3479 adapterLuid->LowPart = 0;
3480
3481 if (!mDxgiAdapter)
3482 {
3483 return false;
3484 }
3485
3486 DXGI_ADAPTER_DESC adapterDesc;
3487 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3488 {
3489 return false;
3490 }
3491
3492 *adapterLuid = adapterDesc.AdapterLuid;
3493 return true;
3494}
3495
Geoff Lang005df412013-10-16 14:12:50 -04003496GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003497{
3498 int clientVersion = getCurrentClientVersion();
3499 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3500}
3501
Jamie Madill95ffb862014-01-29 09:26:59 -05003502rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3503{
3504 return gl_d3d11::GetVertexConversionType(vertexFormat);
3505}
3506
3507GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3508{
3509 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3510}
3511
Geoff Lang61e49a52013-05-29 10:22:58 -04003512Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3513{
3514 MultisampleSupportInfo supportInfo = { 0 };
3515
3516 UINT formatSupport;
3517 HRESULT result;
3518
3519 result = mDevice->CheckFormatSupport(format, &formatSupport);
3520 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3521 {
3522 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3523 {
3524 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3525 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3526 {
3527 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3528 }
3529 else
3530 {
3531 supportInfo.qualityLevels[i - 1] = 0;
3532 }
3533 }
3534 }
3535
3536 return supportInfo;
3537}
3538
Geoff Langcec35902014-04-16 10:52:36 -04003539gl::Caps Renderer11::generateCaps() const
3540{
3541 return d3d11_gl::GenerateCaps(mDevice);
3542}
3543
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003544}