blob: 8b5727703e794f52f7cb7c303f7de8c9472895df [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002//
Geoff Langeeba6e12014-02-03 13:12:30 -05003// Copyright (c) 2012-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// Renderer11.cpp: Implements a back-end specific class for the D3D11 renderer.
9
daniel@transgaming.com5503fd02012-11-28 19:38:57 +000010#include "libGLESv2/main.h"
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +000011#include "common/utilities.h"
daniel@transgaming.com18adad02012-11-28 21:04:03 +000012#include "libGLESv2/Buffer.h"
daniel@transgaming.com53670042012-11-28 20:55:51 +000013#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com80fc3322012-11-28 21:02:13 +000014#include "libGLESv2/Framebuffer.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000015#include "libGLESv2/RenderBuffer.h"
Geoff Langd47e0fc2013-08-29 11:40:43 -040016#include "libGLESv2/renderer/d3d11/Renderer11.h"
17#include "libGLESv2/renderer/d3d11/RenderTarget11.h"
18#include "libGLESv2/renderer/d3d11/renderer11_utils.h"
19#include "libGLESv2/renderer/d3d11/formatutils11.h"
20#include "libGLESv2/renderer/d3d11/ShaderExecutable11.h"
21#include "libGLESv2/renderer/d3d11/SwapChain11.h"
22#include "libGLESv2/renderer/d3d11/Image11.h"
23#include "libGLESv2/renderer/d3d11/VertexBuffer11.h"
24#include "libGLESv2/renderer/d3d11/IndexBuffer11.h"
25#include "libGLESv2/renderer/d3d11/BufferStorage11.h"
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000026#include "libGLESv2/renderer/VertexDataManager.h"
27#include "libGLESv2/renderer/IndexDataManager.h"
Geoff Langd47e0fc2013-08-29 11:40:43 -040028#include "libGLESv2/renderer/d3d11/TextureStorage11.h"
29#include "libGLESv2/renderer/d3d11/Query11.h"
30#include "libGLESv2/renderer/d3d11/Fence11.h"
31#include "libGLESv2/renderer/d3d11/Blit11.h"
32#include "libGLESv2/renderer/d3d11/Clear11.h"
33#include "libGLESv2/renderer/d3d11/PixelTransfer11.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000034#include "libEGL/Display.h"
35
Geoff Lang94a90892014-02-18 17:14:19 -050036// Enable ANGLE_SKIP_DXGI_1_2_CHECK if there is not a possibility of using cross-process
37// HWNDs or the Windows 7 Platform Update (KB2670838) is expected to be installed.
38#ifndef ANGLE_SKIP_DXGI_1_2_CHECK
39#define ANGLE_SKIP_DXGI_1_2_CHECK 0
40#endif
41
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000042#ifdef _DEBUG
43// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
44// and conformance tests. to enable all warnings, remove this define.
45#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
46#endif
47
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000048namespace rx
49{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000050static const DXGI_FORMAT RenderTargetFormats[] =
51 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000052 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000053 DXGI_FORMAT_R8G8B8A8_UNORM
54 };
55
56static const DXGI_FORMAT DepthStencilFormats[] =
57 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000058 DXGI_FORMAT_UNKNOWN,
59 DXGI_FORMAT_D24_UNORM_S8_UINT,
60 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000061 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000062
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000063enum
64{
65 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
66};
67
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000068Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
69{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000070 mVertexDataManager = NULL;
71 mIndexDataManager = NULL;
72
daniel@transgaming.comc5114302012-12-20 21:11:36 +000073 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000074 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000075
Geoff Langb86b9792013-06-04 16:32:05 -040076 mBlit = NULL;
Jamie Madilla21eea32013-09-18 14:36:25 -040077 mPixelTransfer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000078
Geoff Langda507fe2013-08-20 12:01:42 -040079 mClear = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000080
daniel@transgaming.combdf787f2013-02-01 03:20:36 +000081 mSyncQuery = NULL;
82
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000083 mD3d11Module = NULL;
84 mDxgiModule = NULL;
85
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000086 mDeviceLost = false;
87
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +000088 mMaxSupportedSamples = 0;
89
daniel@transgaming.com25072f62012-11-28 19:31:32 +000090 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000091 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000092 mDxgiAdapter = NULL;
93 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +000094
95 mDriverConstantBufferVS = NULL;
96 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +000097
Jamie Madill6246dc82014-01-29 09:26:47 -050098 mAppliedVertexShader = NULL;
99 mAppliedGeometryShader = NULL;
Geoff Lang4c5c6bb2014-02-05 16:32:46 -0500100 mCurPointGeometryShader = NULL;
Jamie Madill6246dc82014-01-29 09:26:47 -0500101 mAppliedPixelShader = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000102}
103
104Renderer11::~Renderer11()
105{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000106 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000107}
108
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000109Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
110{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000111 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000112 return static_cast<rx::Renderer11*>(renderer);
113}
114
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000115#ifndef __d3d11_1_h__
116#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
117#endif
118
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000119EGLint Renderer11::initialize()
120{
Geoff Langdad5ed32014-02-10 12:59:17 -0500121 if (!mCompiler.initialize())
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000122 {
123 return EGL_NOT_INITIALIZED;
124 }
125
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000126 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
127 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000128
129 if (mD3d11Module == NULL || mDxgiModule == NULL)
130 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000131 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000132 return EGL_NOT_INITIALIZED;
133 }
134
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000135 // create the D3D11 device
136 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000137 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000138
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000139 if (D3D11CreateDevice == NULL)
140 {
141 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
142 return EGL_NOT_INITIALIZED;
143 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000144
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000145 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000146 {
147 D3D_FEATURE_LEVEL_11_0,
148 D3D_FEATURE_LEVEL_10_1,
149 D3D_FEATURE_LEVEL_10_0,
150 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000151
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000152 HRESULT result = S_OK;
153
154#ifdef _DEBUG
155 result = D3D11CreateDevice(NULL,
156 D3D_DRIVER_TYPE_HARDWARE,
157 NULL,
158 D3D11_CREATE_DEVICE_DEBUG,
159 featureLevels,
160 ArraySize(featureLevels),
161 D3D11_SDK_VERSION,
162 &mDevice,
163 &mFeatureLevel,
164 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000165
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000166 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000167 {
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000168 ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n");
169 }
170
171 if (!mDevice || FAILED(result))
172#endif
173 {
174 result = D3D11CreateDevice(NULL,
175 D3D_DRIVER_TYPE_HARDWARE,
176 NULL,
177 0,
178 featureLevels,
179 ArraySize(featureLevels),
180 D3D11_SDK_VERSION,
181 &mDevice,
182 &mFeatureLevel,
183 &mDeviceContext);
184
185 if (!mDevice || FAILED(result))
186 {
187 ERR("Could not create D3D11 device - aborting!\n");
188 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
189 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000190 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000191
Geoff Lang94a90892014-02-18 17:14:19 -0500192#if !ANGLE_SKIP_DXGI_1_2_CHECK
193 // In order to create a swap chain for an HWND owned by another process, DXGI 1.2 is required.
194 // The easiest way to check is to query for a IDXGIDevice2.
195 bool requireDXGI1_2 = false;
196 HWND hwnd = WindowFromDC(mDc);
197 if (hwnd)
198 {
199 DWORD currentProcessId = GetCurrentProcessId();
200 DWORD wndProcessId;
201 GetWindowThreadProcessId(hwnd, &wndProcessId);
202 requireDXGI1_2 = (currentProcessId != wndProcessId);
203 }
204 else
205 {
206 requireDXGI1_2 = true;
207 }
208
209 if (requireDXGI1_2)
210 {
211 IDXGIDevice2 *dxgiDevice2 = NULL;
212 result = mDevice->QueryInterface(__uuidof(IDXGIDevice2), (void**)&dxgiDevice2);
213 if (FAILED(result))
214 {
215 ERR("DXGI 1.2 required to present to HWNDs owned by another process.\n");
216 return EGL_NOT_INITIALIZED;
217 }
218 SafeRelease(dxgiDevice2);
219 }
220#endif
221
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000222 IDXGIDevice *dxgiDevice = NULL;
223 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
224
225 if (FAILED(result))
226 {
227 ERR("Could not query DXGI device - aborting!\n");
228 return EGL_NOT_INITIALIZED;
229 }
230
231 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
232
233 if (FAILED(result))
234 {
235 ERR("Could not retrieve DXGI adapter - aborting!\n");
236 return EGL_NOT_INITIALIZED;
237 }
238
Geoff Langea228632013-07-30 15:17:12 -0400239 SafeRelease(dxgiDevice);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000240
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000241 mDxgiAdapter->GetDesc(&mAdapterDescription);
242 memset(mDescription, 0, sizeof(mDescription));
243 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
244
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000245 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
246
247 if (!mDxgiFactory || FAILED(result))
248 {
249 ERR("Could not create DXGI factory - aborting!\n");
250 return EGL_NOT_INITIALIZED;
251 }
252
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000253 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
254#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
255 ID3D11InfoQueue *infoQueue;
256 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
257
258 if (SUCCEEDED(result))
259 {
260 D3D11_MESSAGE_ID hideMessages[] =
261 {
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000262 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000263 };
264
265 D3D11_INFO_QUEUE_FILTER filter = {0};
266 filter.DenyList.NumIDs = ArraySize(hideMessages);
267 filter.DenyList.pIDList = hideMessages;
268
269 infoQueue->AddStorageFilterEntries(&filter);
Geoff Langea228632013-07-30 15:17:12 -0400270 SafeRelease(infoQueue);
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000271 }
272#endif
273
Geoff Lang61e49a52013-05-29 10:22:58 -0400274 mMaxSupportedSamples = 0;
275
276 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
277 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000278 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400279 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
280 mMultisampleSupportMap.insert(std::make_pair(*i, support));
281 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000282 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000283
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000284 initializeDevice();
285
286 return EGL_SUCCESS;
287}
288
289// do any one-time device initialization
290// NOTE: this is also needed after a device lost/reset
291// to reset the scene status and ensure the default states are reset.
292void Renderer11::initializeDevice()
293{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000294 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000295 mInputLayoutCache.initialize(mDevice, mDeviceContext);
296
297 ASSERT(!mVertexDataManager && !mIndexDataManager);
298 mVertexDataManager = new VertexDataManager(this);
299 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000300
Geoff Langb86b9792013-06-04 16:32:05 -0400301 ASSERT(!mBlit);
302 mBlit = new Blit11(this);
303
Geoff Langda507fe2013-08-20 12:01:42 -0400304 ASSERT(!mClear);
305 mClear = new Clear11(this);
306
Jamie Madilla21eea32013-09-18 14:36:25 -0400307 ASSERT(!mPixelTransfer);
308 mPixelTransfer = new PixelTransfer11(this);
309
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000310 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000311}
312
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000313int Renderer11::generateConfigs(ConfigDesc **configDescList)
314{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000315 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
316 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000317 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
318 int numConfigs = 0;
319
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000320 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000321 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000322 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000323 {
324 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
325
326 UINT formatSupport = 0;
327 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000328
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000329 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
330 {
331 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
332
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000333 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000334
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000335 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
336 {
Geoff Lang5c9a29a2014-02-11 10:26:24 -0500337 UINT depthStencilSupport = 0;
338 result = mDevice->CheckFormatSupport(depthStencilFormat, &depthStencilSupport);
339 depthStencilFormatOK = SUCCEEDED(result) && (depthStencilSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000340 }
341
342 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000343 {
Jamie Madilld6cb2442013-07-10 15:13:38 -0400344 // FIXME: parse types from context version
345 ASSERT(d3d11_gl::GetInternalFormat(renderTargetFormat, 2) == d3d11_gl::GetInternalFormat(renderTargetFormat, 3));
346 ASSERT(d3d11_gl::GetInternalFormat(depthStencilFormat, 2) == d3d11_gl::GetInternalFormat(depthStencilFormat, 3));
347
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000348 ConfigDesc newConfig;
Jamie Madilld6cb2442013-07-10 15:13:38 -0400349 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat, getCurrentClientVersion());
350 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat, getCurrentClientVersion());
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000351 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
352 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000353 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000354
355 (*configDescList)[numConfigs++] = newConfig;
356 }
357 }
358 }
359 }
360
361 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000362}
363
364void Renderer11::deleteConfigs(ConfigDesc *configDescList)
365{
366 delete [] (configDescList);
367}
368
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000369void Renderer11::sync(bool block)
370{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000371 if (block)
372 {
373 HRESULT result;
374
375 if (!mSyncQuery)
376 {
377 D3D11_QUERY_DESC queryDesc;
378 queryDesc.Query = D3D11_QUERY_EVENT;
379 queryDesc.MiscFlags = 0;
380
381 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
382 ASSERT(SUCCEEDED(result));
383 }
384
385 mDeviceContext->End(mSyncQuery);
386 mDeviceContext->Flush();
387
388 do
389 {
390 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
391
392 // Keep polling, but allow other threads to do something useful first
393 Sleep(0);
394
395 if (testDeviceLost(true))
396 {
397 return;
398 }
399 }
400 while (result == S_FALSE);
401 }
402 else
403 {
404 mDeviceContext->Flush();
405 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000406}
407
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000408SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
409{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000410 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000411}
412
Geoff Lange2e0ce02013-09-17 17:05:08 -0400413void Renderer11::generateSwizzle(gl::Texture *texture)
414{
Geoff Lang42477a42013-09-17 17:07:02 -0400415 if (texture)
416 {
417 TextureStorageInterface *texStorage = texture->getNativeTexture();
418 if (texStorage)
419 {
420 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
421
422 storage11->generateSwizzles(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
423 texture->getSwizzleAlpha());
424 }
425 }
Geoff Lange2e0ce02013-09-17 17:05:08 -0400426}
427
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000428void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
429{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000430 if (type == gl::SAMPLER_PIXEL)
431 {
432 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
433 {
434 ERR("Pixel shader sampler index %i is not valid.", index);
435 return;
436 }
437
438 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
439 {
440 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
441
442 if (!dxSamplerState)
443 {
444 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
445 "sampler state for pixel shaders at slot %i.", index);
446 }
447
448 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
449
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000450 mCurPixelSamplerStates[index] = samplerState;
451 }
452
453 mForceSetPixelSamplerStates[index] = false;
454 }
455 else if (type == gl::SAMPLER_VERTEX)
456 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000457 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000458 {
459 ERR("Vertex shader sampler index %i is not valid.", index);
460 return;
461 }
462
463 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
464 {
465 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
466
467 if (!dxSamplerState)
468 {
469 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
470 "sampler state for vertex shaders at slot %i.", index);
471 }
472
473 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
474
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000475 mCurVertexSamplerStates[index] = samplerState;
476 }
477
478 mForceSetVertexSamplerStates[index] = false;
479 }
480 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000481}
482
483void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
484{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000485 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000486 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000487
488 if (texture)
489 {
490 TextureStorageInterface *texStorage = texture->getNativeTexture();
491 if (texStorage)
492 {
493 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
Nicolas Capensa10c1c72014-03-27 11:20:52 -0400494 gl::SamplerState samplerState;
495 texture->getSamplerState(&samplerState);
496 textureSRV = storage11->getSRV(samplerState);
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000497 }
498
499 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
500 // missing the shader resource view
501 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000502
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000503 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000504 }
505
506 if (type == gl::SAMPLER_PIXEL)
507 {
508 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
509 {
510 ERR("Pixel shader sampler index %i is not valid.", index);
511 return;
512 }
513
Geoff Lang91382e52014-01-07 16:16:30 -0500514 if (forceSetTexture || mCurPixelSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000515 {
516 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
517 }
518
Geoff Lang91382e52014-01-07 16:16:30 -0500519 mCurPixelSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000520 }
521 else if (type == gl::SAMPLER_VERTEX)
522 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000523 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000524 {
525 ERR("Vertex shader sampler index %i is not valid.", index);
526 return;
527 }
528
Geoff Lang91382e52014-01-07 16:16:30 -0500529 if (forceSetTexture || mCurVertexSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000530 {
531 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
532 }
533
Geoff Lang91382e52014-01-07 16:16:30 -0500534 mCurVertexSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000535 }
536 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000537}
538
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000539bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
540{
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000541 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
542 {
543 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
544 if (uniformBuffer)
545 {
546 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500547 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000548
549 if (!constantBuffer)
550 {
551 return false;
552 }
553
Geoff Langc6354ee2013-07-22 10:40:07 -0400554 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
555 {
556 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
557 1, &constantBuffer);
558 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
559 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000560 }
561 }
562
563 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
564 {
565 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
566 if (uniformBuffer)
567 {
568 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500569 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000570
571 if (!constantBuffer)
572 {
573 return false;
574 }
575
Geoff Langc6354ee2013-07-22 10:40:07 -0400576 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
577 {
578 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
579 1, &constantBuffer);
580 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
581 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000582 }
583 }
584
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000585 return true;
586}
587
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000588void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000589{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000590 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000591 {
Nicolas Capensaea8e942014-05-09 19:14:08 -0400592 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000593 if (!dxRasterState)
594 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000595 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000596 "rasterizer state.");
597 }
598
599 mDeviceContext->RSSetState(dxRasterState);
600
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000601 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000602 }
603
604 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000605}
606
Geoff Langc142e9d2013-09-30 15:19:47 -0400607void Renderer11::setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000608 unsigned int sampleMask)
609{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000610 if (mForceSetBlendState ||
611 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400612 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000613 sampleMask != mCurSampleMask)
614 {
Geoff Langc142e9d2013-09-30 15:19:47 -0400615 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(framebuffer, blendState);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000616 if (!dxBlendState)
617 {
618 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
619 "blend state.");
620 }
621
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000622 float blendColors[4] = {0.0f};
623 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
624 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
625 {
626 blendColors[0] = blendColor.red;
627 blendColors[1] = blendColor.green;
628 blendColors[2] = blendColor.blue;
629 blendColors[3] = blendColor.alpha;
630 }
631 else
632 {
633 blendColors[0] = blendColor.alpha;
634 blendColors[1] = blendColor.alpha;
635 blendColors[2] = blendColor.alpha;
636 blendColors[3] = blendColor.alpha;
637 }
638
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000639 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
640
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000641 mCurBlendState = blendState;
642 mCurBlendColor = blendColor;
643 mCurSampleMask = sampleMask;
644 }
645
646 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000647}
648
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000649void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000650 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000651{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000652 if (mForceSetDepthStencilState ||
653 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
654 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
655 {
656 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
657 stencilRef != stencilBackRef ||
658 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
659 {
660 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
661 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000662 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000663 }
664
665 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
666 if (!dxDepthStencilState)
667 {
668 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
669 "setting the default depth stencil state.");
670 }
671
Jamie Madillec91cd32014-01-21 16:38:12 -0500672 // Max D3D11 stencil reference value is 0xFF, corresponding to the max 8 bits in a stencil buffer
673 // GL specifies we should clamp the ref value to the nearest bit depth when doing stencil ops
674 META_ASSERT(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF);
675 META_ASSERT(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF);
Geoff Lang0bf3a982014-02-11 09:40:48 -0500676 UINT dxStencilRef = std::min<UINT>(stencilRef, 0xFFu);
Jamie Madillec91cd32014-01-21 16:38:12 -0500677
Geoff Lang0bf3a982014-02-11 09:40:48 -0500678 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, dxStencilRef);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000679
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000680 mCurDepthStencilState = depthStencilState;
681 mCurStencilRef = stencilRef;
682 mCurStencilBackRef = stencilBackRef;
683 }
684
685 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000686}
687
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000688void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000689{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000690 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
691 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000692 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000693 if (enabled)
694 {
695 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000696 rect.left = std::max(0, scissor.x);
697 rect.top = std::max(0, scissor.y);
698 rect.right = scissor.x + std::max(0, scissor.width);
699 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000700
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000701 mDeviceContext->RSSetScissorRects(1, &rect);
702 }
703
704 if (enabled != mScissorEnabled)
705 {
706 mForceSetRasterState = true;
707 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000708
709 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000710 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000711 }
712
713 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000714}
715
daniel@transgaming.com12985182012-12-20 20:56:31 +0000716bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000717 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000718{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000719 gl::Rectangle actualViewport = viewport;
720 float actualZNear = gl::clamp01(zNear);
721 float actualZFar = gl::clamp01(zFar);
722 if (ignoreViewport)
723 {
724 actualViewport.x = 0;
725 actualViewport.y = 0;
726 actualViewport.width = mRenderTargetDesc.width;
727 actualViewport.height = mRenderTargetDesc.height;
728 actualZNear = 0.0f;
729 actualZFar = 1.0f;
730 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000731
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000732 // Get D3D viewport bounds, which depends on the feature level
733 const Range& viewportBounds = getViewportBounds();
734
735 // Clamp width and height first to the gl maximum, then clamp further if we extend past the D3D maximum bounds
daniel@transgaming.com53670042012-11-28 20:55:51 +0000736 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000737 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
738 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
739 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
740 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
741 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
742 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000743 dxViewport.MinDepth = actualZNear;
744 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000745
746 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
747 {
748 return false; // Nothing to render
749 }
750
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000751 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
752 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000753
daniel@transgaming.com53670042012-11-28 20:55:51 +0000754 if (viewportChanged)
755 {
756 mDeviceContext->RSSetViewports(1, &dxViewport);
757
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000758 mCurViewport = actualViewport;
759 mCurNear = actualZNear;
760 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000761
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000762 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
763 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
764 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
765 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000766
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000767 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
768 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000769
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000770 mVertexConstants.depthRange[0] = actualZNear;
771 mVertexConstants.depthRange[1] = actualZFar;
772 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000773
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000774 mPixelConstants.depthRange[0] = actualZNear;
775 mPixelConstants.depthRange[1] = actualZFar;
776 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000777 }
778
779 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000780 return true;
781}
782
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000783bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
784{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000785 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000786
Geoff Lang57e713e2013-07-31 17:01:58 -0400787 GLsizei minCount = 0;
788
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000789 switch (mode)
790 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400791 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
792 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
793 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
794 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
795 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
796 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000797 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400798 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000799 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000800 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000801 }
802
Geoff Lang4c095862013-07-22 10:43:36 -0400803 if (primitiveTopology != mCurrentPrimitiveTopology)
804 {
805 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
806 mCurrentPrimitiveTopology = primitiveTopology;
807 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000808
Geoff Lang57e713e2013-07-31 17:01:58 -0400809 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000810}
811
812bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000813{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000814 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000815 // Also extract the render target dimensions and view
816 unsigned int renderTargetWidth = 0;
817 unsigned int renderTargetHeight = 0;
818 GLenum renderTargetFormat = 0;
819 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
820 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
821 bool missingColorRenderTarget = true;
822
823 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000824 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000825 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
826
827 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000828 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000829 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
830 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
831
Jamie Madill3c7fa222014-06-05 13:08:51 -0400832 gl::FramebufferAttachment *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000833
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000834 if (!colorbuffer)
835 {
836 ERR("render target pointer unexpectedly null.");
837 return false;
838 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000839
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000840 // check for zero-sized default framebuffer, which is a special case.
841 // in this case we do not wish to modify any state and just silently return false.
842 // this will not report any gl error but will cause the calling method to return.
843 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
844 {
845 return false;
846 }
847
848 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
849
850 // Extract the render target dimensions and view
851 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
852 if (!renderTarget)
853 {
854 ERR("render target pointer unexpectedly null.");
855 return false;
856 }
857
858 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
859 if (!framebufferRTVs[colorAttachment])
860 {
861 ERR("render target view pointer unexpectedly null.");
862 return false;
863 }
864
865 if (missingColorRenderTarget)
866 {
867 renderTargetWidth = colorbuffer->getWidth();
868 renderTargetHeight = colorbuffer->getHeight();
869 renderTargetFormat = colorbuffer->getActualFormat();
870 missingColorRenderTarget = false;
871 }
Jamie Madillba597af2013-10-22 13:12:15 -0400872
Geoff Lang91382e52014-01-07 16:16:30 -0500873 // TODO: Detect if this color buffer is already bound as a texture and unbind it first to prevent
874 // D3D11 warnings.
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000875 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000876 }
877
878 // Get the depth stencil render buffer and serials
Jamie Madill3c7fa222014-06-05 13:08:51 -0400879 gl::FramebufferAttachment *depthStencil = NULL;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000880 unsigned int depthbufferSerial = 0;
881 unsigned int stencilbufferSerial = 0;
882 if (framebuffer->getDepthbufferType() != GL_NONE)
883 {
884 depthStencil = framebuffer->getDepthbuffer();
885 if (!depthStencil)
886 {
887 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000888 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000889 return false;
890 }
891
892 depthbufferSerial = depthStencil->getSerial();
893 }
894 else if (framebuffer->getStencilbufferType() != GL_NONE)
895 {
896 depthStencil = framebuffer->getStencilbuffer();
897 if (!depthStencil)
898 {
899 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000900 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000901 return false;
902 }
903
904 stencilbufferSerial = depthStencil->getSerial();
905 }
906
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000907 ID3D11DepthStencilView* framebufferDSV = NULL;
908 if (depthStencil)
909 {
910 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
911 if (!depthStencilRenderTarget)
912 {
913 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000914 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000915 return false;
916 }
917
918 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
919 if (!framebufferDSV)
920 {
921 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000922 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000923 return false;
924 }
925
926 // If there is no render buffer, the width, height and format values come from
927 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000928 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000929 {
930 renderTargetWidth = depthStencil->getWidth();
931 renderTargetHeight = depthStencil->getHeight();
932 renderTargetFormat = depthStencil->getActualFormat();
933 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000934 }
935
936 // Apply the render target and depth stencil
937 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000938 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000939 depthbufferSerial != mAppliedDepthbufferSerial ||
940 stencilbufferSerial != mAppliedStencilbufferSerial)
941 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000942 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000943
944 mRenderTargetDesc.width = renderTargetWidth;
945 mRenderTargetDesc.height = renderTargetHeight;
946 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000947 mForceSetViewport = true;
948 mForceSetScissor = true;
Geoff Langc142e9d2013-09-30 15:19:47 -0400949 mForceSetBlendState = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000950
Nicolas Capensaea8e942014-05-09 19:14:08 -0400951 if (!mDepthStencilInitialized)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000952 {
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000953 mForceSetRasterState = true;
954 }
955
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000956 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
957 {
958 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
959 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000960 mAppliedDepthbufferSerial = depthbufferSerial;
961 mAppliedStencilbufferSerial = stencilbufferSerial;
962 mRenderTargetDescInitialized = true;
963 mDepthStencilInitialized = true;
964 }
965
Geoff Lang42477a42013-09-17 17:07:02 -0400966 invalidateFramebufferSwizzles(framebuffer);
967
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000968 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000969}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000970
Jamie Madill57a89722013-07-02 11:57:03 -0400971GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -0400972 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000973{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000974 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -0400975 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000976 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000977 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000978 return err;
979 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000980
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000981 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000982}
983
daniel@transgaming.com31240482012-11-28 21:06:41 +0000984GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000985{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000986 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000987
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000988 if (err == GL_NO_ERROR)
989 {
Geoff Lang7840b172014-03-13 11:20:44 -0400990 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
991
992 ID3D11Buffer *buffer = NULL;
993 DXGI_FORMAT bufferFormat = indexBuffer->getIndexFormat();
994
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000995 if (indexInfo->storage)
996 {
Geoff Lang7840b172014-03-13 11:20:44 -0400997 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
998 buffer = storage->getBuffer(BUFFER_USAGE_INDEX);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000999 }
Geoff Lang7840b172014-03-13 11:20:44 -04001000 else
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001001 {
Geoff Lang7840b172014-03-13 11:20:44 -04001002 buffer = indexBuffer->getBuffer();
1003 }
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001004
Geoff Lang7840b172014-03-13 11:20:44 -04001005 if (buffer != mAppliedIB || bufferFormat != mAppliedIBFormat || indexInfo->startOffset != mAppliedIBOffset)
1006 {
1007 mDeviceContext->IASetIndexBuffer(buffer, bufferFormat, indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001008
Geoff Lang7840b172014-03-13 11:20:44 -04001009 mAppliedIB = buffer;
1010 mAppliedIBFormat = bufferFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001011 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001012 }
1013 }
1014
1015 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001016}
1017
Geoff Langeeba6e12014-02-03 13:12:30 -05001018void Renderer11::applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[])
1019{
1020 ID3D11Buffer* d3dBuffers[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1021 UINT d3dOffsets[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1022 bool requiresUpdate = false;
1023 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1024 {
1025 if (transformFeedbackBuffers[i])
1026 {
1027 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(transformFeedbackBuffers[i]->getStorage());
1028 ID3D11Buffer *buffer = storage->getBuffer(BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK);
1029
1030 d3dBuffers[i] = buffer;
1031 d3dOffsets[i] = (mAppliedTFBuffers[i] != buffer) ? static_cast<UINT>(offsets[i]) : -1;
1032 }
1033 else
1034 {
1035 d3dBuffers[i] = NULL;
1036 d3dOffsets[i] = 0;
1037 }
1038
1039 if (d3dBuffers[i] != mAppliedTFBuffers[i] || offsets[i] != mAppliedTFOffsets[i])
1040 {
1041 requiresUpdate = true;
1042 }
1043 }
1044
1045 if (requiresUpdate)
1046 {
1047 mDeviceContext->SOSetTargets(ArraySize(d3dBuffers), d3dBuffers, d3dOffsets);
1048 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1049 {
1050 mAppliedTFBuffers[i] = d3dBuffers[i];
1051 mAppliedTFOffsets[i] = offsets[i];
1052 }
1053 }
1054}
1055
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001056void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001057{
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001058 if (mode == GL_POINTS && transformFeedbackActive)
1059 {
1060 // Since point sprites are generated with a geometry shader, too many vertices will
1061 // be written if transform feedback is active. To work around this, draw only the points
1062 // with the stream out shader and no pixel shader to feed the stream out buffers and then
1063 // draw again with the point sprite geometry shader to rasterize the point sprites.
1064
1065 mDeviceContext->PSSetShader(NULL, NULL, 0);
1066
1067 if (instances > 0)
1068 {
1069 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1070 }
1071 else
1072 {
1073 mDeviceContext->Draw(count, 0);
1074 }
1075
1076 mDeviceContext->GSSetShader(mCurPointGeometryShader, NULL, 0);
1077 mDeviceContext->PSSetShader(mAppliedPixelShader, NULL, 0);
1078
1079 if (instances > 0)
1080 {
1081 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1082 }
1083 else
1084 {
1085 mDeviceContext->Draw(count, 0);
1086 }
1087
1088 mDeviceContext->GSSetShader(mAppliedGeometryShader, NULL, 0);
1089 }
1090 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001091 {
1092 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1093 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001094 else if (mode == GL_TRIANGLE_FAN)
1095 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001096 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001097 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001098 else if (instances > 0)
1099 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001100 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001101 }
1102 else
1103 {
1104 mDeviceContext->Draw(count, 0);
1105 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001106}
1107
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001108void Renderer11::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices,
1109 gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001110{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001111 if (mode == GL_LINE_LOOP)
1112 {
1113 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1114 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001115 else if (mode == GL_TRIANGLE_FAN)
1116 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001117 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1118 }
1119 else if (instances > 0)
1120 {
1121 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001122 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001123 else
1124 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001125 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001126 }
1127}
1128
1129void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1130{
1131 // Get the raw indices for an indexed draw
1132 if (type != GL_NONE && elementArrayBuffer)
1133 {
1134 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001135 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001136 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001137 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001138 }
1139
1140 if (!mLineLoopIB)
1141 {
1142 mLineLoopIB = new StreamingIndexBufferInterface(this);
1143 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1144 {
1145 delete mLineLoopIB;
1146 mLineLoopIB = NULL;
1147
1148 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001149 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001150 }
1151 }
1152
Geoff Lang57e713e2013-07-31 17:01:58 -04001153 // Checked by Renderer11::applyPrimitiveType
1154 ASSERT(count >= 0);
1155
1156 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001157 {
1158 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1159 return gl::error(GL_OUT_OF_MEMORY);
1160 }
1161
Geoff Lang57e713e2013-07-31 17:01:58 -04001162 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001163 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1164 {
1165 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001166 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001167 }
1168
1169 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001170 unsigned int offset;
1171 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001172 {
1173 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001174 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001175 }
1176
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001177 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001178 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001179
1180 switch (type)
1181 {
1182 case GL_NONE: // Non-indexed draw
1183 for (int i = 0; i < count; i++)
1184 {
1185 data[i] = i;
1186 }
1187 data[count] = 0;
1188 break;
1189 case GL_UNSIGNED_BYTE:
1190 for (int i = 0; i < count; i++)
1191 {
1192 data[i] = static_cast<const GLubyte*>(indices)[i];
1193 }
1194 data[count] = static_cast<const GLubyte*>(indices)[0];
1195 break;
1196 case GL_UNSIGNED_SHORT:
1197 for (int i = 0; i < count; i++)
1198 {
1199 data[i] = static_cast<const GLushort*>(indices)[i];
1200 }
1201 data[count] = static_cast<const GLushort*>(indices)[0];
1202 break;
1203 case GL_UNSIGNED_INT:
1204 for (int i = 0; i < count; i++)
1205 {
1206 data[i] = static_cast<const GLuint*>(indices)[i];
1207 }
1208 data[count] = static_cast<const GLuint*>(indices)[0];
1209 break;
1210 default: UNREACHABLE();
1211 }
1212
1213 if (!mLineLoopIB->unmapBuffer())
1214 {
1215 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001216 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001217 }
1218
Geoff Lang7840b172014-03-13 11:20:44 -04001219 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1220 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1221 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001222
Geoff Lang7840b172014-03-13 11:20:44 -04001223 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1224 {
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001225 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001226 mAppliedIB = d3dIndexBuffer;
1227 mAppliedIBFormat = indexFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001228 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001229 }
1230
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001231 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001232}
1233
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001234void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001235{
1236 // Get the raw indices for an indexed draw
1237 if (type != GL_NONE && elementArrayBuffer)
1238 {
1239 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001240 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001241 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001242 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001243 }
1244
1245 if (!mTriangleFanIB)
1246 {
1247 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1248 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1249 {
1250 delete mTriangleFanIB;
1251 mTriangleFanIB = NULL;
1252
1253 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001254 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001255 }
1256 }
1257
Geoff Lang57e713e2013-07-31 17:01:58 -04001258 // Checked by Renderer11::applyPrimitiveType
1259 ASSERT(count >= 3);
1260
Geoff Langeadfd572013-07-09 15:55:07 -04001261 const unsigned int numTris = count - 2;
1262
Geoff Lang57e713e2013-07-31 17:01:58 -04001263 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001264 {
1265 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1266 return gl::error(GL_OUT_OF_MEMORY);
1267 }
1268
1269 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001270 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1271 {
1272 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001273 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001274 }
1275
1276 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001277 unsigned int offset;
1278 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001279 {
1280 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001281 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001282 }
1283
1284 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001285 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001286
1287 switch (type)
1288 {
1289 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001290 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001291 {
1292 data[i*3 + 0] = 0;
1293 data[i*3 + 1] = i + 1;
1294 data[i*3 + 2] = i + 2;
1295 }
1296 break;
1297 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001298 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001299 {
1300 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1301 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1302 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1303 }
1304 break;
1305 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001306 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001307 {
1308 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1309 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1310 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1311 }
1312 break;
1313 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001314 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001315 {
1316 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1317 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1318 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1319 }
1320 break;
1321 default: UNREACHABLE();
1322 }
1323
1324 if (!mTriangleFanIB->unmapBuffer())
1325 {
1326 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001327 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001328 }
1329
Geoff Lang7840b172014-03-13 11:20:44 -04001330 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1331 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1332 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001333
Geoff Lang7840b172014-03-13 11:20:44 -04001334 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1335 {
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001336 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001337 mAppliedIB = d3dIndexBuffer;
1338 mAppliedIBFormat = indexFormat;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001339 mAppliedIBOffset = indexBufferOffset;
1340 }
1341
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001342 if (instances > 0)
1343 {
1344 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1345 }
1346 else
1347 {
1348 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1349 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001350}
1351
Geoff Lang04fb89a2014-06-09 15:05:36 -04001352void Renderer11::applyShaders(gl::ProgramBinary *programBinary, const gl::VertexFormat inputLayout[], const gl::Framebuffer *framebuffer,
1353 bool rasterizerDiscard, bool transformFeedbackActive)
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001354{
Jamie Madillc5a83002014-02-14 16:41:25 -05001355 ShaderExecutable *vertexExe = programBinary->getVertexExecutableForInputLayout(inputLayout);
Geoff Lang04fb89a2014-06-09 15:05:36 -04001356 ShaderExecutable *pixelExe = programBinary->getPixelExecutableForFramebuffer(framebuffer);
Jamie Madill6246dc82014-01-29 09:26:47 -05001357 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001358
Jamie Madill6246dc82014-01-29 09:26:47 -05001359 ID3D11VertexShader *vertexShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader() : NULL);
Jamie Madill6246dc82014-01-29 09:26:47 -05001360
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001361 ID3D11PixelShader *pixelShader = NULL;
1362 // Skip pixel shader if we're doing rasterizer discard.
1363 if (!rasterizerDiscard)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001364 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001365 pixelShader = (pixelExe ? ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader() : NULL);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001366 }
1367
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001368 ID3D11GeometryShader *geometryShader = NULL;
1369 if (transformFeedbackActive)
Geoff Lang0550d032014-01-30 11:29:07 -05001370 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001371 geometryShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getStreamOutShader() : NULL);
1372 }
1373 else if (mCurRasterState.pointDrawMode)
1374 {
1375 geometryShader = (geometryExe ? ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader() : NULL);
Geoff Lang0550d032014-01-30 11:29:07 -05001376 }
1377
Jamie Madill6246dc82014-01-29 09:26:47 -05001378 bool dirtyUniforms = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001379
Jamie Madill6246dc82014-01-29 09:26:47 -05001380 if (vertexShader != mAppliedVertexShader)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001381 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001382 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1383 mAppliedVertexShader = vertexShader;
1384 dirtyUniforms = true;
1385 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001386
Jamie Madill6246dc82014-01-29 09:26:47 -05001387 if (geometryShader != mAppliedGeometryShader)
1388 {
1389 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1390 mAppliedGeometryShader = geometryShader;
1391 dirtyUniforms = true;
1392 }
1393
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001394 if (geometryExe && mCurRasterState.pointDrawMode)
1395 {
1396 mCurPointGeometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
1397 }
1398 else
1399 {
1400 mCurPointGeometryShader = NULL;
1401 }
1402
Jamie Madill6246dc82014-01-29 09:26:47 -05001403 if (pixelShader != mAppliedPixelShader)
1404 {
1405 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1406 mAppliedPixelShader = pixelShader;
1407 dirtyUniforms = true;
1408 }
1409
1410 if (dirtyUniforms)
1411 {
1412 programBinary->dirtyAllUniforms();
daniel@transgaming.come4991412012-12-20 20:55:34 +00001413 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001414}
1415
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001416void Renderer11::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001417{
Jamie Madill834e8b72014-04-11 13:33:58 -04001418 const std::vector<gl::LinkedUniform*> &uniformArray = programBinary.getUniforms();
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001419
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001420 unsigned int totalRegisterCountVS = 0;
1421 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001422
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001423 bool vertexUniformsDirty = false;
1424 bool pixelUniformsDirty = false;
1425
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001426 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001427 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001428 const gl::LinkedUniform &uniform = *uniformArray[uniformIndex];
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001429
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001430 if (uniform.isReferencedByVertexShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001431 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001432 totalRegisterCountVS += uniform.registerCount;
1433 vertexUniformsDirty = (vertexUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001434 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001435
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001436 if (uniform.isReferencedByFragmentShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001437 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001438 totalRegisterCountPS += uniform.registerCount;
1439 pixelUniformsDirty = (pixelUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001440 }
1441 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001442
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001443 const UniformStorage11 *vertexUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getVertexUniformStorage());
1444 const UniformStorage11 *fragmentUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getFragmentUniformStorage());
1445 ASSERT(vertexUniformStorage);
1446 ASSERT(fragmentUniformStorage);
1447
1448 ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
1449 ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001450
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001451 float (*mapVS)[4] = NULL;
1452 float (*mapPS)[4] = NULL;
1453
Shannon Woods5ab33c82013-06-26 15:31:09 -04001454 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1455 {
1456 D3D11_MAPPED_SUBRESOURCE map = {0};
1457 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001458 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001459 ASSERT(SUCCEEDED(result));
1460 mapVS = (float(*)[4])map.pData;
1461 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001462
Shannon Woods5ab33c82013-06-26 15:31:09 -04001463 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1464 {
1465 D3D11_MAPPED_SUBRESOURCE map = {0};
1466 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001467 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001468 ASSERT(SUCCEEDED(result));
1469 mapPS = (float(*)[4])map.pData;
1470 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001471
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001472 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001473 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001474 gl::LinkedUniform *uniform = uniformArray[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001475
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001476 if (!uniform->isSampler())
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001477 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001478 unsigned int componentCount = (4 - uniform->registerElement);
1479
Jamie Madill71cc91f2013-09-18 12:51:22 -04001480 // 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 -04001481 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001482
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001483 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001484 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001485 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001486 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001487
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001488 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001489 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001490 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001491 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001492 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001493 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001494
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001495 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001496 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001497 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001498 }
1499
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001500 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001501 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001502 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001503 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001504
1505 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1506 {
1507 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1508 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1509 }
1510
1511 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1512 {
1513 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1514 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1515 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001516
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001517 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001518 if (!mDriverConstantBufferVS)
1519 {
1520 D3D11_BUFFER_DESC constantBufferDescription = {0};
1521 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1522 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1523 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1524 constantBufferDescription.CPUAccessFlags = 0;
1525 constantBufferDescription.MiscFlags = 0;
1526 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001527
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001528 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001529 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001530 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001531
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001532 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1533 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001534
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001535 if (!mDriverConstantBufferPS)
1536 {
1537 D3D11_BUFFER_DESC constantBufferDescription = {0};
1538 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1539 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1540 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1541 constantBufferDescription.CPUAccessFlags = 0;
1542 constantBufferDescription.MiscFlags = 0;
1543 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001544
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001545 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001546 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001547 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001548
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001549 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1550 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001551
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001552 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1553 {
1554 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1555 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1556 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001557
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001558 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1559 {
1560 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1561 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1562 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001563
1564 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001565 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1566 {
1567 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1568 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1569 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001570}
1571
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001572void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001573{
Geoff Langda507fe2013-08-20 12:01:42 -04001574 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001575 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001576}
1577
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001578void Renderer11::markAllStateDirty()
1579{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001580 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1581 {
1582 mAppliedRenderTargetSerials[rtIndex] = 0;
1583 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001584 mAppliedDepthbufferSerial = 0;
1585 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001586 mDepthStencilInitialized = false;
1587 mRenderTargetDescInitialized = false;
1588
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001589 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001590 {
1591 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001592 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001593 }
1594 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1595 {
1596 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001597 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001598 }
1599
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001600 mForceSetBlendState = true;
1601 mForceSetRasterState = true;
1602 mForceSetDepthStencilState = true;
1603 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001604 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001605
Geoff Lang7840b172014-03-13 11:20:44 -04001606 mAppliedIB = NULL;
1607 mAppliedIBFormat = DXGI_FORMAT_UNKNOWN;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001608 mAppliedIBOffset = 0;
1609
Jamie Madill6246dc82014-01-29 09:26:47 -05001610 mAppliedVertexShader = NULL;
1611 mAppliedGeometryShader = NULL;
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001612 mCurPointGeometryShader = NULL;
Jamie Madill6246dc82014-01-29 09:26:47 -05001613 mAppliedPixelShader = NULL;
Geoff Langeeba6e12014-02-03 13:12:30 -05001614
1615 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1616 {
1617 mAppliedTFBuffers[i] = NULL;
1618 mAppliedTFOffsets[i] = 0;
1619 }
1620
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001621 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1622 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001623
1624 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001625
1626 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1627 {
1628 mCurrentConstantBufferVS[i] = -1;
1629 mCurrentConstantBufferPS[i] = -1;
1630 }
1631
1632 mCurrentVertexConstantBuffer = NULL;
1633 mCurrentPixelConstantBuffer = NULL;
1634 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001635
1636 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001637}
1638
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001639void Renderer11::releaseDeviceResources()
1640{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001641 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001642 mInputLayoutCache.clear();
1643
Geoff Langea228632013-07-30 15:17:12 -04001644 SafeDelete(mVertexDataManager);
1645 SafeDelete(mIndexDataManager);
1646 SafeDelete(mLineLoopIB);
1647 SafeDelete(mTriangleFanIB);
1648 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001649 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001650 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001651
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001652 SafeRelease(mDriverConstantBufferVS);
1653 SafeRelease(mDriverConstantBufferPS);
1654 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001655}
1656
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001657void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001658{
1659 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001660 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001661}
1662
1663bool Renderer11::isDeviceLost()
1664{
1665 return mDeviceLost;
1666}
1667
1668// set notify to true to broadcast a message to all contexts of the device loss
1669bool Renderer11::testDeviceLost(bool notify)
1670{
1671 bool isLost = false;
1672
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001673 // GetRemovedReason is used to test if the device is removed
1674 HRESULT result = mDevice->GetDeviceRemovedReason();
1675 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001676
1677 if (isLost)
1678 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001679 // Log error if this is a new device lost event
1680 if (mDeviceLost == false)
1681 {
1682 ERR("The D3D11 device was removed: 0x%08X", result);
1683 }
1684
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001685 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001686 // we'll probably get this done again by notifyDeviceLost
1687 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001688 // Note that we don't want to clear the device loss status here
1689 // -- this needs to be done by resetDevice
1690 mDeviceLost = true;
1691 if (notify)
1692 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001693 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001694 }
1695 }
1696
1697 return isLost;
1698}
1699
1700bool Renderer11::testDeviceResettable()
1701{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001702 // determine if the device is resettable by creating a dummy device
1703 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001704
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001705 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001706 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001707 return false;
1708 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001709
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001710 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001711 {
1712 D3D_FEATURE_LEVEL_11_0,
1713 D3D_FEATURE_LEVEL_10_1,
1714 D3D_FEATURE_LEVEL_10_0,
1715 };
1716
1717 ID3D11Device* dummyDevice;
1718 D3D_FEATURE_LEVEL dummyFeatureLevel;
1719 ID3D11DeviceContext* dummyContext;
1720
1721 HRESULT result = D3D11CreateDevice(NULL,
1722 D3D_DRIVER_TYPE_HARDWARE,
1723 NULL,
1724 #if defined(_DEBUG)
1725 D3D11_CREATE_DEVICE_DEBUG,
1726 #else
1727 0,
1728 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001729 featureLevels,
1730 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001731 D3D11_SDK_VERSION,
1732 &dummyDevice,
1733 &dummyFeatureLevel,
1734 &dummyContext);
1735
1736 if (!mDevice || FAILED(result))
1737 {
1738 return false;
1739 }
1740
Geoff Langea228632013-07-30 15:17:12 -04001741 SafeRelease(dummyContext);
1742 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001743
1744 return true;
1745}
1746
1747void Renderer11::release()
1748{
1749 releaseDeviceResources();
1750
Geoff Langea228632013-07-30 15:17:12 -04001751 SafeRelease(mDxgiFactory);
1752 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001753
1754 if (mDeviceContext)
1755 {
1756 mDeviceContext->ClearState();
1757 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001758 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001759 }
1760
Geoff Langea228632013-07-30 15:17:12 -04001761 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001762
1763 if (mD3d11Module)
1764 {
1765 FreeLibrary(mD3d11Module);
1766 mD3d11Module = NULL;
1767 }
1768
1769 if (mDxgiModule)
1770 {
1771 FreeLibrary(mDxgiModule);
1772 mDxgiModule = NULL;
1773 }
Geoff Langdad5ed32014-02-10 12:59:17 -05001774
1775 mCompiler.release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001776}
1777
1778bool Renderer11::resetDevice()
1779{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001780 // recreate everything
1781 release();
1782 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001783
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001784 if (result != EGL_SUCCESS)
1785 {
1786 ERR("Could not reinitialize D3D11 device: %08X", result);
1787 return false;
1788 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001789
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001790 mDeviceLost = false;
1791
1792 return true;
1793}
1794
1795DWORD Renderer11::getAdapterVendor() const
1796{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001797 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001798}
1799
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001800std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001801{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001802 std::ostringstream rendererString;
1803
1804 rendererString << mDescription;
1805 rendererString << " Direct3D11";
1806
1807 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1808 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1809
1810 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001811}
1812
1813GUID Renderer11::getAdapterIdentifier() const
1814{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001815 // Use the adapter LUID as our adapter ID
1816 // This number is local to a machine is only guaranteed to be unique between restarts
1817 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1818 GUID adapterId = {0};
1819 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1820 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001821}
1822
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001823Range Renderer11::getViewportBounds() const
1824{
1825 switch (mFeatureLevel)
1826 {
1827 case D3D_FEATURE_LEVEL_11_0:
1828 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1829 case D3D_FEATURE_LEVEL_10_1:
1830 case D3D_FEATURE_LEVEL_10_0:
1831 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1832 default: UNREACHABLE();
1833 return Range(0, 0);
1834 }
1835}
1836
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001837unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001838{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001839 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1840 switch (mFeatureLevel)
1841 {
1842 case D3D_FEATURE_LEVEL_11_0:
1843 case D3D_FEATURE_LEVEL_10_1:
1844 case D3D_FEATURE_LEVEL_10_0:
1845 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1846 default: UNREACHABLE();
1847 return 0;
1848 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001849}
1850
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001851unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1852{
1853 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1854}
1855
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001856unsigned int Renderer11::getReservedVertexUniformVectors() const
1857{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001858 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001859}
1860
1861unsigned int Renderer11::getReservedFragmentUniformVectors() const
1862{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001863 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001864}
1865
1866unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001867{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001868 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1869 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1870 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001871}
1872
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001873unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001874{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001875 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1876 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1877 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001878}
1879
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001880unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001881{
1882 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001883 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1884 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001885 switch (mFeatureLevel)
1886 {
1887 case D3D_FEATURE_LEVEL_11_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001888 return D3D11_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001889 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001890 return D3D10_1_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001891 case D3D_FEATURE_LEVEL_10_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001892 return D3D10_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001893 default: UNREACHABLE();
1894 return 0;
1895 }
1896}
1897
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001898unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
1899{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001900 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1901 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1902
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001903 switch (mFeatureLevel)
1904 {
1905 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001906 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001907 case D3D_FEATURE_LEVEL_10_1:
1908 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001909 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001910 default: UNREACHABLE();
1911 return 0;
1912 }
1913}
1914
1915unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
1916{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001917 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1918 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1919
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001920 switch (mFeatureLevel)
1921 {
1922 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001923 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001924 case D3D_FEATURE_LEVEL_10_1:
1925 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001926 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001927 default: UNREACHABLE();
1928 return 0;
1929 }
1930}
1931
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001932unsigned int Renderer11::getReservedVertexUniformBuffers() const
1933{
1934 // we reserve one buffer for the application uniforms, and one for driver uniforms
1935 return 2;
1936}
1937
1938unsigned int Renderer11::getReservedFragmentUniformBuffers() const
1939{
1940 // we reserve one buffer for the application uniforms, and one for driver uniforms
1941 return 2;
1942}
1943
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001944unsigned int Renderer11::getReservedVaryings() const
1945{
1946 // We potentially reserve varyings for gl_Position, _dx_Position, gl_FragCoord and gl_PointSize
1947 return 4;
1948}
1949
1950
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001951unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
1952{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001953 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
1954 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
1955
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001956 switch (mFeatureLevel)
1957 {
1958 case D3D_FEATURE_LEVEL_11_0:
1959 return D3D11_SO_BUFFER_SLOT_COUNT;
1960 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001961 return D3D10_1_SO_BUFFER_SLOT_COUNT;
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001962 case D3D_FEATURE_LEVEL_10_0:
1963 return D3D10_SO_BUFFER_SLOT_COUNT;
1964 default: UNREACHABLE();
1965 return 0;
1966 }
1967}
1968
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001969unsigned int Renderer11::getMaxTransformFeedbackSeparateComponents() const
1970{
1971 switch (mFeatureLevel)
1972 {
1973 case D3D_FEATURE_LEVEL_11_0:
1974 return getMaxTransformFeedbackInterleavedComponents() / getMaxTransformFeedbackBuffers();
1975 case D3D_FEATURE_LEVEL_10_1:
1976 case D3D_FEATURE_LEVEL_10_0:
1977 // D3D 10 and 10.1 only allow one output per output slot if an output slot other than zero
1978 // is used.
1979 return 4;
1980 default: UNREACHABLE();
1981 return 0;
1982 }
1983}
1984
1985unsigned int Renderer11::getMaxTransformFeedbackInterleavedComponents() const
1986{
1987 return (getMaxVaryingVectors() * 4);
1988}
1989
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00001990unsigned int Renderer11::getMaxUniformBufferSize() const
1991{
1992 // Each component is a 4-element vector of 4-byte units (floats)
1993 const unsigned int bytesPerComponent = 4 * sizeof(float);
1994
1995 switch (mFeatureLevel)
1996 {
1997 case D3D_FEATURE_LEVEL_11_0:
1998 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1999 case D3D_FEATURE_LEVEL_10_1:
2000 case D3D_FEATURE_LEVEL_10_0:
2001 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2002 default: UNREACHABLE();
2003 return 0;
2004 }
2005}
2006
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002007bool Renderer11::getShareHandleSupport() const
2008{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002009 // We only currently support share handles with BGRA surfaces, because
2010 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002011 // PIX doesn't seem to support using share handles, so disable them.
Geoff Langcec35902014-04-16 10:52:36 -04002012 return getCaps().extensions.textureFormatBGRA8888 && !gl::perfActive();
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002013}
2014
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002015bool Renderer11::getPostSubBufferSupport() const
2016{
2017 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2018 return false;
2019}
2020
Jamie Madill13a2f852013-12-11 16:35:08 -05002021int Renderer11::getMaxRecommendedElementsIndices() const
2022{
2023 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2024 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2025
2026 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2027 return std::numeric_limits<GLint>::max();
2028}
2029
2030int Renderer11::getMaxRecommendedElementsVertices() const
2031{
2032 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2033 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2034
2035 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2036 return std::numeric_limits<GLint>::max();
2037}
2038
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002039int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002040{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002041 switch (mFeatureLevel)
2042 {
2043 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002044 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002045 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2046 default: UNREACHABLE(); return 0;
2047 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002048}
2049
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002050int Renderer11::getMinorShaderModel() const
2051{
2052 switch (mFeatureLevel)
2053 {
2054 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2055 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2056 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2057 default: UNREACHABLE(); return 0;
2058 }
2059}
2060
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002061float Renderer11::getMaxPointSize() const
2062{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002063 // choose a reasonable maximum. we enforce this in the shader.
2064 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2065 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002066}
2067
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002068int Renderer11::getMaxViewportDimension() const
2069{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002070 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2071 // In our case return the maximum texture size, which is the maximum render buffer size.
2072 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2073 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2074
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002075 switch (mFeatureLevel)
2076 {
2077 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002078 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002079 case D3D_FEATURE_LEVEL_10_1:
2080 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002081 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002082 default: UNREACHABLE();
2083 return 0;
2084 }
2085}
2086
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002087int Renderer11::getMaxTextureWidth() const
2088{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002089 switch (mFeatureLevel)
2090 {
2091 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2092 case D3D_FEATURE_LEVEL_10_1:
2093 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2094 default: UNREACHABLE(); return 0;
2095 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002096}
2097
2098int Renderer11::getMaxTextureHeight() const
2099{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002100 switch (mFeatureLevel)
2101 {
2102 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2103 case D3D_FEATURE_LEVEL_10_1:
2104 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2105 default: UNREACHABLE(); return 0;
2106 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002107}
2108
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002109int Renderer11::getMaxTextureDepth() const
2110{
2111 switch (mFeatureLevel)
2112 {
2113 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2114 case D3D_FEATURE_LEVEL_10_1:
2115 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2116 default: UNREACHABLE(); return 0;
2117 }
2118}
2119
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002120int Renderer11::getMaxTextureArrayLayers() const
2121{
2122 switch (mFeatureLevel)
2123 {
2124 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2125 case D3D_FEATURE_LEVEL_10_1:
2126 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2127 default: UNREACHABLE(); return 0;
2128 }
2129}
2130
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002131int Renderer11::getMinSwapInterval() const
2132{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002133 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002134}
2135
2136int Renderer11::getMaxSwapInterval() const
2137{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002138 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002139}
2140
2141int Renderer11::getMaxSupportedSamples() const
2142{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002143 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002144}
2145
Geoff Lang005df412013-10-16 14:12:50 -04002146GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002147{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002148 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002149 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2150 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2151}
2152
Geoff Lang005df412013-10-16 14:12:50 -04002153GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002154{
2155 unsigned int numCounts = 0;
2156
2157 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002158 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2159 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002160 {
2161 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2162 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2163
2164 if (iter != mMultisampleSupportMap.end())
2165 {
2166 const MultisampleSupportInfo& info = iter->second;
2167 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2168 {
2169 if (info.qualityLevels[i] > 0)
2170 {
2171 numCounts++;
2172 }
2173 }
2174 }
2175 }
2176
2177 return numCounts;
2178}
2179
Geoff Lang005df412013-10-16 14:12:50 -04002180void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002181{
2182 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002183 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2184 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2185 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002186 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002187 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002188
2189 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2190 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2191
2192 if (iter != mMultisampleSupportMap.end())
2193 {
2194 const MultisampleSupportInfo& info = iter->second;
2195 int bufPos = 0;
2196 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2197 {
2198 if (info.qualityLevels[i] > 0)
2199 {
2200 params[bufPos++] = i + 1;
2201 }
2202 }
2203 }
2204}
2205
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002206int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2207{
2208 if (requested == 0)
2209 {
2210 return 0;
2211 }
2212
2213 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2214 if (iter != mMultisampleSupportMap.end())
2215 {
2216 const MultisampleSupportInfo& info = iter->second;
2217 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2218 {
2219 if (info.qualityLevels[i] > 0)
2220 {
2221 return i + 1;
2222 }
2223 }
2224 }
2225
2226 return -1;
2227}
2228
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002229unsigned int Renderer11::getMaxRenderTargets() const
2230{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002231 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2232 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2233
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002234 switch (mFeatureLevel)
2235 {
2236 case D3D_FEATURE_LEVEL_11_0:
2237 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2238 case D3D_FEATURE_LEVEL_10_1:
2239 case D3D_FEATURE_LEVEL_10_0:
Geoff Langa0dc2192014-06-09 15:37:55 -04002240 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002241 default:
2242 UNREACHABLE();
2243 return 1;
2244 }
2245}
2246
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002247bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002248{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002249 if (source && dest)
2250 {
2251 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2252 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2253
Nicolas Capens76b258f2014-04-03 10:59:42 -04002254 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002255
2256 dest11->invalidateSwizzleCache();
2257
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002258 return true;
2259 }
2260
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002261 return false;
2262}
2263
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002264bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002265{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002266 if (source && dest)
2267 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002268 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2269 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002270
Nicolas Capens76b258f2014-04-03 10:59:42 -04002271 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002272
2273 dest11->invalidateSwizzleCache();
2274
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002275 return true;
2276 }
2277
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002278 return false;
2279}
2280
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002281bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2282{
2283 if (source && dest)
2284 {
2285 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2286 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2287
Nicolas Capens76b258f2014-04-03 10:59:42 -04002288 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002289
2290 dest11->invalidateSwizzleCache();
2291
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002292 return true;
2293 }
2294
2295 return false;
2296}
2297
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002298bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2299{
2300 if (source && dest)
2301 {
2302 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2303 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2304
Nicolas Capens76b258f2014-04-03 10:59:42 -04002305 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002306
2307 dest11->invalidateSwizzleCache();
2308
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002309 return true;
2310 }
2311
2312 return false;
2313}
2314
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002315bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002316 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002317{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002318 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002319 if (!colorbuffer)
2320 {
2321 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002322 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002323 }
2324
2325 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2326 if (!sourceRenderTarget)
2327 {
2328 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002329 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002330 }
2331
2332 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2333 if (!source)
2334 {
2335 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002336 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002337 }
2338
2339 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2340 if (!storage11)
2341 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002342 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002343 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002344 }
2345
2346 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2347 if (!destRenderTarget)
2348 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002349 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002350 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002351 }
2352
2353 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2354 if (!dest)
2355 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002356 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002357 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002358 }
2359
Geoff Langb86b9792013-06-04 16:32:05 -04002360 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2361 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002362
Geoff Langb86b9792013-06-04 16:32:05 -04002363 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2364 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002365
Geoff Langb86b9792013-06-04 16:32:05 -04002366 // Use nearest filtering because source and destination are the same size for the direct
2367 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002368 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002369 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002370
Geoff Lang42477a42013-09-17 17:07:02 -04002371 storage11->invalidateSwizzleCacheLevel(level);
2372
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002373 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002374}
2375
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002376bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002377 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002378{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002379 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002380 if (!colorbuffer)
2381 {
2382 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002383 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002384 }
2385
2386 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2387 if (!sourceRenderTarget)
2388 {
2389 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002390 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002391 }
2392
2393 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2394 if (!source)
2395 {
2396 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002397 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002398 }
2399
2400 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2401 if (!storage11)
2402 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002403 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002404 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002405 }
2406
Nicolas Capensb13f8662013-06-04 13:30:19 -04002407 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002408 if (!destRenderTarget)
2409 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002410 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002411 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002412 }
2413
2414 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2415 if (!dest)
2416 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002417 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002418 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002419 }
2420
Geoff Langb86b9792013-06-04 16:32:05 -04002421 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2422 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002423
Geoff Langb86b9792013-06-04 16:32:05 -04002424 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2425 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002426
Geoff Langb86b9792013-06-04 16:32:05 -04002427 // Use nearest filtering because source and destination are the same size for the direct
2428 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002429 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002430 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002431
Geoff Lang42477a42013-09-17 17:07:02 -04002432 storage11->invalidateSwizzleCacheLevel(level);
2433
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002434 return ret;
2435}
2436
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002437bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2438 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2439{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002440 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002441 if (!colorbuffer)
2442 {
2443 ERR("Failed to retrieve the color buffer from the frame buffer.");
2444 return gl::error(GL_OUT_OF_MEMORY, false);
2445 }
2446
2447 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2448 if (!sourceRenderTarget)
2449 {
2450 ERR("Failed to retrieve the render target from the frame buffer.");
2451 return gl::error(GL_OUT_OF_MEMORY, false);
2452 }
2453
2454 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2455 if (!source)
2456 {
2457 ERR("Failed to retrieve the render target view from the render target.");
2458 return gl::error(GL_OUT_OF_MEMORY, false);
2459 }
2460
2461 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2462 if (!storage11)
2463 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002464 ERR("Failed to retrieve the texture storage from the destination.");
2465 return gl::error(GL_OUT_OF_MEMORY, false);
2466 }
2467
2468 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2469 if (!destRenderTarget)
2470 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002471 ERR("Failed to retrieve the render target from the destination storage.");
2472 return gl::error(GL_OUT_OF_MEMORY, false);
2473 }
2474
2475 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2476 if (!dest)
2477 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002478 ERR("Failed to retrieve the render target view from the destination render target.");
2479 return gl::error(GL_OUT_OF_MEMORY, false);
2480 }
2481
Geoff Langb86b9792013-06-04 16:32:05 -04002482 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2483 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002484
Geoff Langb86b9792013-06-04 16:32:05 -04002485 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2486 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002487
Geoff Langb86b9792013-06-04 16:32:05 -04002488 // Use nearest filtering because source and destination are the same size for the direct
2489 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002490 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002491 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002492
Geoff Lang42477a42013-09-17 17:07:02 -04002493 storage11->invalidateSwizzleCacheLevel(level);
2494
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002495 return ret;
2496}
2497
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002498bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2499 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2500{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002501 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002502 if (!colorbuffer)
2503 {
2504 ERR("Failed to retrieve the color buffer from the frame buffer.");
2505 return gl::error(GL_OUT_OF_MEMORY, false);
2506 }
2507
2508 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2509 if (!sourceRenderTarget)
2510 {
2511 ERR("Failed to retrieve the render target from the frame buffer.");
2512 return gl::error(GL_OUT_OF_MEMORY, false);
2513 }
2514
2515 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2516 if (!source)
2517 {
2518 ERR("Failed to retrieve the render target view from the render target.");
2519 return gl::error(GL_OUT_OF_MEMORY, false);
2520 }
2521
2522 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2523 if (!storage11)
2524 {
Geoff Langea228632013-07-30 15:17:12 -04002525 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002526 ERR("Failed to retrieve the texture storage from the destination.");
2527 return gl::error(GL_OUT_OF_MEMORY, false);
2528 }
2529
2530 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2531 if (!destRenderTarget)
2532 {
Geoff Langea228632013-07-30 15:17:12 -04002533 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002534 ERR("Failed to retrieve the render target from the destination storage.");
2535 return gl::error(GL_OUT_OF_MEMORY, false);
2536 }
2537
2538 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2539 if (!dest)
2540 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002541 ERR("Failed to retrieve the render target view from the destination render target.");
2542 return gl::error(GL_OUT_OF_MEMORY, false);
2543 }
2544
Geoff Langb86b9792013-06-04 16:32:05 -04002545 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2546 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002547
Geoff Langb86b9792013-06-04 16:32:05 -04002548 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2549 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002550
Geoff Langb86b9792013-06-04 16:32:05 -04002551 // Use nearest filtering because source and destination are the same size for the direct
2552 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002553 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002554 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002555
Geoff Lang42477a42013-09-17 17:07:02 -04002556 storage11->invalidateSwizzleCacheLevel(level);
2557
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002558 return ret;
2559}
2560
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002561void Renderer11::unapplyRenderTargets()
2562{
2563 setOneTimeRenderTarget(NULL);
2564}
2565
2566void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2567{
2568 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2569
2570 rtvArray[0] = renderTargetView;
2571
2572 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2573
2574 // Do not preserve the serial for this one-time-use render target
2575 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2576 {
2577 mAppliedRenderTargetSerials[rtIndex] = 0;
2578 }
2579}
2580
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002581RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2582{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002583 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002584 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002585
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002586 if (depth)
2587 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002588 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002589 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002590 swapChain11->getDepthStencilTexture(),
2591 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002592 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002593 }
2594 else
2595 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002596 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002597 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002598 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002599 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002600 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002601 }
2602 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002603}
2604
Geoff Langa2d97f12013-06-11 11:44:02 -04002605RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002606{
Geoff Langa2d97f12013-06-11 11:44:02 -04002607 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002608 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002609}
2610
Geoff Lang48dcae72014-02-05 16:28:24 -05002611ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type,
2612 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2613 bool separatedOutputBuffers)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002614{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002615 ShaderExecutable11 *executable = NULL;
Geoff Lang48dcae72014-02-05 16:28:24 -05002616 HRESULT result;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002617
2618 switch (type)
2619 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002620 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002621 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002622 ID3D11VertexShader *vertexShader = NULL;
2623 ID3D11GeometryShader *streamOutShader = NULL;
2624
2625 result = mDevice->CreateVertexShader(function, length, NULL, &vertexShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002626 ASSERT(SUCCEEDED(result));
2627
Geoff Lang48dcae72014-02-05 16:28:24 -05002628 if (transformFeedbackVaryings.size() > 0)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002629 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002630 std::vector<D3D11_SO_DECLARATION_ENTRY> soDeclaration;
2631 for (size_t i = 0; i < transformFeedbackVaryings.size(); i++)
2632 {
2633 const gl::LinkedVarying &varying = transformFeedbackVaryings[i];
Geoff Langcebb5aa2014-04-07 14:13:40 -04002634 for (size_t j = 0; j < varying.semanticIndexCount; j++)
Geoff Lang48dcae72014-02-05 16:28:24 -05002635 {
2636 D3D11_SO_DECLARATION_ENTRY entry = { 0 };
2637 entry.Stream = 0;
Geoff Langcebb5aa2014-04-07 14:13:40 -04002638 entry.SemanticName = varying.semanticName.c_str();
2639 entry.SemanticIndex = varying.semanticIndex + j;
Geoff Lang48dcae72014-02-05 16:28:24 -05002640 entry.StartComponent = 0;
2641 entry.ComponentCount = gl::VariableRowCount(type);
2642 entry.OutputSlot = (separatedOutputBuffers ? i : 0);
2643 soDeclaration.push_back(entry);
2644 }
2645 }
2646
2647 result = mDevice->CreateGeometryShaderWithStreamOutput(function, length, soDeclaration.data(), soDeclaration.size(),
2648 NULL, 0, 0, NULL, &streamOutShader);
2649 ASSERT(SUCCEEDED(result));
2650 }
2651
2652 if (vertexShader)
2653 {
2654 executable = new ShaderExecutable11(function, length, vertexShader, streamOutShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002655 }
2656 }
2657 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002658 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002659 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002660 ID3D11PixelShader *pixelShader = NULL;
2661
2662 result = mDevice->CreatePixelShader(function, length, NULL, &pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002663 ASSERT(SUCCEEDED(result));
2664
Geoff Lang48dcae72014-02-05 16:28:24 -05002665 if (pixelShader)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002666 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002667 executable = new ShaderExecutable11(function, length, pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002668 }
2669 }
2670 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002671 case rx::SHADER_GEOMETRY:
2672 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002673 ID3D11GeometryShader *geometryShader = NULL;
2674
2675 result = mDevice->CreateGeometryShader(function, length, NULL, &geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002676 ASSERT(SUCCEEDED(result));
2677
Geoff Lang48dcae72014-02-05 16:28:24 -05002678 if (geometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002679 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002680 executable = new ShaderExecutable11(function, length, geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002681 }
2682 }
2683 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002684 default:
2685 UNREACHABLE();
2686 break;
2687 }
2688
2689 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002690}
2691
Geoff Lang48dcae72014-02-05 16:28:24 -05002692ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
2693 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2694 bool separatedOutputBuffers, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002695{
Geoff Lang6e05c272014-03-17 10:46:54 -07002696 const char *profileType = NULL;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002697 switch (type)
2698 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002699 case rx::SHADER_VERTEX:
Geoff Lang6e05c272014-03-17 10:46:54 -07002700 profileType = "vs";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002701 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002702 case rx::SHADER_PIXEL:
Geoff Lang6e05c272014-03-17 10:46:54 -07002703 profileType = "ps";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002704 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002705 case rx::SHADER_GEOMETRY:
Geoff Lang6e05c272014-03-17 10:46:54 -07002706 profileType = "gs";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002707 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002708 default:
2709 UNREACHABLE();
2710 return NULL;
2711 }
2712
Geoff Lang6e05c272014-03-17 10:46:54 -07002713 const char *profileVersion = NULL;
2714 switch (mFeatureLevel)
2715 {
2716 case D3D_FEATURE_LEVEL_11_0:
2717 profileVersion = "5_0";
2718 break;
2719 case D3D_FEATURE_LEVEL_10_1:
2720 profileVersion = "4_1";
2721 break;
2722 case D3D_FEATURE_LEVEL_10_0:
2723 profileVersion = "4_0";
2724 break;
2725 default:
2726 UNREACHABLE();
2727 return NULL;
2728 }
2729
2730 char profile[32];
2731 snprintf(profile, ArraySize(profile), "%s_%s", profileType, profileVersion);
2732
Nicolas Capens93faad92014-05-10 12:14:13 -04002733 UINT flags = D3DCOMPILE_OPTIMIZATION_LEVEL0;
2734
2735 if (gl::perfActive())
2736 {
2737#ifndef NDEBUG
2738 flags = D3DCOMPILE_SKIP_OPTIMIZATION;
2739#endif
2740
2741 flags |= D3DCOMPILE_DEBUG;
2742
2743 std::string sourcePath = getTempPath();
2744 std::string sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(shaderHLSL);
2745 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
2746 }
2747
2748 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
2749 // Try the default flags first and if compilation fails, try some alternatives.
2750 const UINT extraFlags[] =
2751 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002752 flags,
Nicolas Capens2c27db62014-05-10 12:21:11 -04002753 flags | D3DCOMPILE_SKIP_VALIDATION,
2754 flags | D3DCOMPILE_SKIP_OPTIMIZATION
Nicolas Capens93faad92014-05-10 12:14:13 -04002755 };
2756
2757 const static char *extraFlagNames[] =
2758 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002759 "default",
Nicolas Capens2c27db62014-05-10 12:21:11 -04002760 "skip validation",
2761 "skip optimization"
Nicolas Capens93faad92014-05-10 12:14:13 -04002762 };
2763
2764 int attempts = ArraySize(extraFlags);
2765
2766 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, extraFlags, extraFlagNames, attempts);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002767 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002768 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002769 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002770 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002771
Geoff Lang48dcae72014-02-05 16:28:24 -05002772 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type,
2773 transformFeedbackVaryings, separatedOutputBuffers);
Geoff Langea228632013-07-30 15:17:12 -04002774 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002775
2776 return executable;
2777}
2778
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002779rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2780{
2781 return new UniformStorage11(this, storageSize);
2782}
2783
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002784VertexBuffer *Renderer11::createVertexBuffer()
2785{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002786 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002787}
2788
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002789IndexBuffer *Renderer11::createIndexBuffer()
2790{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002791 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002792}
2793
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002794BufferStorage *Renderer11::createBufferStorage()
2795{
2796 return new BufferStorage11(this);
2797}
2798
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002799QueryImpl *Renderer11::createQuery(GLenum type)
2800{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002801 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002802}
2803
2804FenceImpl *Renderer11::createFence()
2805{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002806 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002807}
2808
Geoff Lang005df412013-10-16 14:12:50 -04002809bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002810{
Jamie Madill4461f092013-10-10 15:10:39 -04002811 int clientVersion = getCurrentClientVersion();
2812
2813 // We only support buffer to texture copies in ES3
2814 if (clientVersion <= 2)
2815 {
2816 return false;
2817 }
2818
2819 // sRGB formats do not work with D3D11 buffer SRVs
2820 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2821 {
2822 return false;
2823 }
2824
2825 // We cannot support direct copies to non-color-renderable formats
Geoff Langcec35902014-04-16 10:52:36 -04002826 if (!getCaps().textureCaps.get(internalFormat).colorRendering)
Jamie Madill4461f092013-10-10 15:10:39 -04002827 {
2828 return false;
2829 }
2830
2831 // We skip all 3-channel formats since sometimes format support is missing
2832 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2833 {
2834 return false;
2835 }
2836
2837 // We don't support formats which we can't represent without conversion
2838 if (getNativeTextureFormat(internalFormat) != internalFormat)
2839 {
2840 return false;
2841 }
2842
2843 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002844}
2845
Jamie Madilla21eea32013-09-18 14:36:25 -04002846bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2847 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2848{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002849 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002850 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2851}
2852
Jamie Madill3c7fa222014-06-05 13:08:51 -04002853bool Renderer11::getRenderTargetResource(gl::FramebufferAttachment *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002854{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002855 ASSERT(colorbuffer != NULL);
2856
2857 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2858 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002859 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002860 *subresourceIndex = renderTarget->getSubresourceIndex();
2861
2862 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2863 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002864 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002865 ID3D11Resource *textureResource = NULL;
2866 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002867
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002868 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002869 {
Geoff Lang8e328842014-02-10 13:11:20 -05002870 HRESULT result = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002871 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002872
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002873 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002874 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002875 return true;
2876 }
2877 else
2878 {
2879 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2880 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002881 }
2882 }
2883 }
2884 }
2885
2886 return false;
2887}
2888
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002889bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002890 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002891{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002892 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002893 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002894 gl::FramebufferAttachment *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002895
2896 if (!readBuffer)
2897 {
2898 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2899 return gl::error(GL_OUT_OF_MEMORY, false);
2900 }
2901
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002902 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002903
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002904 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002905 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002906 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2907 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002908 gl::FramebufferAttachment *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002909
2910 if (!drawBuffer)
2911 {
2912 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2913 return gl::error(GL_OUT_OF_MEMORY, false);
2914 }
2915
2916 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2917
Geoff Lang125deab2013-08-09 13:34:16 -04002918 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002919 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002920 {
2921 return false;
2922 }
2923 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002924 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002925 }
2926
Geoff Lang685806d2013-06-12 11:16:36 -04002927 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002928 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002929 gl::FramebufferAttachment *readBuffer = readTarget->getDepthOrStencilbuffer();
2930 gl::FramebufferAttachment *drawBuffer = drawTarget->getDepthOrStencilbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002931
2932 if (!readBuffer)
2933 {
2934 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2935 return gl::error(GL_OUT_OF_MEMORY, false);
2936 }
2937
2938 if (!drawBuffer)
2939 {
2940 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2941 return gl::error(GL_OUT_OF_MEMORY, false);
2942 }
2943
2944 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2945 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2946
Geoff Lang125deab2013-08-09 13:34:16 -04002947 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002948 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002949 {
2950 return false;
2951 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002952 }
2953
Geoff Lang42477a42013-09-17 17:07:02 -04002954 invalidateFramebufferSwizzles(drawTarget);
2955
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002956 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002957}
2958
Jamie Madilleb9baab2014-03-24 13:19:43 -04002959void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
2960 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002961{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002962 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002963 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002964
Jamie Madill3c7fa222014-06-05 13:08:51 -04002965 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002966
2967 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002968 {
2969 gl::Rectangle area;
2970 area.x = x;
2971 area.y = y;
2972 area.width = width;
2973 area.height = height;
2974
Jamie Madill1ef6fe72014-05-01 14:51:05 -04002975 if (pack.pixelBuffer.get() != NULL)
2976 {
2977 rx::BufferStorage11 *packBufferStorage = BufferStorage11::makeBufferStorage11(pack.pixelBuffer.get()->getStorage());
2978 PackPixelsParams packParams(area, format, type, outputPitch, pack, reinterpret_cast<ptrdiff_t>(pixels));
2979 packBufferStorage->packPixels(colorBufferTexture, subresourceIndex, packParams);
2980 }
2981 else
2982 {
2983 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch, pack, pixels);
2984 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002985
Geoff Langea228632013-07-30 15:17:12 -04002986 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002987 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002988}
2989
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002990Image *Renderer11::createImage()
2991{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002992 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002993}
2994
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002995void Renderer11::generateMipmap(Image *dest, Image *src)
2996{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002997 Image11 *dest11 = Image11::makeImage11(dest);
2998 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04002999 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003000}
3001
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003002TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3003{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003004 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3005 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003006}
3007
Nicolas Capensbf712d02014-03-31 14:23:35 -04003008TextureStorage *Renderer11::createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003009{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003010 return new TextureStorage11_2D(this, internalformat, renderTarget, width, height, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003011}
3012
Nicolas Capensbf712d02014-03-31 14:23:35 -04003013TextureStorage *Renderer11::createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003014{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003015 return new TextureStorage11_Cube(this, internalformat, renderTarget, size, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003016}
3017
Nicolas Capensbf712d02014-03-31 14:23:35 -04003018TextureStorage *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 +00003019{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003020 return new TextureStorage11_3D(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003021}
3022
Nicolas Capensbf712d02014-03-31 14:23:35 -04003023TextureStorage *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 +00003024{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003025 return new TextureStorage11_2DArray(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003026}
3027
Jamie Madilleb9baab2014-03-24 13:19:43 -04003028void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format,
3029 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void *pixels)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003030{
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003031 ASSERT(area.width >= 0);
3032 ASSERT(area.height >= 0);
3033
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003034 D3D11_TEXTURE2D_DESC textureDesc;
3035 texture->GetDesc(&textureDesc);
3036
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003037 // Clamp read region to the defined texture boundaries, preventing out of bounds reads
3038 // and reads of uninitialized data.
3039 gl::Rectangle safeArea;
3040 safeArea.x = gl::clamp(area.x, 0, static_cast<int>(textureDesc.Width));
3041 safeArea.y = gl::clamp(area.y, 0, static_cast<int>(textureDesc.Height));
3042 safeArea.width = gl::clamp(area.width + std::min(area.x, 0), 0,
3043 static_cast<int>(textureDesc.Width) - safeArea.x);
3044 safeArea.height = gl::clamp(area.height + std::min(area.y, 0), 0,
3045 static_cast<int>(textureDesc.Height) - safeArea.y);
3046
3047 ASSERT(safeArea.x >= 0 && safeArea.y >= 0);
3048 ASSERT(safeArea.x + safeArea.width <= static_cast<int>(textureDesc.Width));
3049 ASSERT(safeArea.y + safeArea.height <= static_cast<int>(textureDesc.Height));
3050
3051 if (safeArea.width == 0 || safeArea.height == 0)
3052 {
3053 // no work to do
3054 return;
3055 }
3056
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003057 D3D11_TEXTURE2D_DESC stagingDesc;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003058 stagingDesc.Width = safeArea.width;
3059 stagingDesc.Height = safeArea.height;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003060 stagingDesc.MipLevels = 1;
3061 stagingDesc.ArraySize = 1;
3062 stagingDesc.Format = textureDesc.Format;
3063 stagingDesc.SampleDesc.Count = 1;
3064 stagingDesc.SampleDesc.Quality = 0;
3065 stagingDesc.Usage = D3D11_USAGE_STAGING;
3066 stagingDesc.BindFlags = 0;
3067 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3068 stagingDesc.MiscFlags = 0;
3069
3070 ID3D11Texture2D* stagingTex = NULL;
3071 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3072 if (FAILED(result))
3073 {
3074 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3075 return;
3076 }
3077
3078 ID3D11Texture2D* srcTex = NULL;
3079 if (textureDesc.SampleDesc.Count > 1)
3080 {
3081 D3D11_TEXTURE2D_DESC resolveDesc;
3082 resolveDesc.Width = textureDesc.Width;
3083 resolveDesc.Height = textureDesc.Height;
3084 resolveDesc.MipLevels = 1;
3085 resolveDesc.ArraySize = 1;
3086 resolveDesc.Format = textureDesc.Format;
3087 resolveDesc.SampleDesc.Count = 1;
3088 resolveDesc.SampleDesc.Quality = 0;
3089 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3090 resolveDesc.BindFlags = 0;
3091 resolveDesc.CPUAccessFlags = 0;
3092 resolveDesc.MiscFlags = 0;
3093
3094 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3095 if (FAILED(result))
3096 {
3097 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003098 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003099 return;
3100 }
3101
3102 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3103 subResource = 0;
3104 }
3105 else
3106 {
3107 srcTex = texture;
3108 srcTex->AddRef();
3109 }
3110
3111 D3D11_BOX srcBox;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003112 srcBox.left = static_cast<UINT>(safeArea.x);
3113 srcBox.right = static_cast<UINT>(safeArea.x + safeArea.width);
3114 srcBox.top = static_cast<UINT>(safeArea.y);
3115 srcBox.bottom = static_cast<UINT>(safeArea.y + safeArea.height);
3116 srcBox.front = 0;
3117 srcBox.back = 1;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003118
3119 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3120
Geoff Langea228632013-07-30 15:17:12 -04003121 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003122
Jamie Madill7538f7f2014-04-17 11:53:39 -04003123 PackPixelsParams packParams(safeArea, format, type, outputPitch, pack, 0);
3124 packPixels(stagingTex, packParams, pixels);
3125
3126 SafeRelease(stagingTex);
3127}
3128
3129void Renderer11::packPixels(ID3D11Texture2D *readTexture, const PackPixelsParams &params, void *pixelsOut)
3130{
3131 D3D11_TEXTURE2D_DESC textureDesc;
3132 readTexture->GetDesc(&textureDesc);
3133
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003134 D3D11_MAPPED_SUBRESOURCE mapping;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003135 HRESULT hr = mDeviceContext->Map(readTexture, 0, D3D11_MAP_READ, 0, &mapping);
Geoff Lang9cd19152014-05-28 15:54:34 -04003136 UNUSED_ASSERTION_VARIABLE(hr);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003137 ASSERT(SUCCEEDED(hr));
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003138
3139 unsigned char *source;
3140 int inputPitch;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003141 if (params.pack.reverseRowOrder)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003142 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003143 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (params.area.height - 1);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003144 inputPitch = -static_cast<int>(mapping.RowPitch);
3145 }
3146 else
3147 {
3148 source = static_cast<unsigned char*>(mapping.pData);
3149 inputPitch = static_cast<int>(mapping.RowPitch);
3150 }
3151
Geoff Lang697ad3e2013-06-04 10:11:28 -04003152 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003153
Geoff Lang005df412013-10-16 14:12:50 -04003154 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003155 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3156 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3157
3158 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3159
Jamie Madill7538f7f2014-04-17 11:53:39 -04003160 if (sourceFormat == params.format && sourceType == params.type)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003161 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003162 unsigned char *dest = static_cast<unsigned char*>(pixelsOut) + params.offset;
3163 for (int y = 0; y < params.area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003164 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003165 memcpy(dest + y * params.outputPitch, source + y * inputPitch, params.area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003166 }
3167 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003168 else
3169 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003170 GLenum destInternalFormat = gl::GetSizedInternalFormat(params.format, params.type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003171 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3172
Jamie Madill7538f7f2014-04-17 11:53:39 -04003173 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, params.format, params.type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003174 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003175 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003176 // Fast copy is possible through some special function
Jamie Madill7538f7f2014-04-17 11:53:39 -04003177 for (int y = 0; y < params.area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003178 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003179 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003180 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003181 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003182 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3183
3184 fastCopyFunc(src, dest);
3185 }
3186 }
3187 }
3188 else
3189 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003190 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003191 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(params.format, params.type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003192
3193 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3194 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3195 sizeof(temp) >= sizeof(gl::ColorUI) &&
3196 sizeof(temp) >= sizeof(gl::ColorI));
3197
Jamie Madill7538f7f2014-04-17 11:53:39 -04003198 for (int y = 0; y < params.area.height; y++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003199 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003200 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003201 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003202 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003203 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3204
3205 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3206 // will not allow the copy otherwise.
3207 readFunc(src, temp);
3208 writeFunc(temp, dest);
3209 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003210 }
3211 }
3212 }
3213
Jamie Madill7538f7f2014-04-17 11:53:39 -04003214 mDeviceContext->Unmap(readTexture, 0);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003215}
3216
Geoff Lang758d5b22013-06-11 11:42:50 -04003217bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003218 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3219 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003220{
Geoff Lang975af372013-06-12 11:19:22 -04003221 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3222 // it should never be the case that both color and depth/stencil need to be blitted at
3223 // at the same time.
3224 ASSERT(colorBlit != (depthBlit || stencilBlit));
3225
Geoff Langc1f51be2013-06-11 11:49:14 -04003226 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003227
Geoff Lang4d782732013-07-22 10:44:18 -04003228 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3229 if (!drawRenderTarget)
3230 {
3231 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3232 return gl::error(GL_OUT_OF_MEMORY, false);
3233 }
3234
3235 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3236 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3237 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3238 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3239
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003240 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3241 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003242 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003243 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003244 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003245 }
3246
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003247 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003248 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003249 unsigned int readSubresource = 0;
3250 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003251 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003252 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3253 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003254
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003255 if (unresolvedTexture)
3256 {
3257 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3258 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003259
Geoff Langea228632013-07-30 15:17:12 -04003260 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003261
Geoff Lang5c9a29a2014-02-11 10:26:24 -05003262 HRESULT hresult = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3263 if (FAILED(hresult))
Geoff Langc1f51be2013-06-11 11:49:14 -04003264 {
Geoff Langea228632013-07-30 15:17:12 -04003265 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003266 return gl::error(GL_OUT_OF_MEMORY, false);
3267 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003268 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003269 }
3270 else
3271 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003272 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003273 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003274 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003275 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003276 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003277 }
3278
Geoff Lang4d782732013-07-22 10:44:18 -04003279 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003280 {
Geoff Lang4d782732013-07-22 10:44:18 -04003281 SafeRelease(readTexture);
3282 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003283 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003284 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003285 }
3286
Geoff Lang125deab2013-08-09 13:34:16 -04003287 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3288 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3289
3290 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3291
3292 bool wholeBufferCopy = !scissorNeeded &&
3293 readRect.x == 0 && readRect.width == readSize.width &&
3294 readRect.y == 0 && readRect.height == readSize.height &&
3295 drawRect.x == 0 && drawRect.width == drawSize.width &&
3296 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003297
Geoff Langc1f51be2013-06-11 11:49:14 -04003298 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003299
Geoff Lang1cd1b212014-02-11 09:42:27 -05003300 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003301
3302 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3303 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3304 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3305 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3306
3307 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3308 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3309 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3310
Geoff Langc1f51be2013-06-11 11:49:14 -04003311 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003312 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3313 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003314 {
Geoff Lang125deab2013-08-09 13:34:16 -04003315 UINT dstX = drawRect.x;
3316 UINT dstY = drawRect.y;
3317
Geoff Langc1f51be2013-06-11 11:49:14 -04003318 D3D11_BOX readBox;
3319 readBox.left = readRect.x;
3320 readBox.right = readRect.x + readRect.width;
3321 readBox.top = readRect.y;
3322 readBox.bottom = readRect.y + readRect.height;
3323 readBox.front = 0;
3324 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003325
Geoff Lang125deab2013-08-09 13:34:16 -04003326 if (scissorNeeded)
3327 {
3328 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3329 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3330
3331 if (drawRect.x < scissor->x)
3332 {
3333 dstX = scissor->x;
3334 readBox.left += (scissor->x - drawRect.x);
3335 }
3336 if (drawRect.y < scissor->y)
3337 {
3338 dstY = scissor->y;
3339 readBox.top += (scissor->y - drawRect.y);
3340 }
3341 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3342 {
3343 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3344 }
3345 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3346 {
3347 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3348 }
3349 }
3350
Geoff Langc1f51be2013-06-11 11:49:14 -04003351 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3352 // We also require complete framebuffer copies for depth-stencil blit.
3353 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003354
Geoff Lang125deab2013-08-09 13:34:16 -04003355 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003356 readTexture, readSubresource, pSrcBox);
3357 result = true;
3358 }
3359 else
3360 {
3361 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003362 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003363
Geoff Lang975af372013-06-12 11:19:22 -04003364 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003365 {
Geoff Lang975af372013-06-12 11:19:22 -04003366 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003367 drawTexture, drawSubresource, drawArea, drawSize,
3368 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003369 }
3370 else if (depthBlit)
3371 {
Geoff Lang125deab2013-08-09 13:34:16 -04003372 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3373 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003374 }
3375 else if (stencilBlit)
3376 {
3377 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003378 drawTexture, drawSubresource, drawArea, drawSize,
3379 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003380 }
3381 else
3382 {
Geoff Lang685806d2013-06-12 11:16:36 -04003383 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003384 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3385 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003386 }
3387 }
3388
3389 SafeRelease(readTexture);
3390 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003391
3392 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003393}
3394
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003395ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3396{
3397 D3D11_TEXTURE2D_DESC textureDesc;
3398 source->GetDesc(&textureDesc);
3399
3400 if (textureDesc.SampleDesc.Count > 1)
3401 {
3402 D3D11_TEXTURE2D_DESC resolveDesc;
3403 resolveDesc.Width = textureDesc.Width;
3404 resolveDesc.Height = textureDesc.Height;
3405 resolveDesc.MipLevels = 1;
3406 resolveDesc.ArraySize = 1;
3407 resolveDesc.Format = textureDesc.Format;
3408 resolveDesc.SampleDesc.Count = 1;
3409 resolveDesc.SampleDesc.Quality = 0;
3410 resolveDesc.Usage = textureDesc.Usage;
3411 resolveDesc.BindFlags = textureDesc.BindFlags;
3412 resolveDesc.CPUAccessFlags = 0;
3413 resolveDesc.MiscFlags = 0;
3414
3415 ID3D11Texture2D *resolveTexture = NULL;
3416 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3417 if (FAILED(result))
3418 {
3419 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3420 return NULL;
3421 }
3422
3423 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3424 return resolveTexture;
3425 }
3426 else
3427 {
3428 source->AddRef();
3429 return source;
3430 }
3431}
3432
Jamie Madill3c7fa222014-06-05 13:08:51 -04003433void Renderer11::invalidateFBOAttachmentSwizzles(gl::FramebufferAttachment *attachment, int mipLevel)
Geoff Lang42477a42013-09-17 17:07:02 -04003434{
Jamie Madill3c7fa222014-06-05 13:08:51 -04003435 ASSERT(attachment->isTexture());
3436 TextureStorage *texStorage = attachment->getTextureStorage();
Geoff Lang42477a42013-09-17 17:07:02 -04003437 if (texStorage)
3438 {
3439 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3440 if (!texStorage11)
3441 {
3442 ERR("texture storage pointer unexpectedly null.");
3443 return;
3444 }
3445
3446 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3447 }
3448}
3449
3450void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3451{
3452 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3453 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003454 gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(colorAttachment);
3455 if (attachment && attachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003456 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003457 invalidateFBOAttachmentSwizzles(attachment, framebuffer->getColorbufferMipLevel(colorAttachment));
Geoff Lang42477a42013-09-17 17:07:02 -04003458 }
3459 }
3460
Jamie Madill3c7fa222014-06-05 13:08:51 -04003461 gl::FramebufferAttachment *depthAttachment = framebuffer->getDepthbuffer();
3462 if (depthAttachment && depthAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003463 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003464 invalidateFBOAttachmentSwizzles(depthAttachment, framebuffer->getDepthbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003465 }
3466
Jamie Madill3c7fa222014-06-05 13:08:51 -04003467 gl::FramebufferAttachment *stencilAttachment = framebuffer->getStencilbuffer();
3468 if (stencilAttachment && stencilAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003469 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003470 invalidateFBOAttachmentSwizzles(stencilAttachment, framebuffer->getStencilbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003471 }
3472}
3473
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003474bool Renderer11::getLUID(LUID *adapterLuid) const
3475{
3476 adapterLuid->HighPart = 0;
3477 adapterLuid->LowPart = 0;
3478
3479 if (!mDxgiAdapter)
3480 {
3481 return false;
3482 }
3483
3484 DXGI_ADAPTER_DESC adapterDesc;
3485 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3486 {
3487 return false;
3488 }
3489
3490 *adapterLuid = adapterDesc.AdapterLuid;
3491 return true;
3492}
3493
Geoff Lang005df412013-10-16 14:12:50 -04003494GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003495{
3496 int clientVersion = getCurrentClientVersion();
3497 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3498}
3499
Jamie Madill95ffb862014-01-29 09:26:59 -05003500rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3501{
3502 return gl_d3d11::GetVertexConversionType(vertexFormat);
3503}
3504
3505GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3506{
3507 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3508}
3509
Geoff Lang61e49a52013-05-29 10:22:58 -04003510Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3511{
3512 MultisampleSupportInfo supportInfo = { 0 };
3513
3514 UINT formatSupport;
3515 HRESULT result;
3516
3517 result = mDevice->CheckFormatSupport(format, &formatSupport);
3518 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3519 {
3520 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3521 {
3522 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3523 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3524 {
3525 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3526 }
3527 else
3528 {
3529 supportInfo.qualityLevels[i - 1] = 0;
3530 }
3531 }
3532 }
3533
3534 return supportInfo;
3535}
3536
Geoff Langcec35902014-04-16 10:52:36 -04003537gl::Caps Renderer11::generateCaps() const
3538{
3539 return d3d11_gl::GenerateCaps(mDevice);
3540}
3541
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003542}