blob: 05d6fd5a581f418fb9f9b0089a0f7fba0b8e34f0 [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"
Brandon Jones5bf98292014-06-06 17:19:38 -070034#include "libGLESv2/renderer/d3d11/VertexArray11.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000035#include "libEGL/Display.h"
36
Geoff Lang94a90892014-02-18 17:14:19 -050037// Enable ANGLE_SKIP_DXGI_1_2_CHECK if there is not a possibility of using cross-process
38// HWNDs or the Windows 7 Platform Update (KB2670838) is expected to be installed.
39#ifndef ANGLE_SKIP_DXGI_1_2_CHECK
40#define ANGLE_SKIP_DXGI_1_2_CHECK 0
41#endif
42
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000043#ifdef _DEBUG
44// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
45// and conformance tests. to enable all warnings, remove this define.
46#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
47#endif
48
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000049namespace rx
50{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000051static const DXGI_FORMAT RenderTargetFormats[] =
52 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000053 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000054 DXGI_FORMAT_R8G8B8A8_UNORM
55 };
56
57static const DXGI_FORMAT DepthStencilFormats[] =
58 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000059 DXGI_FORMAT_UNKNOWN,
60 DXGI_FORMAT_D24_UNORM_S8_UINT,
61 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000062 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000063
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000064enum
65{
66 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
67};
68
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000069Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
70{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000071 mVertexDataManager = NULL;
72 mIndexDataManager = NULL;
73
daniel@transgaming.comc5114302012-12-20 21:11:36 +000074 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000075 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000076
Geoff Langb86b9792013-06-04 16:32:05 -040077 mBlit = NULL;
Jamie Madilla21eea32013-09-18 14:36:25 -040078 mPixelTransfer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000079
Geoff Langda507fe2013-08-20 12:01:42 -040080 mClear = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000081
daniel@transgaming.combdf787f2013-02-01 03:20:36 +000082 mSyncQuery = NULL;
83
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000084 mD3d11Module = NULL;
85 mDxgiModule = NULL;
86
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000087 mDeviceLost = false;
88
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +000089 mMaxSupportedSamples = 0;
90
daniel@transgaming.com25072f62012-11-28 19:31:32 +000091 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000092 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000093 mDxgiAdapter = NULL;
94 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +000095
96 mDriverConstantBufferVS = NULL;
97 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +000098
Jamie Madill6246dc82014-01-29 09:26:47 -050099 mAppliedVertexShader = NULL;
100 mAppliedGeometryShader = NULL;
Geoff Lang4c5c6bb2014-02-05 16:32:46 -0500101 mCurPointGeometryShader = NULL;
Jamie Madill6246dc82014-01-29 09:26:47 -0500102 mAppliedPixelShader = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000103}
104
105Renderer11::~Renderer11()
106{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000107 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000108}
109
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000110Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
111{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000112 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000113 return static_cast<rx::Renderer11*>(renderer);
114}
115
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000116#ifndef __d3d11_1_h__
117#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
118#endif
119
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000120EGLint Renderer11::initialize()
121{
Geoff Langdad5ed32014-02-10 12:59:17 -0500122 if (!mCompiler.initialize())
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000123 {
124 return EGL_NOT_INITIALIZED;
125 }
126
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000127 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
128 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000129
130 if (mD3d11Module == NULL || mDxgiModule == NULL)
131 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000132 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000133 return EGL_NOT_INITIALIZED;
134 }
135
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000136 // create the D3D11 device
137 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000138 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000139
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000140 if (D3D11CreateDevice == NULL)
141 {
142 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
143 return EGL_NOT_INITIALIZED;
144 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000145
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000146 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000147 {
148 D3D_FEATURE_LEVEL_11_0,
149 D3D_FEATURE_LEVEL_10_1,
150 D3D_FEATURE_LEVEL_10_0,
151 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000152
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000153 HRESULT result = S_OK;
154
155#ifdef _DEBUG
156 result = D3D11CreateDevice(NULL,
157 D3D_DRIVER_TYPE_HARDWARE,
158 NULL,
159 D3D11_CREATE_DEVICE_DEBUG,
160 featureLevels,
161 ArraySize(featureLevels),
162 D3D11_SDK_VERSION,
163 &mDevice,
164 &mFeatureLevel,
165 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000166
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000167 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000168 {
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000169 ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n");
170 }
171
172 if (!mDevice || FAILED(result))
173#endif
174 {
175 result = D3D11CreateDevice(NULL,
176 D3D_DRIVER_TYPE_HARDWARE,
177 NULL,
178 0,
179 featureLevels,
180 ArraySize(featureLevels),
181 D3D11_SDK_VERSION,
182 &mDevice,
183 &mFeatureLevel,
184 &mDeviceContext);
185
186 if (!mDevice || FAILED(result))
187 {
188 ERR("Could not create D3D11 device - aborting!\n");
189 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
190 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000191 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000192
Geoff Lang94a90892014-02-18 17:14:19 -0500193#if !ANGLE_SKIP_DXGI_1_2_CHECK
194 // In order to create a swap chain for an HWND owned by another process, DXGI 1.2 is required.
195 // The easiest way to check is to query for a IDXGIDevice2.
196 bool requireDXGI1_2 = false;
197 HWND hwnd = WindowFromDC(mDc);
198 if (hwnd)
199 {
200 DWORD currentProcessId = GetCurrentProcessId();
201 DWORD wndProcessId;
202 GetWindowThreadProcessId(hwnd, &wndProcessId);
203 requireDXGI1_2 = (currentProcessId != wndProcessId);
204 }
205 else
206 {
207 requireDXGI1_2 = true;
208 }
209
210 if (requireDXGI1_2)
211 {
212 IDXGIDevice2 *dxgiDevice2 = NULL;
213 result = mDevice->QueryInterface(__uuidof(IDXGIDevice2), (void**)&dxgiDevice2);
214 if (FAILED(result))
215 {
216 ERR("DXGI 1.2 required to present to HWNDs owned by another process.\n");
217 return EGL_NOT_INITIALIZED;
218 }
219 SafeRelease(dxgiDevice2);
220 }
221#endif
222
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000223 IDXGIDevice *dxgiDevice = NULL;
224 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
225
226 if (FAILED(result))
227 {
228 ERR("Could not query DXGI device - aborting!\n");
229 return EGL_NOT_INITIALIZED;
230 }
231
232 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
233
234 if (FAILED(result))
235 {
236 ERR("Could not retrieve DXGI adapter - aborting!\n");
237 return EGL_NOT_INITIALIZED;
238 }
239
Geoff Langea228632013-07-30 15:17:12 -0400240 SafeRelease(dxgiDevice);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000241
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000242 mDxgiAdapter->GetDesc(&mAdapterDescription);
243 memset(mDescription, 0, sizeof(mDescription));
244 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
245
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000246 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
247
248 if (!mDxgiFactory || FAILED(result))
249 {
250 ERR("Could not create DXGI factory - aborting!\n");
251 return EGL_NOT_INITIALIZED;
252 }
253
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000254 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
255#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
256 ID3D11InfoQueue *infoQueue;
257 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
258
259 if (SUCCEEDED(result))
260 {
261 D3D11_MESSAGE_ID hideMessages[] =
262 {
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000263 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000264 };
265
266 D3D11_INFO_QUEUE_FILTER filter = {0};
267 filter.DenyList.NumIDs = ArraySize(hideMessages);
268 filter.DenyList.pIDList = hideMessages;
269
270 infoQueue->AddStorageFilterEntries(&filter);
Geoff Langea228632013-07-30 15:17:12 -0400271 SafeRelease(infoQueue);
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000272 }
273#endif
274
Geoff Lang61e49a52013-05-29 10:22:58 -0400275 mMaxSupportedSamples = 0;
276
277 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
278 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000279 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400280 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
281 mMultisampleSupportMap.insert(std::make_pair(*i, support));
282 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000283 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000284
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000285 initializeDevice();
286
287 return EGL_SUCCESS;
288}
289
290// do any one-time device initialization
291// NOTE: this is also needed after a device lost/reset
292// to reset the scene status and ensure the default states are reset.
293void Renderer11::initializeDevice()
294{
Jamie Madill1e3fa742014-06-16 10:34:00 -0400295 mStateCache.initialize(this);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000296 mInputLayoutCache.initialize(mDevice, mDeviceContext);
297
298 ASSERT(!mVertexDataManager && !mIndexDataManager);
299 mVertexDataManager = new VertexDataManager(this);
300 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000301
Geoff Langb86b9792013-06-04 16:32:05 -0400302 ASSERT(!mBlit);
303 mBlit = new Blit11(this);
304
Geoff Langda507fe2013-08-20 12:01:42 -0400305 ASSERT(!mClear);
306 mClear = new Clear11(this);
307
Jamie Madilla21eea32013-09-18 14:36:25 -0400308 ASSERT(!mPixelTransfer);
309 mPixelTransfer = new PixelTransfer11(this);
310
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000311 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000312}
313
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000314int Renderer11::generateConfigs(ConfigDesc **configDescList)
315{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000316 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
317 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000318 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
319 int numConfigs = 0;
320
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000321 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000322 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000323 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000324 {
325 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
326
327 UINT formatSupport = 0;
328 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000329
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000330 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
331 {
332 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
333
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000334 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000335
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000336 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
337 {
Geoff Lang5c9a29a2014-02-11 10:26:24 -0500338 UINT depthStencilSupport = 0;
339 result = mDevice->CheckFormatSupport(depthStencilFormat, &depthStencilSupport);
340 depthStencilFormatOK = SUCCEEDED(result) && (depthStencilSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000341 }
342
343 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000344 {
Jamie Madilld6cb2442013-07-10 15:13:38 -0400345 // FIXME: parse types from context version
346 ASSERT(d3d11_gl::GetInternalFormat(renderTargetFormat, 2) == d3d11_gl::GetInternalFormat(renderTargetFormat, 3));
347 ASSERT(d3d11_gl::GetInternalFormat(depthStencilFormat, 2) == d3d11_gl::GetInternalFormat(depthStencilFormat, 3));
348
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000349 ConfigDesc newConfig;
Jamie Madilld6cb2442013-07-10 15:13:38 -0400350 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat, getCurrentClientVersion());
351 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat, getCurrentClientVersion());
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000352 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
353 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000354 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000355
356 (*configDescList)[numConfigs++] = newConfig;
357 }
358 }
359 }
360 }
361
362 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000363}
364
365void Renderer11::deleteConfigs(ConfigDesc *configDescList)
366{
367 delete [] (configDescList);
368}
369
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000370void Renderer11::sync(bool block)
371{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000372 if (block)
373 {
374 HRESULT result;
375
376 if (!mSyncQuery)
377 {
378 D3D11_QUERY_DESC queryDesc;
379 queryDesc.Query = D3D11_QUERY_EVENT;
380 queryDesc.MiscFlags = 0;
381
382 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
383 ASSERT(SUCCEEDED(result));
384 }
385
386 mDeviceContext->End(mSyncQuery);
387 mDeviceContext->Flush();
388
389 do
390 {
391 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
392
393 // Keep polling, but allow other threads to do something useful first
394 Sleep(0);
395
396 if (testDeviceLost(true))
397 {
398 return;
399 }
400 }
401 while (result == S_FALSE);
402 }
403 else
404 {
405 mDeviceContext->Flush();
406 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000407}
408
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000409SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
410{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000411 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000412}
413
Geoff Lange2e0ce02013-09-17 17:05:08 -0400414void Renderer11::generateSwizzle(gl::Texture *texture)
415{
Geoff Lang42477a42013-09-17 17:07:02 -0400416 if (texture)
417 {
418 TextureStorageInterface *texStorage = texture->getNativeTexture();
419 if (texStorage)
420 {
421 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
422
423 storage11->generateSwizzles(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
424 texture->getSwizzleAlpha());
425 }
426 }
Geoff Lange2e0ce02013-09-17 17:05:08 -0400427}
428
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000429void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
430{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000431 if (type == gl::SAMPLER_PIXEL)
432 {
433 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
434 {
435 ERR("Pixel shader sampler index %i is not valid.", index);
436 return;
437 }
438
439 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
440 {
441 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
442
443 if (!dxSamplerState)
444 {
445 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
446 "sampler state for pixel shaders at slot %i.", index);
447 }
448
449 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
450
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000451 mCurPixelSamplerStates[index] = samplerState;
452 }
453
454 mForceSetPixelSamplerStates[index] = false;
455 }
456 else if (type == gl::SAMPLER_VERTEX)
457 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000458 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000459 {
460 ERR("Vertex shader sampler index %i is not valid.", index);
461 return;
462 }
463
464 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
465 {
466 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
467
468 if (!dxSamplerState)
469 {
470 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
471 "sampler state for vertex shaders at slot %i.", index);
472 }
473
474 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
475
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000476 mCurVertexSamplerStates[index] = samplerState;
477 }
478
479 mForceSetVertexSamplerStates[index] = false;
480 }
481 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000482}
483
484void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
485{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000486 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000487 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000488
489 if (texture)
490 {
491 TextureStorageInterface *texStorage = texture->getNativeTexture();
492 if (texStorage)
493 {
494 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
Nicolas Capensa10c1c72014-03-27 11:20:52 -0400495 gl::SamplerState samplerState;
496 texture->getSamplerState(&samplerState);
497 textureSRV = storage11->getSRV(samplerState);
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000498 }
499
500 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
501 // missing the shader resource view
502 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000503
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000504 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000505 }
506
507 if (type == gl::SAMPLER_PIXEL)
508 {
509 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
510 {
511 ERR("Pixel shader sampler index %i is not valid.", index);
512 return;
513 }
514
Geoff Lang91382e52014-01-07 16:16:30 -0500515 if (forceSetTexture || mCurPixelSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000516 {
517 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
518 }
519
Geoff Lang91382e52014-01-07 16:16:30 -0500520 mCurPixelSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000521 }
522 else if (type == gl::SAMPLER_VERTEX)
523 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000524 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000525 {
526 ERR("Vertex shader sampler index %i is not valid.", index);
527 return;
528 }
529
Geoff Lang91382e52014-01-07 16:16:30 -0500530 if (forceSetTexture || mCurVertexSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000531 {
532 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
533 }
534
Geoff Lang91382e52014-01-07 16:16:30 -0500535 mCurVertexSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000536 }
537 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000538}
539
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000540bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
541{
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000542 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
543 {
544 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
545 if (uniformBuffer)
546 {
547 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500548 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000549
550 if (!constantBuffer)
551 {
552 return false;
553 }
554
Geoff Langc6354ee2013-07-22 10:40:07 -0400555 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
556 {
557 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
558 1, &constantBuffer);
559 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
560 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000561 }
562 }
563
564 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
565 {
566 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
567 if (uniformBuffer)
568 {
569 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500570 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000571
572 if (!constantBuffer)
573 {
574 return false;
575 }
576
Geoff Langc6354ee2013-07-22 10:40:07 -0400577 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
578 {
579 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
580 1, &constantBuffer);
581 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
582 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000583 }
584 }
585
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000586 return true;
587}
588
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000589void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000590{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000591 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000592 {
Nicolas Capensaea8e942014-05-09 19:14:08 -0400593 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000594 if (!dxRasterState)
595 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000596 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000597 "rasterizer state.");
598 }
599
600 mDeviceContext->RSSetState(dxRasterState);
601
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000602 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000603 }
604
605 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000606}
607
Geoff Langc142e9d2013-09-30 15:19:47 -0400608void Renderer11::setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000609 unsigned int sampleMask)
610{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000611 if (mForceSetBlendState ||
612 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400613 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000614 sampleMask != mCurSampleMask)
615 {
Geoff Langc142e9d2013-09-30 15:19:47 -0400616 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(framebuffer, blendState);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000617 if (!dxBlendState)
618 {
619 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
620 "blend state.");
621 }
622
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000623 float blendColors[4] = {0.0f};
624 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
625 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
626 {
627 blendColors[0] = blendColor.red;
628 blendColors[1] = blendColor.green;
629 blendColors[2] = blendColor.blue;
630 blendColors[3] = blendColor.alpha;
631 }
632 else
633 {
634 blendColors[0] = blendColor.alpha;
635 blendColors[1] = blendColor.alpha;
636 blendColors[2] = blendColor.alpha;
637 blendColors[3] = blendColor.alpha;
638 }
639
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000640 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
641
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000642 mCurBlendState = blendState;
643 mCurBlendColor = blendColor;
644 mCurSampleMask = sampleMask;
645 }
646
647 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000648}
649
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000650void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000651 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000652{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000653 if (mForceSetDepthStencilState ||
654 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
655 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
656 {
Jamie Madillac528012014-06-20 13:21:23 -0400657 ASSERT(depthStencilState.stencilWritemask == depthStencilState.stencilBackWritemask);
658 ASSERT(stencilRef == stencilBackRef);
659 ASSERT(depthStencilState.stencilMask == depthStencilState.stencilBackMask);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000660
661 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
662 if (!dxDepthStencilState)
663 {
664 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
665 "setting the default depth stencil state.");
666 }
667
Jamie Madillec91cd32014-01-21 16:38:12 -0500668 // Max D3D11 stencil reference value is 0xFF, corresponding to the max 8 bits in a stencil buffer
669 // GL specifies we should clamp the ref value to the nearest bit depth when doing stencil ops
670 META_ASSERT(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF);
671 META_ASSERT(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF);
Geoff Lang0bf3a982014-02-11 09:40:48 -0500672 UINT dxStencilRef = std::min<UINT>(stencilRef, 0xFFu);
Jamie Madillec91cd32014-01-21 16:38:12 -0500673
Geoff Lang0bf3a982014-02-11 09:40:48 -0500674 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, dxStencilRef);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000675
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000676 mCurDepthStencilState = depthStencilState;
677 mCurStencilRef = stencilRef;
678 mCurStencilBackRef = stencilBackRef;
679 }
680
681 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000682}
683
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000684void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000685{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000686 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
687 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000688 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000689 if (enabled)
690 {
691 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000692 rect.left = std::max(0, scissor.x);
693 rect.top = std::max(0, scissor.y);
694 rect.right = scissor.x + std::max(0, scissor.width);
695 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000696
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000697 mDeviceContext->RSSetScissorRects(1, &rect);
698 }
699
700 if (enabled != mScissorEnabled)
701 {
702 mForceSetRasterState = true;
703 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000704
705 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000706 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000707 }
708
709 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000710}
711
daniel@transgaming.com12985182012-12-20 20:56:31 +0000712bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000713 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000714{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000715 gl::Rectangle actualViewport = viewport;
716 float actualZNear = gl::clamp01(zNear);
717 float actualZFar = gl::clamp01(zFar);
718 if (ignoreViewport)
719 {
720 actualViewport.x = 0;
721 actualViewport.y = 0;
722 actualViewport.width = mRenderTargetDesc.width;
723 actualViewport.height = mRenderTargetDesc.height;
724 actualZNear = 0.0f;
725 actualZFar = 1.0f;
726 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000727
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000728 // Get D3D viewport bounds, which depends on the feature level
729 const Range& viewportBounds = getViewportBounds();
730
731 // 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 +0000732 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000733 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
734 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
735 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
736 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
737 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
738 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000739 dxViewport.MinDepth = actualZNear;
740 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000741
742 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
743 {
744 return false; // Nothing to render
745 }
746
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000747 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
748 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000749
daniel@transgaming.com53670042012-11-28 20:55:51 +0000750 if (viewportChanged)
751 {
752 mDeviceContext->RSSetViewports(1, &dxViewport);
753
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000754 mCurViewport = actualViewport;
755 mCurNear = actualZNear;
756 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000757
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000758 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
759 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
760 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
761 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000762
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000763 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
764 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000765
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000766 mVertexConstants.depthRange[0] = actualZNear;
767 mVertexConstants.depthRange[1] = actualZFar;
768 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000769
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000770 mPixelConstants.depthRange[0] = actualZNear;
771 mPixelConstants.depthRange[1] = actualZFar;
772 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000773 }
774
775 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000776 return true;
777}
778
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000779bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
780{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000781 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000782
Geoff Lang57e713e2013-07-31 17:01:58 -0400783 GLsizei minCount = 0;
784
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000785 switch (mode)
786 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400787 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
788 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
789 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
790 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
791 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
792 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000793 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400794 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000795 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000796 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000797 }
798
Geoff Lang4c095862013-07-22 10:43:36 -0400799 if (primitiveTopology != mCurrentPrimitiveTopology)
800 {
801 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
802 mCurrentPrimitiveTopology = primitiveTopology;
803 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000804
Geoff Lang57e713e2013-07-31 17:01:58 -0400805 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000806}
807
808bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000809{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000810 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000811 // Also extract the render target dimensions and view
812 unsigned int renderTargetWidth = 0;
813 unsigned int renderTargetHeight = 0;
814 GLenum renderTargetFormat = 0;
815 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
816 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
817 bool missingColorRenderTarget = true;
818
819 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000820 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000821 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
822
823 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000824 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000825 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
826 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
827
Jamie Madill3c7fa222014-06-05 13:08:51 -0400828 gl::FramebufferAttachment *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000829
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000830 if (!colorbuffer)
831 {
832 ERR("render target pointer unexpectedly null.");
833 return false;
834 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000835
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000836 // check for zero-sized default framebuffer, which is a special case.
837 // in this case we do not wish to modify any state and just silently return false.
838 // this will not report any gl error but will cause the calling method to return.
839 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
840 {
841 return false;
842 }
843
844 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
845
846 // Extract the render target dimensions and view
847 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
848 if (!renderTarget)
849 {
850 ERR("render target pointer unexpectedly null.");
851 return false;
852 }
853
854 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
855 if (!framebufferRTVs[colorAttachment])
856 {
857 ERR("render target view pointer unexpectedly null.");
858 return false;
859 }
860
861 if (missingColorRenderTarget)
862 {
863 renderTargetWidth = colorbuffer->getWidth();
864 renderTargetHeight = colorbuffer->getHeight();
865 renderTargetFormat = colorbuffer->getActualFormat();
866 missingColorRenderTarget = false;
867 }
Jamie Madillba597af2013-10-22 13:12:15 -0400868
Geoff Lang91382e52014-01-07 16:16:30 -0500869 // TODO: Detect if this color buffer is already bound as a texture and unbind it first to prevent
870 // D3D11 warnings.
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000871 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000872 }
873
874 // Get the depth stencil render buffer and serials
Jamie Madill3c7fa222014-06-05 13:08:51 -0400875 gl::FramebufferAttachment *depthStencil = NULL;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000876 unsigned int depthbufferSerial = 0;
877 unsigned int stencilbufferSerial = 0;
878 if (framebuffer->getDepthbufferType() != GL_NONE)
879 {
880 depthStencil = framebuffer->getDepthbuffer();
881 if (!depthStencil)
882 {
883 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000884 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000885 return false;
886 }
887
888 depthbufferSerial = depthStencil->getSerial();
889 }
890 else if (framebuffer->getStencilbufferType() != GL_NONE)
891 {
892 depthStencil = framebuffer->getStencilbuffer();
893 if (!depthStencil)
894 {
895 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000896 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000897 return false;
898 }
899
900 stencilbufferSerial = depthStencil->getSerial();
901 }
902
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000903 ID3D11DepthStencilView* framebufferDSV = NULL;
904 if (depthStencil)
905 {
906 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
907 if (!depthStencilRenderTarget)
908 {
909 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000910 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000911 return false;
912 }
913
914 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
915 if (!framebufferDSV)
916 {
917 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000918 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000919 return false;
920 }
921
922 // If there is no render buffer, the width, height and format values come from
923 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000924 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000925 {
926 renderTargetWidth = depthStencil->getWidth();
927 renderTargetHeight = depthStencil->getHeight();
928 renderTargetFormat = depthStencil->getActualFormat();
929 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000930 }
931
932 // Apply the render target and depth stencil
933 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000934 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000935 depthbufferSerial != mAppliedDepthbufferSerial ||
936 stencilbufferSerial != mAppliedStencilbufferSerial)
937 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000938 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000939
940 mRenderTargetDesc.width = renderTargetWidth;
941 mRenderTargetDesc.height = renderTargetHeight;
942 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000943 mForceSetViewport = true;
944 mForceSetScissor = true;
Geoff Langc142e9d2013-09-30 15:19:47 -0400945 mForceSetBlendState = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000946
Nicolas Capensaea8e942014-05-09 19:14:08 -0400947 if (!mDepthStencilInitialized)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000948 {
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000949 mForceSetRasterState = true;
950 }
951
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000952 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
953 {
954 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
955 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000956 mAppliedDepthbufferSerial = depthbufferSerial;
957 mAppliedStencilbufferSerial = stencilbufferSerial;
958 mRenderTargetDescInitialized = true;
959 mDepthStencilInitialized = true;
960 }
961
Geoff Lang42477a42013-09-17 17:07:02 -0400962 invalidateFramebufferSwizzles(framebuffer);
963
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000964 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000965}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000966
Jamie Madill57a89722013-07-02 11:57:03 -0400967GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -0400968 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000969{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000970 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -0400971 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000972 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000973 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000974 return err;
975 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000976
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000977 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000978}
979
daniel@transgaming.com31240482012-11-28 21:06:41 +0000980GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000981{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000982 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000983
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000984 if (err == GL_NO_ERROR)
985 {
Geoff Lang7840b172014-03-13 11:20:44 -0400986 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
987
988 ID3D11Buffer *buffer = NULL;
989 DXGI_FORMAT bufferFormat = indexBuffer->getIndexFormat();
990
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000991 if (indexInfo->storage)
992 {
Geoff Lang7840b172014-03-13 11:20:44 -0400993 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
994 buffer = storage->getBuffer(BUFFER_USAGE_INDEX);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000995 }
Geoff Lang7840b172014-03-13 11:20:44 -0400996 else
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000997 {
Geoff Lang7840b172014-03-13 11:20:44 -0400998 buffer = indexBuffer->getBuffer();
999 }
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001000
Geoff Lang7840b172014-03-13 11:20:44 -04001001 if (buffer != mAppliedIB || bufferFormat != mAppliedIBFormat || indexInfo->startOffset != mAppliedIBOffset)
1002 {
1003 mDeviceContext->IASetIndexBuffer(buffer, bufferFormat, indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001004
Geoff Lang7840b172014-03-13 11:20:44 -04001005 mAppliedIB = buffer;
1006 mAppliedIBFormat = bufferFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001007 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001008 }
1009 }
1010
1011 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001012}
1013
Geoff Langeeba6e12014-02-03 13:12:30 -05001014void Renderer11::applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[])
1015{
1016 ID3D11Buffer* d3dBuffers[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1017 UINT d3dOffsets[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1018 bool requiresUpdate = false;
1019 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1020 {
1021 if (transformFeedbackBuffers[i])
1022 {
1023 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(transformFeedbackBuffers[i]->getStorage());
1024 ID3D11Buffer *buffer = storage->getBuffer(BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK);
1025
1026 d3dBuffers[i] = buffer;
1027 d3dOffsets[i] = (mAppliedTFBuffers[i] != buffer) ? static_cast<UINT>(offsets[i]) : -1;
1028 }
1029 else
1030 {
1031 d3dBuffers[i] = NULL;
1032 d3dOffsets[i] = 0;
1033 }
1034
1035 if (d3dBuffers[i] != mAppliedTFBuffers[i] || offsets[i] != mAppliedTFOffsets[i])
1036 {
1037 requiresUpdate = true;
1038 }
1039 }
1040
1041 if (requiresUpdate)
1042 {
1043 mDeviceContext->SOSetTargets(ArraySize(d3dBuffers), d3dBuffers, d3dOffsets);
1044 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1045 {
1046 mAppliedTFBuffers[i] = d3dBuffers[i];
1047 mAppliedTFOffsets[i] = offsets[i];
1048 }
1049 }
1050}
1051
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001052void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001053{
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001054 if (mode == GL_POINTS && transformFeedbackActive)
1055 {
1056 // Since point sprites are generated with a geometry shader, too many vertices will
1057 // be written if transform feedback is active. To work around this, draw only the points
1058 // with the stream out shader and no pixel shader to feed the stream out buffers and then
1059 // draw again with the point sprite geometry shader to rasterize the point sprites.
1060
1061 mDeviceContext->PSSetShader(NULL, NULL, 0);
1062
1063 if (instances > 0)
1064 {
1065 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1066 }
1067 else
1068 {
1069 mDeviceContext->Draw(count, 0);
1070 }
1071
1072 mDeviceContext->GSSetShader(mCurPointGeometryShader, NULL, 0);
1073 mDeviceContext->PSSetShader(mAppliedPixelShader, NULL, 0);
1074
1075 if (instances > 0)
1076 {
1077 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1078 }
1079 else
1080 {
1081 mDeviceContext->Draw(count, 0);
1082 }
1083
1084 mDeviceContext->GSSetShader(mAppliedGeometryShader, NULL, 0);
1085 }
1086 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001087 {
1088 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1089 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001090 else if (mode == GL_TRIANGLE_FAN)
1091 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001092 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001093 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001094 else if (instances > 0)
1095 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001096 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001097 }
1098 else
1099 {
1100 mDeviceContext->Draw(count, 0);
1101 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001102}
1103
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001104void Renderer11::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices,
1105 gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001106{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001107 if (mode == GL_LINE_LOOP)
1108 {
1109 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1110 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001111 else if (mode == GL_TRIANGLE_FAN)
1112 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001113 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1114 }
1115 else if (instances > 0)
1116 {
1117 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001118 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001119 else
1120 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001121 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001122 }
1123}
1124
1125void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1126{
1127 // Get the raw indices for an indexed draw
1128 if (type != GL_NONE && elementArrayBuffer)
1129 {
1130 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001131 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001132 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001133 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001134 }
1135
1136 if (!mLineLoopIB)
1137 {
1138 mLineLoopIB = new StreamingIndexBufferInterface(this);
1139 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1140 {
1141 delete mLineLoopIB;
1142 mLineLoopIB = NULL;
1143
1144 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001145 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001146 }
1147 }
1148
Geoff Lang57e713e2013-07-31 17:01:58 -04001149 // Checked by Renderer11::applyPrimitiveType
1150 ASSERT(count >= 0);
1151
1152 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001153 {
1154 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1155 return gl::error(GL_OUT_OF_MEMORY);
1156 }
1157
Geoff Lang57e713e2013-07-31 17:01:58 -04001158 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001159 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1160 {
1161 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001162 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001163 }
1164
1165 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001166 unsigned int offset;
1167 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001168 {
1169 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001170 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001171 }
1172
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001173 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001174 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001175
1176 switch (type)
1177 {
1178 case GL_NONE: // Non-indexed draw
1179 for (int i = 0; i < count; i++)
1180 {
1181 data[i] = i;
1182 }
1183 data[count] = 0;
1184 break;
1185 case GL_UNSIGNED_BYTE:
1186 for (int i = 0; i < count; i++)
1187 {
1188 data[i] = static_cast<const GLubyte*>(indices)[i];
1189 }
1190 data[count] = static_cast<const GLubyte*>(indices)[0];
1191 break;
1192 case GL_UNSIGNED_SHORT:
1193 for (int i = 0; i < count; i++)
1194 {
1195 data[i] = static_cast<const GLushort*>(indices)[i];
1196 }
1197 data[count] = static_cast<const GLushort*>(indices)[0];
1198 break;
1199 case GL_UNSIGNED_INT:
1200 for (int i = 0; i < count; i++)
1201 {
1202 data[i] = static_cast<const GLuint*>(indices)[i];
1203 }
1204 data[count] = static_cast<const GLuint*>(indices)[0];
1205 break;
1206 default: UNREACHABLE();
1207 }
1208
1209 if (!mLineLoopIB->unmapBuffer())
1210 {
1211 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001212 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001213 }
1214
Geoff Lang7840b172014-03-13 11:20:44 -04001215 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1216 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1217 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001218
Geoff Lang7840b172014-03-13 11:20:44 -04001219 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1220 {
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001221 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001222 mAppliedIB = d3dIndexBuffer;
1223 mAppliedIBFormat = indexFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001224 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001225 }
1226
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001227 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001228}
1229
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001230void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001231{
1232 // Get the raw indices for an indexed draw
1233 if (type != GL_NONE && elementArrayBuffer)
1234 {
1235 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001236 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001237 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001238 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001239 }
1240
1241 if (!mTriangleFanIB)
1242 {
1243 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1244 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1245 {
1246 delete mTriangleFanIB;
1247 mTriangleFanIB = NULL;
1248
1249 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001250 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001251 }
1252 }
1253
Geoff Lang57e713e2013-07-31 17:01:58 -04001254 // Checked by Renderer11::applyPrimitiveType
1255 ASSERT(count >= 3);
1256
Geoff Langeadfd572013-07-09 15:55:07 -04001257 const unsigned int numTris = count - 2;
1258
Geoff Lang57e713e2013-07-31 17:01:58 -04001259 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001260 {
1261 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1262 return gl::error(GL_OUT_OF_MEMORY);
1263 }
1264
1265 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001266 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1267 {
1268 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001269 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001270 }
1271
1272 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001273 unsigned int offset;
1274 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001275 {
1276 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001277 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001278 }
1279
1280 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001281 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001282
1283 switch (type)
1284 {
1285 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001286 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001287 {
1288 data[i*3 + 0] = 0;
1289 data[i*3 + 1] = i + 1;
1290 data[i*3 + 2] = i + 2;
1291 }
1292 break;
1293 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001294 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001295 {
1296 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1297 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1298 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1299 }
1300 break;
1301 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001302 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001303 {
1304 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1305 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1306 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1307 }
1308 break;
1309 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001310 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001311 {
1312 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1313 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1314 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1315 }
1316 break;
1317 default: UNREACHABLE();
1318 }
1319
1320 if (!mTriangleFanIB->unmapBuffer())
1321 {
1322 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001323 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001324 }
1325
Geoff Lang7840b172014-03-13 11:20:44 -04001326 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1327 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1328 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001329
Geoff Lang7840b172014-03-13 11:20:44 -04001330 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1331 {
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001332 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001333 mAppliedIB = d3dIndexBuffer;
1334 mAppliedIBFormat = indexFormat;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001335 mAppliedIBOffset = indexBufferOffset;
1336 }
1337
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001338 if (instances > 0)
1339 {
1340 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1341 }
1342 else
1343 {
1344 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1345 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001346}
1347
Geoff Lang04fb89a2014-06-09 15:05:36 -04001348void Renderer11::applyShaders(gl::ProgramBinary *programBinary, const gl::VertexFormat inputLayout[], const gl::Framebuffer *framebuffer,
1349 bool rasterizerDiscard, bool transformFeedbackActive)
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001350{
Jamie Madillc5a83002014-02-14 16:41:25 -05001351 ShaderExecutable *vertexExe = programBinary->getVertexExecutableForInputLayout(inputLayout);
Geoff Lang04fb89a2014-06-09 15:05:36 -04001352 ShaderExecutable *pixelExe = programBinary->getPixelExecutableForFramebuffer(framebuffer);
Jamie Madill6246dc82014-01-29 09:26:47 -05001353 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001354
Jamie Madill6246dc82014-01-29 09:26:47 -05001355 ID3D11VertexShader *vertexShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader() : NULL);
Jamie Madill6246dc82014-01-29 09:26:47 -05001356
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001357 ID3D11PixelShader *pixelShader = NULL;
1358 // Skip pixel shader if we're doing rasterizer discard.
1359 if (!rasterizerDiscard)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001360 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001361 pixelShader = (pixelExe ? ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader() : NULL);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001362 }
1363
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001364 ID3D11GeometryShader *geometryShader = NULL;
1365 if (transformFeedbackActive)
Geoff Lang0550d032014-01-30 11:29:07 -05001366 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001367 geometryShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getStreamOutShader() : NULL);
1368 }
1369 else if (mCurRasterState.pointDrawMode)
1370 {
1371 geometryShader = (geometryExe ? ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader() : NULL);
Geoff Lang0550d032014-01-30 11:29:07 -05001372 }
1373
Jamie Madill6246dc82014-01-29 09:26:47 -05001374 bool dirtyUniforms = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001375
Jamie Madill6246dc82014-01-29 09:26:47 -05001376 if (vertexShader != mAppliedVertexShader)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001377 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001378 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1379 mAppliedVertexShader = vertexShader;
1380 dirtyUniforms = true;
1381 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001382
Jamie Madill6246dc82014-01-29 09:26:47 -05001383 if (geometryShader != mAppliedGeometryShader)
1384 {
1385 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1386 mAppliedGeometryShader = geometryShader;
1387 dirtyUniforms = true;
1388 }
1389
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001390 if (geometryExe && mCurRasterState.pointDrawMode)
1391 {
1392 mCurPointGeometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
1393 }
1394 else
1395 {
1396 mCurPointGeometryShader = NULL;
1397 }
1398
Jamie Madill6246dc82014-01-29 09:26:47 -05001399 if (pixelShader != mAppliedPixelShader)
1400 {
1401 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1402 mAppliedPixelShader = pixelShader;
1403 dirtyUniforms = true;
1404 }
1405
1406 if (dirtyUniforms)
1407 {
1408 programBinary->dirtyAllUniforms();
daniel@transgaming.come4991412012-12-20 20:55:34 +00001409 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001410}
1411
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001412void Renderer11::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001413{
Jamie Madill834e8b72014-04-11 13:33:58 -04001414 const std::vector<gl::LinkedUniform*> &uniformArray = programBinary.getUniforms();
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001415
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001416 unsigned int totalRegisterCountVS = 0;
1417 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001418
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001419 bool vertexUniformsDirty = false;
1420 bool pixelUniformsDirty = false;
1421
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001422 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001423 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001424 const gl::LinkedUniform &uniform = *uniformArray[uniformIndex];
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001425
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001426 if (uniform.isReferencedByVertexShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001427 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001428 totalRegisterCountVS += uniform.registerCount;
1429 vertexUniformsDirty = (vertexUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001430 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001431
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001432 if (uniform.isReferencedByFragmentShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001433 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001434 totalRegisterCountPS += uniform.registerCount;
1435 pixelUniformsDirty = (pixelUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001436 }
1437 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001438
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001439 const UniformStorage11 *vertexUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getVertexUniformStorage());
1440 const UniformStorage11 *fragmentUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getFragmentUniformStorage());
1441 ASSERT(vertexUniformStorage);
1442 ASSERT(fragmentUniformStorage);
1443
1444 ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
1445 ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001446
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001447 float (*mapVS)[4] = NULL;
1448 float (*mapPS)[4] = NULL;
1449
Shannon Woods5ab33c82013-06-26 15:31:09 -04001450 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1451 {
1452 D3D11_MAPPED_SUBRESOURCE map = {0};
1453 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001454 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001455 ASSERT(SUCCEEDED(result));
1456 mapVS = (float(*)[4])map.pData;
1457 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001458
Shannon Woods5ab33c82013-06-26 15:31:09 -04001459 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1460 {
1461 D3D11_MAPPED_SUBRESOURCE map = {0};
1462 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001463 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001464 ASSERT(SUCCEEDED(result));
1465 mapPS = (float(*)[4])map.pData;
1466 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001467
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001468 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001469 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001470 gl::LinkedUniform *uniform = uniformArray[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001471
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001472 if (!uniform->isSampler())
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001473 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001474 unsigned int componentCount = (4 - uniform->registerElement);
1475
Jamie Madill71cc91f2013-09-18 12:51:22 -04001476 // 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 -04001477 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001478
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001479 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001480 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001481 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001482 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001483
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001484 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001485 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001486 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001487 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001488 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001489 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001490
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001491 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001492 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001493 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001494 }
1495
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001496 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001497 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001498 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001499 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001500
1501 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1502 {
1503 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1504 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1505 }
1506
1507 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1508 {
1509 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1510 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1511 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001512
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001513 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001514 if (!mDriverConstantBufferVS)
1515 {
1516 D3D11_BUFFER_DESC constantBufferDescription = {0};
1517 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1518 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1519 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1520 constantBufferDescription.CPUAccessFlags = 0;
1521 constantBufferDescription.MiscFlags = 0;
1522 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001523
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001524 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001525 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001526 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001527
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001528 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1529 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001530
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001531 if (!mDriverConstantBufferPS)
1532 {
1533 D3D11_BUFFER_DESC constantBufferDescription = {0};
1534 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1535 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1536 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1537 constantBufferDescription.CPUAccessFlags = 0;
1538 constantBufferDescription.MiscFlags = 0;
1539 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001540
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001541 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001542 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001543 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001544
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001545 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1546 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001547
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001548 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1549 {
1550 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1551 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1552 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001553
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001554 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1555 {
1556 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1557 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1558 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001559
1560 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001561 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1562 {
1563 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1564 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1565 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001566}
1567
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001568void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001569{
Geoff Langda507fe2013-08-20 12:01:42 -04001570 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001571 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001572}
1573
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001574void Renderer11::markAllStateDirty()
1575{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001576 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1577 {
1578 mAppliedRenderTargetSerials[rtIndex] = 0;
1579 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001580 mAppliedDepthbufferSerial = 0;
1581 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001582 mDepthStencilInitialized = false;
1583 mRenderTargetDescInitialized = false;
1584
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001585 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001586 {
1587 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001588 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001589 }
1590 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1591 {
1592 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001593 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001594 }
1595
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001596 mForceSetBlendState = true;
1597 mForceSetRasterState = true;
1598 mForceSetDepthStencilState = true;
1599 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001600 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001601
Geoff Lang7840b172014-03-13 11:20:44 -04001602 mAppliedIB = NULL;
1603 mAppliedIBFormat = DXGI_FORMAT_UNKNOWN;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001604 mAppliedIBOffset = 0;
1605
Jamie Madill6246dc82014-01-29 09:26:47 -05001606 mAppliedVertexShader = NULL;
1607 mAppliedGeometryShader = NULL;
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001608 mCurPointGeometryShader = NULL;
Jamie Madill6246dc82014-01-29 09:26:47 -05001609 mAppliedPixelShader = NULL;
Geoff Langeeba6e12014-02-03 13:12:30 -05001610
1611 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1612 {
1613 mAppliedTFBuffers[i] = NULL;
1614 mAppliedTFOffsets[i] = 0;
1615 }
1616
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001617 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1618 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001619
1620 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001621
1622 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1623 {
1624 mCurrentConstantBufferVS[i] = -1;
1625 mCurrentConstantBufferPS[i] = -1;
1626 }
1627
1628 mCurrentVertexConstantBuffer = NULL;
1629 mCurrentPixelConstantBuffer = NULL;
1630 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001631
1632 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001633}
1634
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001635void Renderer11::releaseDeviceResources()
1636{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001637 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001638 mInputLayoutCache.clear();
1639
Geoff Langea228632013-07-30 15:17:12 -04001640 SafeDelete(mVertexDataManager);
1641 SafeDelete(mIndexDataManager);
1642 SafeDelete(mLineLoopIB);
1643 SafeDelete(mTriangleFanIB);
1644 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001645 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001646 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001647
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001648 SafeRelease(mDriverConstantBufferVS);
1649 SafeRelease(mDriverConstantBufferPS);
1650 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001651}
1652
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001653void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001654{
1655 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001656 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001657}
1658
1659bool Renderer11::isDeviceLost()
1660{
1661 return mDeviceLost;
1662}
1663
1664// set notify to true to broadcast a message to all contexts of the device loss
1665bool Renderer11::testDeviceLost(bool notify)
1666{
1667 bool isLost = false;
1668
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001669 // GetRemovedReason is used to test if the device is removed
1670 HRESULT result = mDevice->GetDeviceRemovedReason();
1671 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001672
1673 if (isLost)
1674 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001675 // Log error if this is a new device lost event
1676 if (mDeviceLost == false)
1677 {
1678 ERR("The D3D11 device was removed: 0x%08X", result);
1679 }
1680
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001681 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001682 // we'll probably get this done again by notifyDeviceLost
1683 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001684 // Note that we don't want to clear the device loss status here
1685 // -- this needs to be done by resetDevice
1686 mDeviceLost = true;
1687 if (notify)
1688 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001689 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001690 }
1691 }
1692
1693 return isLost;
1694}
1695
1696bool Renderer11::testDeviceResettable()
1697{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001698 // determine if the device is resettable by creating a dummy device
1699 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001700
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001701 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001702 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001703 return false;
1704 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001705
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001706 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001707 {
1708 D3D_FEATURE_LEVEL_11_0,
1709 D3D_FEATURE_LEVEL_10_1,
1710 D3D_FEATURE_LEVEL_10_0,
1711 };
1712
1713 ID3D11Device* dummyDevice;
1714 D3D_FEATURE_LEVEL dummyFeatureLevel;
1715 ID3D11DeviceContext* dummyContext;
1716
1717 HRESULT result = D3D11CreateDevice(NULL,
1718 D3D_DRIVER_TYPE_HARDWARE,
1719 NULL,
1720 #if defined(_DEBUG)
1721 D3D11_CREATE_DEVICE_DEBUG,
1722 #else
1723 0,
1724 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001725 featureLevels,
1726 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001727 D3D11_SDK_VERSION,
1728 &dummyDevice,
1729 &dummyFeatureLevel,
1730 &dummyContext);
1731
1732 if (!mDevice || FAILED(result))
1733 {
1734 return false;
1735 }
1736
Geoff Langea228632013-07-30 15:17:12 -04001737 SafeRelease(dummyContext);
1738 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001739
1740 return true;
1741}
1742
1743void Renderer11::release()
1744{
1745 releaseDeviceResources();
1746
Geoff Langea228632013-07-30 15:17:12 -04001747 SafeRelease(mDxgiFactory);
1748 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001749
1750 if (mDeviceContext)
1751 {
1752 mDeviceContext->ClearState();
1753 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001754 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001755 }
1756
Geoff Langea228632013-07-30 15:17:12 -04001757 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001758
1759 if (mD3d11Module)
1760 {
1761 FreeLibrary(mD3d11Module);
1762 mD3d11Module = NULL;
1763 }
1764
1765 if (mDxgiModule)
1766 {
1767 FreeLibrary(mDxgiModule);
1768 mDxgiModule = NULL;
1769 }
Geoff Langdad5ed32014-02-10 12:59:17 -05001770
1771 mCompiler.release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001772}
1773
1774bool Renderer11::resetDevice()
1775{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001776 // recreate everything
1777 release();
1778 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001779
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001780 if (result != EGL_SUCCESS)
1781 {
1782 ERR("Could not reinitialize D3D11 device: %08X", result);
1783 return false;
1784 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001785
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001786 mDeviceLost = false;
1787
1788 return true;
1789}
1790
1791DWORD Renderer11::getAdapterVendor() const
1792{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001793 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001794}
1795
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001796std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001797{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001798 std::ostringstream rendererString;
1799
1800 rendererString << mDescription;
1801 rendererString << " Direct3D11";
1802
1803 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1804 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1805
1806 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001807}
1808
1809GUID Renderer11::getAdapterIdentifier() const
1810{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001811 // Use the adapter LUID as our adapter ID
1812 // This number is local to a machine is only guaranteed to be unique between restarts
1813 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1814 GUID adapterId = {0};
1815 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1816 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001817}
1818
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001819Range Renderer11::getViewportBounds() const
1820{
1821 switch (mFeatureLevel)
1822 {
1823 case D3D_FEATURE_LEVEL_11_0:
1824 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1825 case D3D_FEATURE_LEVEL_10_1:
1826 case D3D_FEATURE_LEVEL_10_0:
1827 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1828 default: UNREACHABLE();
1829 return Range(0, 0);
1830 }
1831}
1832
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001833unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001834{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001835 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1836 switch (mFeatureLevel)
1837 {
1838 case D3D_FEATURE_LEVEL_11_0:
1839 case D3D_FEATURE_LEVEL_10_1:
1840 case D3D_FEATURE_LEVEL_10_0:
1841 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1842 default: UNREACHABLE();
1843 return 0;
1844 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001845}
1846
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001847unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1848{
1849 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1850}
1851
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001852unsigned int Renderer11::getReservedVertexUniformVectors() const
1853{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001854 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001855}
1856
1857unsigned int Renderer11::getReservedFragmentUniformVectors() const
1858{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001859 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001860}
1861
1862unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001863{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001864 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1865 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1866 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001867}
1868
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001869unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001870{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001871 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1872 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1873 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001874}
1875
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001876unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001877{
1878 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001879 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1880 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001881 switch (mFeatureLevel)
1882 {
1883 case D3D_FEATURE_LEVEL_11_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001884 return D3D11_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001885 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001886 return D3D10_1_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001887 case D3D_FEATURE_LEVEL_10_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001888 return D3D10_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001889 default: UNREACHABLE();
1890 return 0;
1891 }
1892}
1893
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001894unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
1895{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001896 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1897 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1898
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001899 switch (mFeatureLevel)
1900 {
1901 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001902 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001903 case D3D_FEATURE_LEVEL_10_1:
1904 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001905 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001906 default: UNREACHABLE();
1907 return 0;
1908 }
1909}
1910
1911unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
1912{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001913 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1914 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1915
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001916 switch (mFeatureLevel)
1917 {
1918 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001919 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001920 case D3D_FEATURE_LEVEL_10_1:
1921 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001922 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001923 default: UNREACHABLE();
1924 return 0;
1925 }
1926}
1927
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001928unsigned int Renderer11::getReservedVertexUniformBuffers() const
1929{
1930 // we reserve one buffer for the application uniforms, and one for driver uniforms
1931 return 2;
1932}
1933
1934unsigned int Renderer11::getReservedFragmentUniformBuffers() const
1935{
1936 // we reserve one buffer for the application uniforms, and one for driver uniforms
1937 return 2;
1938}
1939
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001940unsigned int Renderer11::getReservedVaryings() const
1941{
Jamie Madill2bf8b372014-06-16 17:18:51 -04001942 // We potentially reserve varyings for gl_Position, dx_Position, gl_FragCoord and gl_PointSize
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001943 return 4;
1944}
1945
1946
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001947unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
1948{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001949 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
1950 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
1951
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001952 switch (mFeatureLevel)
1953 {
1954 case D3D_FEATURE_LEVEL_11_0:
1955 return D3D11_SO_BUFFER_SLOT_COUNT;
1956 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001957 return D3D10_1_SO_BUFFER_SLOT_COUNT;
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001958 case D3D_FEATURE_LEVEL_10_0:
1959 return D3D10_SO_BUFFER_SLOT_COUNT;
1960 default: UNREACHABLE();
1961 return 0;
1962 }
1963}
1964
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001965unsigned int Renderer11::getMaxTransformFeedbackSeparateComponents() const
1966{
1967 switch (mFeatureLevel)
1968 {
1969 case D3D_FEATURE_LEVEL_11_0:
1970 return getMaxTransformFeedbackInterleavedComponents() / getMaxTransformFeedbackBuffers();
1971 case D3D_FEATURE_LEVEL_10_1:
1972 case D3D_FEATURE_LEVEL_10_0:
1973 // D3D 10 and 10.1 only allow one output per output slot if an output slot other than zero
1974 // is used.
1975 return 4;
1976 default: UNREACHABLE();
1977 return 0;
1978 }
1979}
1980
1981unsigned int Renderer11::getMaxTransformFeedbackInterleavedComponents() const
1982{
1983 return (getMaxVaryingVectors() * 4);
1984}
1985
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00001986unsigned int Renderer11::getMaxUniformBufferSize() const
1987{
1988 // Each component is a 4-element vector of 4-byte units (floats)
1989 const unsigned int bytesPerComponent = 4 * sizeof(float);
1990
1991 switch (mFeatureLevel)
1992 {
1993 case D3D_FEATURE_LEVEL_11_0:
1994 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1995 case D3D_FEATURE_LEVEL_10_1:
1996 case D3D_FEATURE_LEVEL_10_0:
1997 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1998 default: UNREACHABLE();
1999 return 0;
2000 }
2001}
2002
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002003bool Renderer11::getShareHandleSupport() const
2004{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002005 // We only currently support share handles with BGRA surfaces, because
2006 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002007 // PIX doesn't seem to support using share handles, so disable them.
Geoff Langcec35902014-04-16 10:52:36 -04002008 return getCaps().extensions.textureFormatBGRA8888 && !gl::perfActive();
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002009}
2010
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002011bool Renderer11::getPostSubBufferSupport() const
2012{
2013 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2014 return false;
2015}
2016
Jamie Madill13a2f852013-12-11 16:35:08 -05002017int Renderer11::getMaxRecommendedElementsIndices() const
2018{
2019 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2020 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2021
2022 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2023 return std::numeric_limits<GLint>::max();
2024}
2025
2026int Renderer11::getMaxRecommendedElementsVertices() const
2027{
2028 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2029 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2030
2031 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2032 return std::numeric_limits<GLint>::max();
2033}
2034
Geoff Lang05b05022014-06-11 15:31:45 -04002035bool Renderer11::getSRGBTextureSupport() const
2036{
2037 return true;
2038}
2039
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002040int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002041{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002042 switch (mFeatureLevel)
2043 {
2044 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002045 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002046 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2047 default: UNREACHABLE(); return 0;
2048 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002049}
2050
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002051int Renderer11::getMinorShaderModel() const
2052{
2053 switch (mFeatureLevel)
2054 {
2055 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2056 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2057 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2058 default: UNREACHABLE(); return 0;
2059 }
2060}
2061
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002062float Renderer11::getMaxPointSize() const
2063{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002064 // choose a reasonable maximum. we enforce this in the shader.
2065 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2066 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002067}
2068
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002069int Renderer11::getMaxViewportDimension() const
2070{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002071 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2072 // In our case return the maximum texture size, which is the maximum render buffer size.
2073 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2074 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2075
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002076 switch (mFeatureLevel)
2077 {
2078 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002079 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002080 case D3D_FEATURE_LEVEL_10_1:
2081 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002082 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002083 default: UNREACHABLE();
2084 return 0;
2085 }
2086}
2087
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002088int Renderer11::getMaxTextureWidth() const
2089{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002090 switch (mFeatureLevel)
2091 {
2092 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2093 case D3D_FEATURE_LEVEL_10_1:
2094 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2095 default: UNREACHABLE(); return 0;
2096 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002097}
2098
2099int Renderer11::getMaxTextureHeight() const
2100{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002101 switch (mFeatureLevel)
2102 {
2103 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2104 case D3D_FEATURE_LEVEL_10_1:
2105 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2106 default: UNREACHABLE(); return 0;
2107 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002108}
2109
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002110int Renderer11::getMaxTextureDepth() const
2111{
2112 switch (mFeatureLevel)
2113 {
2114 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2115 case D3D_FEATURE_LEVEL_10_1:
2116 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2117 default: UNREACHABLE(); return 0;
2118 }
2119}
2120
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002121int Renderer11::getMaxTextureArrayLayers() const
2122{
2123 switch (mFeatureLevel)
2124 {
2125 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2126 case D3D_FEATURE_LEVEL_10_1:
2127 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2128 default: UNREACHABLE(); return 0;
2129 }
2130}
2131
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002132int Renderer11::getMinSwapInterval() const
2133{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002134 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002135}
2136
2137int Renderer11::getMaxSwapInterval() const
2138{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002139 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002140}
2141
2142int Renderer11::getMaxSupportedSamples() const
2143{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002144 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002145}
2146
Geoff Lang005df412013-10-16 14:12:50 -04002147GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002148{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002149 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002150 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2151 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2152}
2153
Geoff Lang005df412013-10-16 14:12:50 -04002154GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002155{
2156 unsigned int numCounts = 0;
2157
2158 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002159 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2160 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002161 {
2162 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2163 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2164
2165 if (iter != mMultisampleSupportMap.end())
2166 {
2167 const MultisampleSupportInfo& info = iter->second;
2168 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2169 {
2170 if (info.qualityLevels[i] > 0)
2171 {
2172 numCounts++;
2173 }
2174 }
2175 }
2176 }
2177
2178 return numCounts;
2179}
2180
Geoff Lang005df412013-10-16 14:12:50 -04002181void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002182{
2183 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002184 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2185 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2186 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002187 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002188 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002189
2190 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2191 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2192
2193 if (iter != mMultisampleSupportMap.end())
2194 {
2195 const MultisampleSupportInfo& info = iter->second;
2196 int bufPos = 0;
2197 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2198 {
2199 if (info.qualityLevels[i] > 0)
2200 {
2201 params[bufPos++] = i + 1;
2202 }
2203 }
2204 }
2205}
2206
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002207int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2208{
2209 if (requested == 0)
2210 {
2211 return 0;
2212 }
2213
2214 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2215 if (iter != mMultisampleSupportMap.end())
2216 {
2217 const MultisampleSupportInfo& info = iter->second;
2218 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2219 {
2220 if (info.qualityLevels[i] > 0)
2221 {
2222 return i + 1;
2223 }
2224 }
2225 }
2226
2227 return -1;
2228}
2229
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002230unsigned int Renderer11::getMaxRenderTargets() const
2231{
Geoff Lang4ace4232014-06-18 19:12:48 -04002232 return d3d11::GetMaximumSimultaneousRenderTargets(mFeatureLevel);
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002233}
2234
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002235bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002236{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002237 if (source && dest)
2238 {
2239 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2240 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2241
Nicolas Capens76b258f2014-04-03 10:59:42 -04002242 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002243
2244 dest11->invalidateSwizzleCache();
2245
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002246 return true;
2247 }
2248
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002249 return false;
2250}
2251
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002252bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002253{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002254 if (source && dest)
2255 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002256 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2257 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002258
Nicolas Capens76b258f2014-04-03 10:59:42 -04002259 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002260
2261 dest11->invalidateSwizzleCache();
2262
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002263 return true;
2264 }
2265
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002266 return false;
2267}
2268
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002269bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2270{
2271 if (source && dest)
2272 {
2273 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2274 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2275
Nicolas Capens76b258f2014-04-03 10:59:42 -04002276 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002277
2278 dest11->invalidateSwizzleCache();
2279
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002280 return true;
2281 }
2282
2283 return false;
2284}
2285
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002286bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2287{
2288 if (source && dest)
2289 {
2290 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2291 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2292
Nicolas Capens76b258f2014-04-03 10:59:42 -04002293 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002294
2295 dest11->invalidateSwizzleCache();
2296
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002297 return true;
2298 }
2299
2300 return false;
2301}
2302
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002303bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002304 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002305{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002306 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002307 if (!colorbuffer)
2308 {
2309 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002310 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002311 }
2312
2313 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2314 if (!sourceRenderTarget)
2315 {
2316 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002317 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002318 }
2319
2320 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2321 if (!source)
2322 {
2323 ERR("Failed to retrieve the render target view from the render target.");
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 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2328 if (!storage11)
2329 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002330 ERR("Failed to retrieve the texture storage from the destination.");
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 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2335 if (!destRenderTarget)
2336 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002337 ERR("Failed to retrieve the render target from the destination storage.");
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 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2342 if (!dest)
2343 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002344 ERR("Failed to retrieve the render target view from the destination render target.");
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
Geoff Langb86b9792013-06-04 16:32:05 -04002348 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2349 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002350
Geoff Langb86b9792013-06-04 16:32:05 -04002351 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2352 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002353
Geoff Langb86b9792013-06-04 16:32:05 -04002354 // Use nearest filtering because source and destination are the same size for the direct
2355 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002356 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002357 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002358
Geoff Lang42477a42013-09-17 17:07:02 -04002359 storage11->invalidateSwizzleCacheLevel(level);
2360
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002361 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002362}
2363
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002364bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002365 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002366{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002367 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002368 if (!colorbuffer)
2369 {
2370 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002371 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002372 }
2373
2374 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2375 if (!sourceRenderTarget)
2376 {
2377 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002378 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002379 }
2380
2381 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2382 if (!source)
2383 {
2384 ERR("Failed to retrieve the render target view from the render target.");
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 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2389 if (!storage11)
2390 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002391 ERR("Failed to retrieve the texture storage from the destination.");
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
Nicolas Capensb13f8662013-06-04 13:30:19 -04002395 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002396 if (!destRenderTarget)
2397 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002398 ERR("Failed to retrieve the render target from the destination storage.");
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 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2403 if (!dest)
2404 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002405 ERR("Failed to retrieve the render target view from the destination render target.");
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
Geoff Langb86b9792013-06-04 16:32:05 -04002409 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2410 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002411
Geoff Langb86b9792013-06-04 16:32:05 -04002412 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2413 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002414
Geoff Langb86b9792013-06-04 16:32:05 -04002415 // Use nearest filtering because source and destination are the same size for the direct
2416 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002417 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002418 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002419
Geoff Lang42477a42013-09-17 17:07:02 -04002420 storage11->invalidateSwizzleCacheLevel(level);
2421
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002422 return ret;
2423}
2424
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002425bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2426 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2427{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002428 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002429 if (!colorbuffer)
2430 {
2431 ERR("Failed to retrieve the color buffer from the frame buffer.");
2432 return gl::error(GL_OUT_OF_MEMORY, false);
2433 }
2434
2435 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2436 if (!sourceRenderTarget)
2437 {
2438 ERR("Failed to retrieve the render target from the frame buffer.");
2439 return gl::error(GL_OUT_OF_MEMORY, false);
2440 }
2441
2442 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2443 if (!source)
2444 {
2445 ERR("Failed to retrieve the render target view from the render target.");
2446 return gl::error(GL_OUT_OF_MEMORY, false);
2447 }
2448
2449 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2450 if (!storage11)
2451 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002452 ERR("Failed to retrieve the texture storage from the destination.");
2453 return gl::error(GL_OUT_OF_MEMORY, false);
2454 }
2455
2456 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2457 if (!destRenderTarget)
2458 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002459 ERR("Failed to retrieve the render target from the destination storage.");
2460 return gl::error(GL_OUT_OF_MEMORY, false);
2461 }
2462
2463 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2464 if (!dest)
2465 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002466 ERR("Failed to retrieve the render target view from the destination render target.");
2467 return gl::error(GL_OUT_OF_MEMORY, false);
2468 }
2469
Geoff Langb86b9792013-06-04 16:32:05 -04002470 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2471 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002472
Geoff Langb86b9792013-06-04 16:32:05 -04002473 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2474 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002475
Geoff Langb86b9792013-06-04 16:32:05 -04002476 // Use nearest filtering because source and destination are the same size for the direct
2477 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002478 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002479 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002480
Geoff Lang42477a42013-09-17 17:07:02 -04002481 storage11->invalidateSwizzleCacheLevel(level);
2482
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002483 return ret;
2484}
2485
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002486bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2487 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2488{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002489 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002490 if (!colorbuffer)
2491 {
2492 ERR("Failed to retrieve the color buffer from the frame buffer.");
2493 return gl::error(GL_OUT_OF_MEMORY, false);
2494 }
2495
2496 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2497 if (!sourceRenderTarget)
2498 {
2499 ERR("Failed to retrieve the render target from the frame buffer.");
2500 return gl::error(GL_OUT_OF_MEMORY, false);
2501 }
2502
2503 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2504 if (!source)
2505 {
2506 ERR("Failed to retrieve the render target view from the render target.");
2507 return gl::error(GL_OUT_OF_MEMORY, false);
2508 }
2509
2510 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2511 if (!storage11)
2512 {
Geoff Langea228632013-07-30 15:17:12 -04002513 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002514 ERR("Failed to retrieve the texture storage from the destination.");
2515 return gl::error(GL_OUT_OF_MEMORY, false);
2516 }
2517
2518 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2519 if (!destRenderTarget)
2520 {
Geoff Langea228632013-07-30 15:17:12 -04002521 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002522 ERR("Failed to retrieve the render target from the destination storage.");
2523 return gl::error(GL_OUT_OF_MEMORY, false);
2524 }
2525
2526 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2527 if (!dest)
2528 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002529 ERR("Failed to retrieve the render target view from the destination render target.");
2530 return gl::error(GL_OUT_OF_MEMORY, false);
2531 }
2532
Geoff Langb86b9792013-06-04 16:32:05 -04002533 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2534 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002535
Geoff Langb86b9792013-06-04 16:32:05 -04002536 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2537 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002538
Geoff Langb86b9792013-06-04 16:32:05 -04002539 // Use nearest filtering because source and destination are the same size for the direct
2540 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002541 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002542 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002543
Geoff Lang42477a42013-09-17 17:07:02 -04002544 storage11->invalidateSwizzleCacheLevel(level);
2545
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002546 return ret;
2547}
2548
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002549void Renderer11::unapplyRenderTargets()
2550{
2551 setOneTimeRenderTarget(NULL);
2552}
2553
2554void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2555{
2556 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2557
2558 rtvArray[0] = renderTargetView;
2559
2560 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2561
2562 // Do not preserve the serial for this one-time-use render target
2563 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2564 {
2565 mAppliedRenderTargetSerials[rtIndex] = 0;
2566 }
2567}
2568
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002569RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2570{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002571 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002572 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002573
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002574 if (depth)
2575 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002576 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002577 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002578 swapChain11->getDepthStencilTexture(),
2579 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002580 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002581 }
2582 else
2583 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002584 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002585 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002586 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002587 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002588 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002589 }
2590 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002591}
2592
Geoff Langa2d97f12013-06-11 11:44:02 -04002593RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002594{
Geoff Langa2d97f12013-06-11 11:44:02 -04002595 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002596 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002597}
2598
Geoff Lang48dcae72014-02-05 16:28:24 -05002599ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type,
2600 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2601 bool separatedOutputBuffers)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002602{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002603 ShaderExecutable11 *executable = NULL;
Geoff Lang48dcae72014-02-05 16:28:24 -05002604 HRESULT result;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002605
2606 switch (type)
2607 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002608 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002609 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002610 ID3D11VertexShader *vertexShader = NULL;
2611 ID3D11GeometryShader *streamOutShader = NULL;
2612
2613 result = mDevice->CreateVertexShader(function, length, NULL, &vertexShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002614 ASSERT(SUCCEEDED(result));
2615
Geoff Lang48dcae72014-02-05 16:28:24 -05002616 if (transformFeedbackVaryings.size() > 0)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002617 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002618 std::vector<D3D11_SO_DECLARATION_ENTRY> soDeclaration;
2619 for (size_t i = 0; i < transformFeedbackVaryings.size(); i++)
2620 {
2621 const gl::LinkedVarying &varying = transformFeedbackVaryings[i];
Jamie Madill6195ef82014-06-18 10:09:43 -04002622 GLenum transposedType = gl::TransposeMatrixType(varying.type);
2623
Geoff Langcebb5aa2014-04-07 14:13:40 -04002624 for (size_t j = 0; j < varying.semanticIndexCount; j++)
Geoff Lang48dcae72014-02-05 16:28:24 -05002625 {
2626 D3D11_SO_DECLARATION_ENTRY entry = { 0 };
2627 entry.Stream = 0;
Geoff Langcebb5aa2014-04-07 14:13:40 -04002628 entry.SemanticName = varying.semanticName.c_str();
2629 entry.SemanticIndex = varying.semanticIndex + j;
Geoff Lang48dcae72014-02-05 16:28:24 -05002630 entry.StartComponent = 0;
Jamie Madill6195ef82014-06-18 10:09:43 -04002631 entry.ComponentCount = gl::VariableColumnCount(transposedType);
Geoff Lang48dcae72014-02-05 16:28:24 -05002632 entry.OutputSlot = (separatedOutputBuffers ? i : 0);
2633 soDeclaration.push_back(entry);
2634 }
2635 }
2636
2637 result = mDevice->CreateGeometryShaderWithStreamOutput(function, length, soDeclaration.data(), soDeclaration.size(),
2638 NULL, 0, 0, NULL, &streamOutShader);
2639 ASSERT(SUCCEEDED(result));
2640 }
2641
2642 if (vertexShader)
2643 {
2644 executable = new ShaderExecutable11(function, length, vertexShader, streamOutShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002645 }
2646 }
2647 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002648 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002649 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002650 ID3D11PixelShader *pixelShader = NULL;
2651
2652 result = mDevice->CreatePixelShader(function, length, NULL, &pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002653 ASSERT(SUCCEEDED(result));
2654
Geoff Lang48dcae72014-02-05 16:28:24 -05002655 if (pixelShader)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002656 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002657 executable = new ShaderExecutable11(function, length, pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002658 }
2659 }
2660 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002661 case rx::SHADER_GEOMETRY:
2662 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002663 ID3D11GeometryShader *geometryShader = NULL;
2664
2665 result = mDevice->CreateGeometryShader(function, length, NULL, &geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002666 ASSERT(SUCCEEDED(result));
2667
Geoff Lang48dcae72014-02-05 16:28:24 -05002668 if (geometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002669 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002670 executable = new ShaderExecutable11(function, length, geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002671 }
2672 }
2673 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002674 default:
2675 UNREACHABLE();
2676 break;
2677 }
2678
2679 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002680}
2681
Geoff Lang48dcae72014-02-05 16:28:24 -05002682ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
2683 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2684 bool separatedOutputBuffers, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002685{
Geoff Lang6e05c272014-03-17 10:46:54 -07002686 const char *profileType = NULL;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002687 switch (type)
2688 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002689 case rx::SHADER_VERTEX:
Geoff Lang6e05c272014-03-17 10:46:54 -07002690 profileType = "vs";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002691 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002692 case rx::SHADER_PIXEL:
Geoff Lang6e05c272014-03-17 10:46:54 -07002693 profileType = "ps";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002694 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002695 case rx::SHADER_GEOMETRY:
Geoff Lang6e05c272014-03-17 10:46:54 -07002696 profileType = "gs";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002697 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002698 default:
2699 UNREACHABLE();
2700 return NULL;
2701 }
2702
Geoff Lang6e05c272014-03-17 10:46:54 -07002703 const char *profileVersion = NULL;
2704 switch (mFeatureLevel)
2705 {
2706 case D3D_FEATURE_LEVEL_11_0:
2707 profileVersion = "5_0";
2708 break;
2709 case D3D_FEATURE_LEVEL_10_1:
2710 profileVersion = "4_1";
2711 break;
2712 case D3D_FEATURE_LEVEL_10_0:
2713 profileVersion = "4_0";
2714 break;
2715 default:
2716 UNREACHABLE();
2717 return NULL;
2718 }
2719
2720 char profile[32];
2721 snprintf(profile, ArraySize(profile), "%s_%s", profileType, profileVersion);
2722
Nicolas Capens93faad92014-05-10 12:14:13 -04002723 UINT flags = D3DCOMPILE_OPTIMIZATION_LEVEL0;
2724
2725 if (gl::perfActive())
2726 {
2727#ifndef NDEBUG
2728 flags = D3DCOMPILE_SKIP_OPTIMIZATION;
2729#endif
2730
2731 flags |= D3DCOMPILE_DEBUG;
2732
2733 std::string sourcePath = getTempPath();
2734 std::string sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(shaderHLSL);
2735 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
2736 }
2737
2738 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
2739 // Try the default flags first and if compilation fails, try some alternatives.
2740 const UINT extraFlags[] =
2741 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002742 flags,
Nicolas Capens2c27db62014-05-10 12:21:11 -04002743 flags | D3DCOMPILE_SKIP_VALIDATION,
2744 flags | D3DCOMPILE_SKIP_OPTIMIZATION
Nicolas Capens93faad92014-05-10 12:14:13 -04002745 };
2746
2747 const static char *extraFlagNames[] =
2748 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002749 "default",
Nicolas Capens2c27db62014-05-10 12:21:11 -04002750 "skip validation",
2751 "skip optimization"
Nicolas Capens93faad92014-05-10 12:14:13 -04002752 };
2753
2754 int attempts = ArraySize(extraFlags);
2755
2756 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, extraFlags, extraFlagNames, attempts);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002757 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002758 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002759 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002760 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002761
Geoff Lang48dcae72014-02-05 16:28:24 -05002762 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type,
2763 transformFeedbackVaryings, separatedOutputBuffers);
Geoff Langea228632013-07-30 15:17:12 -04002764 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002765
2766 return executable;
2767}
2768
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002769rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2770{
2771 return new UniformStorage11(this, storageSize);
2772}
2773
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002774VertexBuffer *Renderer11::createVertexBuffer()
2775{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002776 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002777}
2778
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002779IndexBuffer *Renderer11::createIndexBuffer()
2780{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002781 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002782}
2783
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002784BufferStorage *Renderer11::createBufferStorage()
2785{
2786 return new BufferStorage11(this);
2787}
2788
Brandon Jones5bf98292014-06-06 17:19:38 -07002789VertexArrayImpl *Renderer11::createVertexArray()
2790{
2791 return new VertexArray11(this);
2792}
2793
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002794QueryImpl *Renderer11::createQuery(GLenum type)
2795{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002796 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002797}
2798
2799FenceImpl *Renderer11::createFence()
2800{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002801 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002802}
2803
Geoff Lang005df412013-10-16 14:12:50 -04002804bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002805{
Jamie Madill4461f092013-10-10 15:10:39 -04002806 int clientVersion = getCurrentClientVersion();
2807
2808 // We only support buffer to texture copies in ES3
2809 if (clientVersion <= 2)
2810 {
2811 return false;
2812 }
2813
2814 // sRGB formats do not work with D3D11 buffer SRVs
2815 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2816 {
2817 return false;
2818 }
2819
2820 // We cannot support direct copies to non-color-renderable formats
Geoff Langcec35902014-04-16 10:52:36 -04002821 if (!getCaps().textureCaps.get(internalFormat).colorRendering)
Jamie Madill4461f092013-10-10 15:10:39 -04002822 {
2823 return false;
2824 }
2825
2826 // We skip all 3-channel formats since sometimes format support is missing
2827 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2828 {
2829 return false;
2830 }
2831
2832 // We don't support formats which we can't represent without conversion
2833 if (getNativeTextureFormat(internalFormat) != internalFormat)
2834 {
2835 return false;
2836 }
2837
2838 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002839}
2840
Jamie Madilla21eea32013-09-18 14:36:25 -04002841bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2842 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2843{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002844 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002845 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2846}
2847
Jamie Madill3c7fa222014-06-05 13:08:51 -04002848bool Renderer11::getRenderTargetResource(gl::FramebufferAttachment *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002849{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002850 ASSERT(colorbuffer != NULL);
2851
2852 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2853 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002854 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002855 *subresourceIndex = renderTarget->getSubresourceIndex();
2856
2857 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2858 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002859 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002860 ID3D11Resource *textureResource = NULL;
2861 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002862
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002863 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002864 {
Geoff Lang8e328842014-02-10 13:11:20 -05002865 HRESULT result = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002866 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002867
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002868 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002869 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002870 return true;
2871 }
2872 else
2873 {
2874 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2875 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002876 }
2877 }
2878 }
2879 }
2880
2881 return false;
2882}
2883
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002884bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002885 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002886{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002887 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002888 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002889 gl::FramebufferAttachment *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002890
2891 if (!readBuffer)
2892 {
2893 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2894 return gl::error(GL_OUT_OF_MEMORY, false);
2895 }
2896
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002897 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002898
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002899 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002900 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002901 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2902 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002903 gl::FramebufferAttachment *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002904
2905 if (!drawBuffer)
2906 {
2907 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2908 return gl::error(GL_OUT_OF_MEMORY, false);
2909 }
2910
2911 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2912
Geoff Lang125deab2013-08-09 13:34:16 -04002913 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002914 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002915 {
2916 return false;
2917 }
2918 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002919 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002920 }
2921
Geoff Lang685806d2013-06-12 11:16:36 -04002922 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002923 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002924 gl::FramebufferAttachment *readBuffer = readTarget->getDepthOrStencilbuffer();
2925 gl::FramebufferAttachment *drawBuffer = drawTarget->getDepthOrStencilbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002926
2927 if (!readBuffer)
2928 {
2929 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2930 return gl::error(GL_OUT_OF_MEMORY, false);
2931 }
2932
2933 if (!drawBuffer)
2934 {
2935 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2936 return gl::error(GL_OUT_OF_MEMORY, false);
2937 }
2938
2939 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2940 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2941
Geoff Lang125deab2013-08-09 13:34:16 -04002942 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002943 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002944 {
2945 return false;
2946 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002947 }
2948
Geoff Lang42477a42013-09-17 17:07:02 -04002949 invalidateFramebufferSwizzles(drawTarget);
2950
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002951 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002952}
2953
Jamie Madilleb9baab2014-03-24 13:19:43 -04002954void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
2955 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002956{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002957 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002958 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002959
Jamie Madill3c7fa222014-06-05 13:08:51 -04002960 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002961
2962 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002963 {
2964 gl::Rectangle area;
2965 area.x = x;
2966 area.y = y;
2967 area.width = width;
2968 area.height = height;
2969
Jamie Madill1ef6fe72014-05-01 14:51:05 -04002970 if (pack.pixelBuffer.get() != NULL)
2971 {
2972 rx::BufferStorage11 *packBufferStorage = BufferStorage11::makeBufferStorage11(pack.pixelBuffer.get()->getStorage());
2973 PackPixelsParams packParams(area, format, type, outputPitch, pack, reinterpret_cast<ptrdiff_t>(pixels));
2974 packBufferStorage->packPixels(colorBufferTexture, subresourceIndex, packParams);
2975 }
2976 else
2977 {
2978 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch, pack, pixels);
2979 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002980
Geoff Langea228632013-07-30 15:17:12 -04002981 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002982 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002983}
2984
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002985Image *Renderer11::createImage()
2986{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002987 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002988}
2989
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002990void Renderer11::generateMipmap(Image *dest, Image *src)
2991{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002992 Image11 *dest11 = Image11::makeImage11(dest);
2993 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04002994 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002995}
2996
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002997TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
2998{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002999 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3000 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003001}
3002
Nicolas Capensbf712d02014-03-31 14:23:35 -04003003TextureStorage *Renderer11::createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003004{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003005 return new TextureStorage11_2D(this, internalformat, renderTarget, width, height, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003006}
3007
Nicolas Capensbf712d02014-03-31 14:23:35 -04003008TextureStorage *Renderer11::createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003009{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003010 return new TextureStorage11_Cube(this, internalformat, renderTarget, size, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003011}
3012
Nicolas Capensbf712d02014-03-31 14:23:35 -04003013TextureStorage *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 +00003014{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003015 return new TextureStorage11_3D(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003016}
3017
Nicolas Capensbf712d02014-03-31 14:23:35 -04003018TextureStorage *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 +00003019{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003020 return new TextureStorage11_2DArray(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003021}
3022
Jamie Madilleb9baab2014-03-24 13:19:43 -04003023void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format,
3024 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void *pixels)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003025{
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003026 ASSERT(area.width >= 0);
3027 ASSERT(area.height >= 0);
3028
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003029 D3D11_TEXTURE2D_DESC textureDesc;
3030 texture->GetDesc(&textureDesc);
3031
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003032 // Clamp read region to the defined texture boundaries, preventing out of bounds reads
3033 // and reads of uninitialized data.
3034 gl::Rectangle safeArea;
3035 safeArea.x = gl::clamp(area.x, 0, static_cast<int>(textureDesc.Width));
3036 safeArea.y = gl::clamp(area.y, 0, static_cast<int>(textureDesc.Height));
3037 safeArea.width = gl::clamp(area.width + std::min(area.x, 0), 0,
3038 static_cast<int>(textureDesc.Width) - safeArea.x);
3039 safeArea.height = gl::clamp(area.height + std::min(area.y, 0), 0,
3040 static_cast<int>(textureDesc.Height) - safeArea.y);
3041
3042 ASSERT(safeArea.x >= 0 && safeArea.y >= 0);
3043 ASSERT(safeArea.x + safeArea.width <= static_cast<int>(textureDesc.Width));
3044 ASSERT(safeArea.y + safeArea.height <= static_cast<int>(textureDesc.Height));
3045
3046 if (safeArea.width == 0 || safeArea.height == 0)
3047 {
3048 // no work to do
3049 return;
3050 }
3051
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003052 D3D11_TEXTURE2D_DESC stagingDesc;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003053 stagingDesc.Width = safeArea.width;
3054 stagingDesc.Height = safeArea.height;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003055 stagingDesc.MipLevels = 1;
3056 stagingDesc.ArraySize = 1;
3057 stagingDesc.Format = textureDesc.Format;
3058 stagingDesc.SampleDesc.Count = 1;
3059 stagingDesc.SampleDesc.Quality = 0;
3060 stagingDesc.Usage = D3D11_USAGE_STAGING;
3061 stagingDesc.BindFlags = 0;
3062 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3063 stagingDesc.MiscFlags = 0;
3064
3065 ID3D11Texture2D* stagingTex = NULL;
3066 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3067 if (FAILED(result))
3068 {
3069 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3070 return;
3071 }
3072
3073 ID3D11Texture2D* srcTex = NULL;
3074 if (textureDesc.SampleDesc.Count > 1)
3075 {
3076 D3D11_TEXTURE2D_DESC resolveDesc;
3077 resolveDesc.Width = textureDesc.Width;
3078 resolveDesc.Height = textureDesc.Height;
3079 resolveDesc.MipLevels = 1;
3080 resolveDesc.ArraySize = 1;
3081 resolveDesc.Format = textureDesc.Format;
3082 resolveDesc.SampleDesc.Count = 1;
3083 resolveDesc.SampleDesc.Quality = 0;
3084 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3085 resolveDesc.BindFlags = 0;
3086 resolveDesc.CPUAccessFlags = 0;
3087 resolveDesc.MiscFlags = 0;
3088
3089 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3090 if (FAILED(result))
3091 {
3092 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003093 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003094 return;
3095 }
3096
3097 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3098 subResource = 0;
3099 }
3100 else
3101 {
3102 srcTex = texture;
3103 srcTex->AddRef();
3104 }
3105
3106 D3D11_BOX srcBox;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003107 srcBox.left = static_cast<UINT>(safeArea.x);
3108 srcBox.right = static_cast<UINT>(safeArea.x + safeArea.width);
3109 srcBox.top = static_cast<UINT>(safeArea.y);
3110 srcBox.bottom = static_cast<UINT>(safeArea.y + safeArea.height);
3111 srcBox.front = 0;
3112 srcBox.back = 1;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003113
3114 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3115
Geoff Langea228632013-07-30 15:17:12 -04003116 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003117
Jamie Madill7538f7f2014-04-17 11:53:39 -04003118 PackPixelsParams packParams(safeArea, format, type, outputPitch, pack, 0);
3119 packPixels(stagingTex, packParams, pixels);
3120
3121 SafeRelease(stagingTex);
3122}
3123
3124void Renderer11::packPixels(ID3D11Texture2D *readTexture, const PackPixelsParams &params, void *pixelsOut)
3125{
3126 D3D11_TEXTURE2D_DESC textureDesc;
3127 readTexture->GetDesc(&textureDesc);
3128
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003129 D3D11_MAPPED_SUBRESOURCE mapping;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003130 HRESULT hr = mDeviceContext->Map(readTexture, 0, D3D11_MAP_READ, 0, &mapping);
Geoff Lang9cd19152014-05-28 15:54:34 -04003131 UNUSED_ASSERTION_VARIABLE(hr);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003132 ASSERT(SUCCEEDED(hr));
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003133
3134 unsigned char *source;
3135 int inputPitch;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003136 if (params.pack.reverseRowOrder)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003137 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003138 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (params.area.height - 1);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003139 inputPitch = -static_cast<int>(mapping.RowPitch);
3140 }
3141 else
3142 {
3143 source = static_cast<unsigned char*>(mapping.pData);
3144 inputPitch = static_cast<int>(mapping.RowPitch);
3145 }
3146
Geoff Lang697ad3e2013-06-04 10:11:28 -04003147 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003148
Geoff Lang005df412013-10-16 14:12:50 -04003149 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003150 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3151 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3152
3153 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3154
Jamie Madill7538f7f2014-04-17 11:53:39 -04003155 if (sourceFormat == params.format && sourceType == params.type)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003156 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003157 unsigned char *dest = static_cast<unsigned char*>(pixelsOut) + params.offset;
3158 for (int y = 0; y < params.area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003159 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003160 memcpy(dest + y * params.outputPitch, source + y * inputPitch, params.area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003161 }
3162 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003163 else
3164 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003165 GLenum destInternalFormat = gl::GetSizedInternalFormat(params.format, params.type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003166 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3167
Jamie Madill7538f7f2014-04-17 11:53:39 -04003168 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, params.format, params.type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003169 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003170 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003171 // Fast copy is possible through some special function
Jamie Madill7538f7f2014-04-17 11:53:39 -04003172 for (int y = 0; y < params.area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003173 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003174 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003175 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003176 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003177 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3178
3179 fastCopyFunc(src, dest);
3180 }
3181 }
3182 }
3183 else
3184 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003185 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003186 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(params.format, params.type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003187
3188 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3189 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3190 sizeof(temp) >= sizeof(gl::ColorUI) &&
3191 sizeof(temp) >= sizeof(gl::ColorI));
3192
Jamie Madill7538f7f2014-04-17 11:53:39 -04003193 for (int y = 0; y < params.area.height; y++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003194 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003195 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003196 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003197 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003198 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3199
3200 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3201 // will not allow the copy otherwise.
3202 readFunc(src, temp);
3203 writeFunc(temp, dest);
3204 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003205 }
3206 }
3207 }
3208
Jamie Madill7538f7f2014-04-17 11:53:39 -04003209 mDeviceContext->Unmap(readTexture, 0);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003210}
3211
Geoff Lang758d5b22013-06-11 11:42:50 -04003212bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003213 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3214 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003215{
Geoff Lang975af372013-06-12 11:19:22 -04003216 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3217 // it should never be the case that both color and depth/stencil need to be blitted at
3218 // at the same time.
3219 ASSERT(colorBlit != (depthBlit || stencilBlit));
3220
Geoff Langc1f51be2013-06-11 11:49:14 -04003221 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003222
Geoff Lang4d782732013-07-22 10:44:18 -04003223 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3224 if (!drawRenderTarget)
3225 {
3226 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3227 return gl::error(GL_OUT_OF_MEMORY, false);
3228 }
3229
3230 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3231 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3232 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3233 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3234
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003235 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3236 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003237 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003238 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003239 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003240 }
3241
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003242 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003243 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003244 unsigned int readSubresource = 0;
3245 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003246 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003247 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3248 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003249
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003250 if (unresolvedTexture)
3251 {
3252 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3253 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003254
Geoff Langea228632013-07-30 15:17:12 -04003255 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003256
Geoff Lang5c9a29a2014-02-11 10:26:24 -05003257 HRESULT hresult = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3258 if (FAILED(hresult))
Geoff Langc1f51be2013-06-11 11:49:14 -04003259 {
Geoff Langea228632013-07-30 15:17:12 -04003260 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003261 return gl::error(GL_OUT_OF_MEMORY, false);
3262 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003263 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003264 }
3265 else
3266 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003267 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003268 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003269 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003270 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003271 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003272 }
3273
Geoff Lang4d782732013-07-22 10:44:18 -04003274 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003275 {
Geoff Lang4d782732013-07-22 10:44:18 -04003276 SafeRelease(readTexture);
3277 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003278 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003279 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003280 }
3281
Geoff Lang125deab2013-08-09 13:34:16 -04003282 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3283 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3284
3285 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3286
3287 bool wholeBufferCopy = !scissorNeeded &&
3288 readRect.x == 0 && readRect.width == readSize.width &&
3289 readRect.y == 0 && readRect.height == readSize.height &&
3290 drawRect.x == 0 && drawRect.width == drawSize.width &&
3291 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003292
Geoff Langc1f51be2013-06-11 11:49:14 -04003293 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003294
Geoff Lang1cd1b212014-02-11 09:42:27 -05003295 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003296
3297 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3298 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3299 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3300 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3301
3302 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3303 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3304 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3305
Geoff Langc1f51be2013-06-11 11:49:14 -04003306 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003307 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3308 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003309 {
Geoff Lang125deab2013-08-09 13:34:16 -04003310 UINT dstX = drawRect.x;
3311 UINT dstY = drawRect.y;
3312
Geoff Langc1f51be2013-06-11 11:49:14 -04003313 D3D11_BOX readBox;
3314 readBox.left = readRect.x;
3315 readBox.right = readRect.x + readRect.width;
3316 readBox.top = readRect.y;
3317 readBox.bottom = readRect.y + readRect.height;
3318 readBox.front = 0;
3319 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003320
Geoff Lang125deab2013-08-09 13:34:16 -04003321 if (scissorNeeded)
3322 {
3323 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3324 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3325
3326 if (drawRect.x < scissor->x)
3327 {
3328 dstX = scissor->x;
3329 readBox.left += (scissor->x - drawRect.x);
3330 }
3331 if (drawRect.y < scissor->y)
3332 {
3333 dstY = scissor->y;
3334 readBox.top += (scissor->y - drawRect.y);
3335 }
3336 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3337 {
3338 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3339 }
3340 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3341 {
3342 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3343 }
3344 }
3345
Geoff Langc1f51be2013-06-11 11:49:14 -04003346 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3347 // We also require complete framebuffer copies for depth-stencil blit.
3348 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003349
Geoff Lang125deab2013-08-09 13:34:16 -04003350 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003351 readTexture, readSubresource, pSrcBox);
3352 result = true;
3353 }
3354 else
3355 {
3356 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003357 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003358
Geoff Lang975af372013-06-12 11:19:22 -04003359 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003360 {
Geoff Lang975af372013-06-12 11:19:22 -04003361 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003362 drawTexture, drawSubresource, drawArea, drawSize,
3363 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003364 }
3365 else if (depthBlit)
3366 {
Geoff Lang125deab2013-08-09 13:34:16 -04003367 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3368 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003369 }
3370 else if (stencilBlit)
3371 {
3372 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003373 drawTexture, drawSubresource, drawArea, drawSize,
3374 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003375 }
3376 else
3377 {
Geoff Lang685806d2013-06-12 11:16:36 -04003378 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003379 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3380 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003381 }
3382 }
3383
3384 SafeRelease(readTexture);
3385 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003386
3387 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003388}
3389
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003390ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3391{
3392 D3D11_TEXTURE2D_DESC textureDesc;
3393 source->GetDesc(&textureDesc);
3394
3395 if (textureDesc.SampleDesc.Count > 1)
3396 {
3397 D3D11_TEXTURE2D_DESC resolveDesc;
3398 resolveDesc.Width = textureDesc.Width;
3399 resolveDesc.Height = textureDesc.Height;
3400 resolveDesc.MipLevels = 1;
3401 resolveDesc.ArraySize = 1;
3402 resolveDesc.Format = textureDesc.Format;
3403 resolveDesc.SampleDesc.Count = 1;
3404 resolveDesc.SampleDesc.Quality = 0;
3405 resolveDesc.Usage = textureDesc.Usage;
3406 resolveDesc.BindFlags = textureDesc.BindFlags;
3407 resolveDesc.CPUAccessFlags = 0;
3408 resolveDesc.MiscFlags = 0;
3409
3410 ID3D11Texture2D *resolveTexture = NULL;
3411 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3412 if (FAILED(result))
3413 {
3414 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3415 return NULL;
3416 }
3417
3418 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3419 return resolveTexture;
3420 }
3421 else
3422 {
3423 source->AddRef();
3424 return source;
3425 }
3426}
3427
Jamie Madill3c7fa222014-06-05 13:08:51 -04003428void Renderer11::invalidateFBOAttachmentSwizzles(gl::FramebufferAttachment *attachment, int mipLevel)
Geoff Lang42477a42013-09-17 17:07:02 -04003429{
Jamie Madill3c7fa222014-06-05 13:08:51 -04003430 ASSERT(attachment->isTexture());
3431 TextureStorage *texStorage = attachment->getTextureStorage();
Geoff Lang42477a42013-09-17 17:07:02 -04003432 if (texStorage)
3433 {
3434 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3435 if (!texStorage11)
3436 {
3437 ERR("texture storage pointer unexpectedly null.");
3438 return;
3439 }
3440
3441 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3442 }
3443}
3444
3445void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3446{
3447 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3448 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003449 gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(colorAttachment);
3450 if (attachment && attachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003451 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003452 invalidateFBOAttachmentSwizzles(attachment, framebuffer->getColorbufferMipLevel(colorAttachment));
Geoff Lang42477a42013-09-17 17:07:02 -04003453 }
3454 }
3455
Jamie Madill3c7fa222014-06-05 13:08:51 -04003456 gl::FramebufferAttachment *depthAttachment = framebuffer->getDepthbuffer();
3457 if (depthAttachment && depthAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003458 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003459 invalidateFBOAttachmentSwizzles(depthAttachment, framebuffer->getDepthbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003460 }
3461
Jamie Madill3c7fa222014-06-05 13:08:51 -04003462 gl::FramebufferAttachment *stencilAttachment = framebuffer->getStencilbuffer();
3463 if (stencilAttachment && stencilAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003464 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003465 invalidateFBOAttachmentSwizzles(stencilAttachment, framebuffer->getStencilbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003466 }
3467}
3468
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003469bool Renderer11::getLUID(LUID *adapterLuid) const
3470{
3471 adapterLuid->HighPart = 0;
3472 adapterLuid->LowPart = 0;
3473
3474 if (!mDxgiAdapter)
3475 {
3476 return false;
3477 }
3478
3479 DXGI_ADAPTER_DESC adapterDesc;
3480 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3481 {
3482 return false;
3483 }
3484
3485 *adapterLuid = adapterDesc.AdapterLuid;
3486 return true;
3487}
3488
Geoff Lang005df412013-10-16 14:12:50 -04003489GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003490{
3491 int clientVersion = getCurrentClientVersion();
3492 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3493}
3494
Jamie Madill95ffb862014-01-29 09:26:59 -05003495rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3496{
3497 return gl_d3d11::GetVertexConversionType(vertexFormat);
3498}
3499
3500GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3501{
3502 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3503}
3504
Geoff Lang61e49a52013-05-29 10:22:58 -04003505Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3506{
3507 MultisampleSupportInfo supportInfo = { 0 };
3508
3509 UINT formatSupport;
3510 HRESULT result;
3511
3512 result = mDevice->CheckFormatSupport(format, &formatSupport);
3513 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3514 {
3515 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3516 {
3517 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3518 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3519 {
3520 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3521 }
3522 else
3523 {
3524 supportInfo.qualityLevels[i - 1] = 0;
3525 }
3526 }
3527 }
3528
3529 return supportInfo;
3530}
3531
Geoff Langcec35902014-04-16 10:52:36 -04003532gl::Caps Renderer11::generateCaps() const
3533{
3534 return d3d11_gl::GenerateCaps(mDevice);
3535}
3536
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003537}