blob: c018bdafdfb3f39031ba75b9c871f1d879a13dc6 [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 Lang626d54e2014-02-07 14:24:12 -05002240 // Feature level 10.0 and 10.1 cards perform very poorly when the pixel shader
2241 // outputs to multiple RTs that are not bound.
2242 // TODO: Remove pixel shader outputs for render targets that are not bound.
2243 return 1;
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002244 default:
2245 UNREACHABLE();
2246 return 1;
2247 }
2248}
2249
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002250bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002251{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002252 if (source && dest)
2253 {
2254 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2255 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2256
Nicolas Capens76b258f2014-04-03 10:59:42 -04002257 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002258
2259 dest11->invalidateSwizzleCache();
2260
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002261 return true;
2262 }
2263
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002264 return false;
2265}
2266
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002267bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002268{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002269 if (source && dest)
2270 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002271 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2272 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002273
Nicolas Capens76b258f2014-04-03 10:59:42 -04002274 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002275
2276 dest11->invalidateSwizzleCache();
2277
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002278 return true;
2279 }
2280
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002281 return false;
2282}
2283
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002284bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2285{
2286 if (source && dest)
2287 {
2288 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2289 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2290
Nicolas Capens76b258f2014-04-03 10:59:42 -04002291 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002292
2293 dest11->invalidateSwizzleCache();
2294
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002295 return true;
2296 }
2297
2298 return false;
2299}
2300
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002301bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2302{
2303 if (source && dest)
2304 {
2305 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2306 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2307
Nicolas Capens76b258f2014-04-03 10:59:42 -04002308 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002309
2310 dest11->invalidateSwizzleCache();
2311
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002312 return true;
2313 }
2314
2315 return false;
2316}
2317
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002318bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002319 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002320{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002321 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002322 if (!colorbuffer)
2323 {
2324 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002325 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002326 }
2327
2328 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2329 if (!sourceRenderTarget)
2330 {
2331 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002332 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002333 }
2334
2335 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2336 if (!source)
2337 {
2338 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002339 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002340 }
2341
2342 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2343 if (!storage11)
2344 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002345 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002346 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002347 }
2348
2349 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2350 if (!destRenderTarget)
2351 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002352 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002353 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002354 }
2355
2356 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2357 if (!dest)
2358 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002359 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002360 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002361 }
2362
Geoff Langb86b9792013-06-04 16:32:05 -04002363 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2364 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002365
Geoff Langb86b9792013-06-04 16:32:05 -04002366 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2367 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002368
Geoff Langb86b9792013-06-04 16:32:05 -04002369 // Use nearest filtering because source and destination are the same size for the direct
2370 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002371 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002372 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002373
Geoff Lang42477a42013-09-17 17:07:02 -04002374 storage11->invalidateSwizzleCacheLevel(level);
2375
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002376 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002377}
2378
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002379bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002380 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002381{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002382 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002383 if (!colorbuffer)
2384 {
2385 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002386 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002387 }
2388
2389 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2390 if (!sourceRenderTarget)
2391 {
2392 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002393 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002394 }
2395
2396 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2397 if (!source)
2398 {
2399 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002400 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002401 }
2402
2403 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2404 if (!storage11)
2405 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002406 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002407 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002408 }
2409
Nicolas Capensb13f8662013-06-04 13:30:19 -04002410 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002411 if (!destRenderTarget)
2412 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002413 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002414 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002415 }
2416
2417 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2418 if (!dest)
2419 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002420 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002421 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002422 }
2423
Geoff Langb86b9792013-06-04 16:32:05 -04002424 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2425 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002426
Geoff Langb86b9792013-06-04 16:32:05 -04002427 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2428 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002429
Geoff Langb86b9792013-06-04 16:32:05 -04002430 // Use nearest filtering because source and destination are the same size for the direct
2431 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002432 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002433 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002434
Geoff Lang42477a42013-09-17 17:07:02 -04002435 storage11->invalidateSwizzleCacheLevel(level);
2436
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002437 return ret;
2438}
2439
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002440bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2441 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2442{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002443 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002444 if (!colorbuffer)
2445 {
2446 ERR("Failed to retrieve the color buffer from the frame buffer.");
2447 return gl::error(GL_OUT_OF_MEMORY, false);
2448 }
2449
2450 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2451 if (!sourceRenderTarget)
2452 {
2453 ERR("Failed to retrieve the render target from the frame buffer.");
2454 return gl::error(GL_OUT_OF_MEMORY, false);
2455 }
2456
2457 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2458 if (!source)
2459 {
2460 ERR("Failed to retrieve the render target view from the render target.");
2461 return gl::error(GL_OUT_OF_MEMORY, false);
2462 }
2463
2464 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2465 if (!storage11)
2466 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002467 ERR("Failed to retrieve the texture storage from the destination.");
2468 return gl::error(GL_OUT_OF_MEMORY, false);
2469 }
2470
2471 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2472 if (!destRenderTarget)
2473 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002474 ERR("Failed to retrieve the render target from the destination storage.");
2475 return gl::error(GL_OUT_OF_MEMORY, false);
2476 }
2477
2478 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2479 if (!dest)
2480 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002481 ERR("Failed to retrieve the render target view from the destination render target.");
2482 return gl::error(GL_OUT_OF_MEMORY, false);
2483 }
2484
Geoff Langb86b9792013-06-04 16:32:05 -04002485 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2486 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002487
Geoff Langb86b9792013-06-04 16:32:05 -04002488 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2489 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002490
Geoff Langb86b9792013-06-04 16:32:05 -04002491 // Use nearest filtering because source and destination are the same size for the direct
2492 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002493 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002494 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002495
Geoff Lang42477a42013-09-17 17:07:02 -04002496 storage11->invalidateSwizzleCacheLevel(level);
2497
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002498 return ret;
2499}
2500
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002501bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2502 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2503{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002504 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002505 if (!colorbuffer)
2506 {
2507 ERR("Failed to retrieve the color buffer from the frame buffer.");
2508 return gl::error(GL_OUT_OF_MEMORY, false);
2509 }
2510
2511 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2512 if (!sourceRenderTarget)
2513 {
2514 ERR("Failed to retrieve the render target from the frame buffer.");
2515 return gl::error(GL_OUT_OF_MEMORY, false);
2516 }
2517
2518 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2519 if (!source)
2520 {
2521 ERR("Failed to retrieve the render target view from the render target.");
2522 return gl::error(GL_OUT_OF_MEMORY, false);
2523 }
2524
2525 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2526 if (!storage11)
2527 {
Geoff Langea228632013-07-30 15:17:12 -04002528 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002529 ERR("Failed to retrieve the texture storage from the destination.");
2530 return gl::error(GL_OUT_OF_MEMORY, false);
2531 }
2532
2533 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2534 if (!destRenderTarget)
2535 {
Geoff Langea228632013-07-30 15:17:12 -04002536 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002537 ERR("Failed to retrieve the render target from the destination storage.");
2538 return gl::error(GL_OUT_OF_MEMORY, false);
2539 }
2540
2541 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2542 if (!dest)
2543 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002544 ERR("Failed to retrieve the render target view from the destination render target.");
2545 return gl::error(GL_OUT_OF_MEMORY, false);
2546 }
2547
Geoff Langb86b9792013-06-04 16:32:05 -04002548 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2549 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002550
Geoff Langb86b9792013-06-04 16:32:05 -04002551 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2552 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002553
Geoff Langb86b9792013-06-04 16:32:05 -04002554 // Use nearest filtering because source and destination are the same size for the direct
2555 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002556 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002557 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002558
Geoff Lang42477a42013-09-17 17:07:02 -04002559 storage11->invalidateSwizzleCacheLevel(level);
2560
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002561 return ret;
2562}
2563
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002564void Renderer11::unapplyRenderTargets()
2565{
2566 setOneTimeRenderTarget(NULL);
2567}
2568
2569void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2570{
2571 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2572
2573 rtvArray[0] = renderTargetView;
2574
2575 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2576
2577 // Do not preserve the serial for this one-time-use render target
2578 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2579 {
2580 mAppliedRenderTargetSerials[rtIndex] = 0;
2581 }
2582}
2583
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002584RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2585{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002586 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002587 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002588
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002589 if (depth)
2590 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002591 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002592 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002593 swapChain11->getDepthStencilTexture(),
2594 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002595 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002596 }
2597 else
2598 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002599 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002600 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002601 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002602 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002603 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002604 }
2605 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002606}
2607
Geoff Langa2d97f12013-06-11 11:44:02 -04002608RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002609{
Geoff Langa2d97f12013-06-11 11:44:02 -04002610 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002611 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002612}
2613
Geoff Lang48dcae72014-02-05 16:28:24 -05002614ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type,
2615 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2616 bool separatedOutputBuffers)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002617{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002618 ShaderExecutable11 *executable = NULL;
Geoff Lang48dcae72014-02-05 16:28:24 -05002619 HRESULT result;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002620
2621 switch (type)
2622 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002623 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002624 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002625 ID3D11VertexShader *vertexShader = NULL;
2626 ID3D11GeometryShader *streamOutShader = NULL;
2627
2628 result = mDevice->CreateVertexShader(function, length, NULL, &vertexShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002629 ASSERT(SUCCEEDED(result));
2630
Geoff Lang48dcae72014-02-05 16:28:24 -05002631 if (transformFeedbackVaryings.size() > 0)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002632 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002633 std::vector<D3D11_SO_DECLARATION_ENTRY> soDeclaration;
2634 for (size_t i = 0; i < transformFeedbackVaryings.size(); i++)
2635 {
2636 const gl::LinkedVarying &varying = transformFeedbackVaryings[i];
Geoff Langcebb5aa2014-04-07 14:13:40 -04002637 for (size_t j = 0; j < varying.semanticIndexCount; j++)
Geoff Lang48dcae72014-02-05 16:28:24 -05002638 {
2639 D3D11_SO_DECLARATION_ENTRY entry = { 0 };
2640 entry.Stream = 0;
Geoff Langcebb5aa2014-04-07 14:13:40 -04002641 entry.SemanticName = varying.semanticName.c_str();
2642 entry.SemanticIndex = varying.semanticIndex + j;
Geoff Lang48dcae72014-02-05 16:28:24 -05002643 entry.StartComponent = 0;
2644 entry.ComponentCount = gl::VariableRowCount(type);
2645 entry.OutputSlot = (separatedOutputBuffers ? i : 0);
2646 soDeclaration.push_back(entry);
2647 }
2648 }
2649
2650 result = mDevice->CreateGeometryShaderWithStreamOutput(function, length, soDeclaration.data(), soDeclaration.size(),
2651 NULL, 0, 0, NULL, &streamOutShader);
2652 ASSERT(SUCCEEDED(result));
2653 }
2654
2655 if (vertexShader)
2656 {
2657 executable = new ShaderExecutable11(function, length, vertexShader, streamOutShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002658 }
2659 }
2660 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002661 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002662 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002663 ID3D11PixelShader *pixelShader = NULL;
2664
2665 result = mDevice->CreatePixelShader(function, length, NULL, &pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002666 ASSERT(SUCCEEDED(result));
2667
Geoff Lang48dcae72014-02-05 16:28:24 -05002668 if (pixelShader)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002669 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002670 executable = new ShaderExecutable11(function, length, pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002671 }
2672 }
2673 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002674 case rx::SHADER_GEOMETRY:
2675 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002676 ID3D11GeometryShader *geometryShader = NULL;
2677
2678 result = mDevice->CreateGeometryShader(function, length, NULL, &geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002679 ASSERT(SUCCEEDED(result));
2680
Geoff Lang48dcae72014-02-05 16:28:24 -05002681 if (geometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002682 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002683 executable = new ShaderExecutable11(function, length, geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002684 }
2685 }
2686 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002687 default:
2688 UNREACHABLE();
2689 break;
2690 }
2691
2692 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002693}
2694
Geoff Lang48dcae72014-02-05 16:28:24 -05002695ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
2696 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2697 bool separatedOutputBuffers, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002698{
Geoff Lang6e05c272014-03-17 10:46:54 -07002699 const char *profileType = NULL;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002700 switch (type)
2701 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002702 case rx::SHADER_VERTEX:
Geoff Lang6e05c272014-03-17 10:46:54 -07002703 profileType = "vs";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002704 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002705 case rx::SHADER_PIXEL:
Geoff Lang6e05c272014-03-17 10:46:54 -07002706 profileType = "ps";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002707 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002708 case rx::SHADER_GEOMETRY:
Geoff Lang6e05c272014-03-17 10:46:54 -07002709 profileType = "gs";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002710 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002711 default:
2712 UNREACHABLE();
2713 return NULL;
2714 }
2715
Geoff Lang6e05c272014-03-17 10:46:54 -07002716 const char *profileVersion = NULL;
2717 switch (mFeatureLevel)
2718 {
2719 case D3D_FEATURE_LEVEL_11_0:
2720 profileVersion = "5_0";
2721 break;
2722 case D3D_FEATURE_LEVEL_10_1:
2723 profileVersion = "4_1";
2724 break;
2725 case D3D_FEATURE_LEVEL_10_0:
2726 profileVersion = "4_0";
2727 break;
2728 default:
2729 UNREACHABLE();
2730 return NULL;
2731 }
2732
2733 char profile[32];
2734 snprintf(profile, ArraySize(profile), "%s_%s", profileType, profileVersion);
2735
Nicolas Capens93faad92014-05-10 12:14:13 -04002736 UINT flags = D3DCOMPILE_OPTIMIZATION_LEVEL0;
2737
2738 if (gl::perfActive())
2739 {
2740#ifndef NDEBUG
2741 flags = D3DCOMPILE_SKIP_OPTIMIZATION;
2742#endif
2743
2744 flags |= D3DCOMPILE_DEBUG;
2745
2746 std::string sourcePath = getTempPath();
2747 std::string sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(shaderHLSL);
2748 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
2749 }
2750
2751 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
2752 // Try the default flags first and if compilation fails, try some alternatives.
2753 const UINT extraFlags[] =
2754 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002755 flags,
Nicolas Capens2c27db62014-05-10 12:21:11 -04002756 flags | D3DCOMPILE_SKIP_VALIDATION,
2757 flags | D3DCOMPILE_SKIP_OPTIMIZATION
Nicolas Capens93faad92014-05-10 12:14:13 -04002758 };
2759
2760 const static char *extraFlagNames[] =
2761 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002762 "default",
Nicolas Capens2c27db62014-05-10 12:21:11 -04002763 "skip validation",
2764 "skip optimization"
Nicolas Capens93faad92014-05-10 12:14:13 -04002765 };
2766
2767 int attempts = ArraySize(extraFlags);
2768
2769 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, extraFlags, extraFlagNames, attempts);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002770 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002771 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002772 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002773 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002774
Geoff Lang48dcae72014-02-05 16:28:24 -05002775 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type,
2776 transformFeedbackVaryings, separatedOutputBuffers);
Geoff Langea228632013-07-30 15:17:12 -04002777 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002778
2779 return executable;
2780}
2781
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002782rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2783{
2784 return new UniformStorage11(this, storageSize);
2785}
2786
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002787VertexBuffer *Renderer11::createVertexBuffer()
2788{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002789 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002790}
2791
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002792IndexBuffer *Renderer11::createIndexBuffer()
2793{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002794 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002795}
2796
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002797BufferStorage *Renderer11::createBufferStorage()
2798{
2799 return new BufferStorage11(this);
2800}
2801
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002802QueryImpl *Renderer11::createQuery(GLenum type)
2803{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002804 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002805}
2806
2807FenceImpl *Renderer11::createFence()
2808{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002809 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002810}
2811
Geoff Lang005df412013-10-16 14:12:50 -04002812bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002813{
Jamie Madill4461f092013-10-10 15:10:39 -04002814 int clientVersion = getCurrentClientVersion();
2815
2816 // We only support buffer to texture copies in ES3
2817 if (clientVersion <= 2)
2818 {
2819 return false;
2820 }
2821
2822 // sRGB formats do not work with D3D11 buffer SRVs
2823 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2824 {
2825 return false;
2826 }
2827
2828 // We cannot support direct copies to non-color-renderable formats
Geoff Langcec35902014-04-16 10:52:36 -04002829 if (!getCaps().textureCaps.get(internalFormat).colorRendering)
Jamie Madill4461f092013-10-10 15:10:39 -04002830 {
2831 return false;
2832 }
2833
2834 // We skip all 3-channel formats since sometimes format support is missing
2835 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2836 {
2837 return false;
2838 }
2839
2840 // We don't support formats which we can't represent without conversion
2841 if (getNativeTextureFormat(internalFormat) != internalFormat)
2842 {
2843 return false;
2844 }
2845
2846 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002847}
2848
Jamie Madilla21eea32013-09-18 14:36:25 -04002849bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2850 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2851{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002852 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002853 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2854}
2855
Jamie Madill3c7fa222014-06-05 13:08:51 -04002856bool Renderer11::getRenderTargetResource(gl::FramebufferAttachment *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002857{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002858 ASSERT(colorbuffer != NULL);
2859
2860 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2861 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002862 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002863 *subresourceIndex = renderTarget->getSubresourceIndex();
2864
2865 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2866 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002867 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002868 ID3D11Resource *textureResource = NULL;
2869 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002870
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002871 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002872 {
Geoff Lang8e328842014-02-10 13:11:20 -05002873 HRESULT result = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002874 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002875
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002876 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002877 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002878 return true;
2879 }
2880 else
2881 {
2882 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2883 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002884 }
2885 }
2886 }
2887 }
2888
2889 return false;
2890}
2891
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002892bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002893 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002894{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002895 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002896 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002897 gl::FramebufferAttachment *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002898
2899 if (!readBuffer)
2900 {
2901 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2902 return gl::error(GL_OUT_OF_MEMORY, false);
2903 }
2904
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002905 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002906
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002907 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002908 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002909 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2910 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002911 gl::FramebufferAttachment *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002912
2913 if (!drawBuffer)
2914 {
2915 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2916 return gl::error(GL_OUT_OF_MEMORY, false);
2917 }
2918
2919 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2920
Geoff Lang125deab2013-08-09 13:34:16 -04002921 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002922 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002923 {
2924 return false;
2925 }
2926 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002927 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002928 }
2929
Geoff Lang685806d2013-06-12 11:16:36 -04002930 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002931 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002932 gl::FramebufferAttachment *readBuffer = readTarget->getDepthOrStencilbuffer();
2933 gl::FramebufferAttachment *drawBuffer = drawTarget->getDepthOrStencilbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002934
2935 if (!readBuffer)
2936 {
2937 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2938 return gl::error(GL_OUT_OF_MEMORY, false);
2939 }
2940
2941 if (!drawBuffer)
2942 {
2943 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2944 return gl::error(GL_OUT_OF_MEMORY, false);
2945 }
2946
2947 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2948 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2949
Geoff Lang125deab2013-08-09 13:34:16 -04002950 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002951 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002952 {
2953 return false;
2954 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002955 }
2956
Geoff Lang42477a42013-09-17 17:07:02 -04002957 invalidateFramebufferSwizzles(drawTarget);
2958
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002959 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002960}
2961
Jamie Madilleb9baab2014-03-24 13:19:43 -04002962void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
2963 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002964{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002965 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002966 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002967
Jamie Madill3c7fa222014-06-05 13:08:51 -04002968 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002969
2970 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002971 {
2972 gl::Rectangle area;
2973 area.x = x;
2974 area.y = y;
2975 area.width = width;
2976 area.height = height;
2977
Jamie Madill1ef6fe72014-05-01 14:51:05 -04002978 if (pack.pixelBuffer.get() != NULL)
2979 {
2980 rx::BufferStorage11 *packBufferStorage = BufferStorage11::makeBufferStorage11(pack.pixelBuffer.get()->getStorage());
2981 PackPixelsParams packParams(area, format, type, outputPitch, pack, reinterpret_cast<ptrdiff_t>(pixels));
2982 packBufferStorage->packPixels(colorBufferTexture, subresourceIndex, packParams);
2983 }
2984 else
2985 {
2986 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch, pack, pixels);
2987 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002988
Geoff Langea228632013-07-30 15:17:12 -04002989 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002990 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002991}
2992
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002993Image *Renderer11::createImage()
2994{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002995 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002996}
2997
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002998void Renderer11::generateMipmap(Image *dest, Image *src)
2999{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003000 Image11 *dest11 = Image11::makeImage11(dest);
3001 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003002 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003003}
3004
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003005TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3006{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003007 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3008 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003009}
3010
Nicolas Capensbf712d02014-03-31 14:23:35 -04003011TextureStorage *Renderer11::createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003012{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003013 return new TextureStorage11_2D(this, internalformat, renderTarget, width, height, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003014}
3015
Nicolas Capensbf712d02014-03-31 14:23:35 -04003016TextureStorage *Renderer11::createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003017{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003018 return new TextureStorage11_Cube(this, internalformat, renderTarget, size, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003019}
3020
Nicolas Capensbf712d02014-03-31 14:23:35 -04003021TextureStorage *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 +00003022{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003023 return new TextureStorage11_3D(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003024}
3025
Nicolas Capensbf712d02014-03-31 14:23:35 -04003026TextureStorage *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 +00003027{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003028 return new TextureStorage11_2DArray(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003029}
3030
Jamie Madilleb9baab2014-03-24 13:19:43 -04003031void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format,
3032 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void *pixels)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003033{
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003034 ASSERT(area.width >= 0);
3035 ASSERT(area.height >= 0);
3036
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003037 D3D11_TEXTURE2D_DESC textureDesc;
3038 texture->GetDesc(&textureDesc);
3039
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003040 // Clamp read region to the defined texture boundaries, preventing out of bounds reads
3041 // and reads of uninitialized data.
3042 gl::Rectangle safeArea;
3043 safeArea.x = gl::clamp(area.x, 0, static_cast<int>(textureDesc.Width));
3044 safeArea.y = gl::clamp(area.y, 0, static_cast<int>(textureDesc.Height));
3045 safeArea.width = gl::clamp(area.width + std::min(area.x, 0), 0,
3046 static_cast<int>(textureDesc.Width) - safeArea.x);
3047 safeArea.height = gl::clamp(area.height + std::min(area.y, 0), 0,
3048 static_cast<int>(textureDesc.Height) - safeArea.y);
3049
3050 ASSERT(safeArea.x >= 0 && safeArea.y >= 0);
3051 ASSERT(safeArea.x + safeArea.width <= static_cast<int>(textureDesc.Width));
3052 ASSERT(safeArea.y + safeArea.height <= static_cast<int>(textureDesc.Height));
3053
3054 if (safeArea.width == 0 || safeArea.height == 0)
3055 {
3056 // no work to do
3057 return;
3058 }
3059
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003060 D3D11_TEXTURE2D_DESC stagingDesc;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003061 stagingDesc.Width = safeArea.width;
3062 stagingDesc.Height = safeArea.height;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003063 stagingDesc.MipLevels = 1;
3064 stagingDesc.ArraySize = 1;
3065 stagingDesc.Format = textureDesc.Format;
3066 stagingDesc.SampleDesc.Count = 1;
3067 stagingDesc.SampleDesc.Quality = 0;
3068 stagingDesc.Usage = D3D11_USAGE_STAGING;
3069 stagingDesc.BindFlags = 0;
3070 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3071 stagingDesc.MiscFlags = 0;
3072
3073 ID3D11Texture2D* stagingTex = NULL;
3074 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3075 if (FAILED(result))
3076 {
3077 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3078 return;
3079 }
3080
3081 ID3D11Texture2D* srcTex = NULL;
3082 if (textureDesc.SampleDesc.Count > 1)
3083 {
3084 D3D11_TEXTURE2D_DESC resolveDesc;
3085 resolveDesc.Width = textureDesc.Width;
3086 resolveDesc.Height = textureDesc.Height;
3087 resolveDesc.MipLevels = 1;
3088 resolveDesc.ArraySize = 1;
3089 resolveDesc.Format = textureDesc.Format;
3090 resolveDesc.SampleDesc.Count = 1;
3091 resolveDesc.SampleDesc.Quality = 0;
3092 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3093 resolveDesc.BindFlags = 0;
3094 resolveDesc.CPUAccessFlags = 0;
3095 resolveDesc.MiscFlags = 0;
3096
3097 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3098 if (FAILED(result))
3099 {
3100 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003101 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003102 return;
3103 }
3104
3105 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3106 subResource = 0;
3107 }
3108 else
3109 {
3110 srcTex = texture;
3111 srcTex->AddRef();
3112 }
3113
3114 D3D11_BOX srcBox;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003115 srcBox.left = static_cast<UINT>(safeArea.x);
3116 srcBox.right = static_cast<UINT>(safeArea.x + safeArea.width);
3117 srcBox.top = static_cast<UINT>(safeArea.y);
3118 srcBox.bottom = static_cast<UINT>(safeArea.y + safeArea.height);
3119 srcBox.front = 0;
3120 srcBox.back = 1;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003121
3122 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3123
Geoff Langea228632013-07-30 15:17:12 -04003124 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003125
Jamie Madill7538f7f2014-04-17 11:53:39 -04003126 PackPixelsParams packParams(safeArea, format, type, outputPitch, pack, 0);
3127 packPixels(stagingTex, packParams, pixels);
3128
3129 SafeRelease(stagingTex);
3130}
3131
3132void Renderer11::packPixels(ID3D11Texture2D *readTexture, const PackPixelsParams &params, void *pixelsOut)
3133{
3134 D3D11_TEXTURE2D_DESC textureDesc;
3135 readTexture->GetDesc(&textureDesc);
3136
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003137 D3D11_MAPPED_SUBRESOURCE mapping;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003138 HRESULT hr = mDeviceContext->Map(readTexture, 0, D3D11_MAP_READ, 0, &mapping);
Geoff Lang9cd19152014-05-28 15:54:34 -04003139 UNUSED_ASSERTION_VARIABLE(hr);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003140 ASSERT(SUCCEEDED(hr));
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003141
3142 unsigned char *source;
3143 int inputPitch;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003144 if (params.pack.reverseRowOrder)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003145 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003146 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (params.area.height - 1);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003147 inputPitch = -static_cast<int>(mapping.RowPitch);
3148 }
3149 else
3150 {
3151 source = static_cast<unsigned char*>(mapping.pData);
3152 inputPitch = static_cast<int>(mapping.RowPitch);
3153 }
3154
Geoff Lang697ad3e2013-06-04 10:11:28 -04003155 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003156
Geoff Lang005df412013-10-16 14:12:50 -04003157 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003158 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3159 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3160
3161 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3162
Jamie Madill7538f7f2014-04-17 11:53:39 -04003163 if (sourceFormat == params.format && sourceType == params.type)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003164 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003165 unsigned char *dest = static_cast<unsigned char*>(pixelsOut) + params.offset;
3166 for (int y = 0; y < params.area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003167 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003168 memcpy(dest + y * params.outputPitch, source + y * inputPitch, params.area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003169 }
3170 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003171 else
3172 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003173 GLenum destInternalFormat = gl::GetSizedInternalFormat(params.format, params.type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003174 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3175
Jamie Madill7538f7f2014-04-17 11:53:39 -04003176 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, params.format, params.type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003177 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003178 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003179 // Fast copy is possible through some special function
Jamie Madill7538f7f2014-04-17 11:53:39 -04003180 for (int y = 0; y < params.area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003181 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003182 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003183 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003184 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003185 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3186
3187 fastCopyFunc(src, dest);
3188 }
3189 }
3190 }
3191 else
3192 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003193 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003194 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(params.format, params.type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003195
3196 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3197 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3198 sizeof(temp) >= sizeof(gl::ColorUI) &&
3199 sizeof(temp) >= sizeof(gl::ColorI));
3200
Jamie Madill7538f7f2014-04-17 11:53:39 -04003201 for (int y = 0; y < params.area.height; y++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003202 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003203 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003204 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003205 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003206 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3207
3208 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3209 // will not allow the copy otherwise.
3210 readFunc(src, temp);
3211 writeFunc(temp, dest);
3212 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003213 }
3214 }
3215 }
3216
Jamie Madill7538f7f2014-04-17 11:53:39 -04003217 mDeviceContext->Unmap(readTexture, 0);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003218}
3219
Geoff Lang758d5b22013-06-11 11:42:50 -04003220bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003221 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3222 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003223{
Geoff Lang975af372013-06-12 11:19:22 -04003224 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3225 // it should never be the case that both color and depth/stencil need to be blitted at
3226 // at the same time.
3227 ASSERT(colorBlit != (depthBlit || stencilBlit));
3228
Geoff Langc1f51be2013-06-11 11:49:14 -04003229 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003230
Geoff Lang4d782732013-07-22 10:44:18 -04003231 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3232 if (!drawRenderTarget)
3233 {
3234 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3235 return gl::error(GL_OUT_OF_MEMORY, false);
3236 }
3237
3238 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3239 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3240 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3241 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3242
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003243 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3244 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003245 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003246 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003247 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003248 }
3249
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003250 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003251 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003252 unsigned int readSubresource = 0;
3253 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003254 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003255 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3256 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003257
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003258 if (unresolvedTexture)
3259 {
3260 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3261 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003262
Geoff Langea228632013-07-30 15:17:12 -04003263 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003264
Geoff Lang5c9a29a2014-02-11 10:26:24 -05003265 HRESULT hresult = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3266 if (FAILED(hresult))
Geoff Langc1f51be2013-06-11 11:49:14 -04003267 {
Geoff Langea228632013-07-30 15:17:12 -04003268 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003269 return gl::error(GL_OUT_OF_MEMORY, false);
3270 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003271 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003272 }
3273 else
3274 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003275 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003276 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003277 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003278 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003279 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003280 }
3281
Geoff Lang4d782732013-07-22 10:44:18 -04003282 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003283 {
Geoff Lang4d782732013-07-22 10:44:18 -04003284 SafeRelease(readTexture);
3285 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003286 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003287 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003288 }
3289
Geoff Lang125deab2013-08-09 13:34:16 -04003290 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3291 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3292
3293 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3294
3295 bool wholeBufferCopy = !scissorNeeded &&
3296 readRect.x == 0 && readRect.width == readSize.width &&
3297 readRect.y == 0 && readRect.height == readSize.height &&
3298 drawRect.x == 0 && drawRect.width == drawSize.width &&
3299 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003300
Geoff Langc1f51be2013-06-11 11:49:14 -04003301 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003302
Geoff Lang1cd1b212014-02-11 09:42:27 -05003303 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003304
3305 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3306 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3307 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3308 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3309
3310 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3311 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3312 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3313
Geoff Langc1f51be2013-06-11 11:49:14 -04003314 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003315 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3316 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003317 {
Geoff Lang125deab2013-08-09 13:34:16 -04003318 UINT dstX = drawRect.x;
3319 UINT dstY = drawRect.y;
3320
Geoff Langc1f51be2013-06-11 11:49:14 -04003321 D3D11_BOX readBox;
3322 readBox.left = readRect.x;
3323 readBox.right = readRect.x + readRect.width;
3324 readBox.top = readRect.y;
3325 readBox.bottom = readRect.y + readRect.height;
3326 readBox.front = 0;
3327 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003328
Geoff Lang125deab2013-08-09 13:34:16 -04003329 if (scissorNeeded)
3330 {
3331 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3332 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3333
3334 if (drawRect.x < scissor->x)
3335 {
3336 dstX = scissor->x;
3337 readBox.left += (scissor->x - drawRect.x);
3338 }
3339 if (drawRect.y < scissor->y)
3340 {
3341 dstY = scissor->y;
3342 readBox.top += (scissor->y - drawRect.y);
3343 }
3344 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3345 {
3346 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3347 }
3348 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3349 {
3350 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3351 }
3352 }
3353
Geoff Langc1f51be2013-06-11 11:49:14 -04003354 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3355 // We also require complete framebuffer copies for depth-stencil blit.
3356 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003357
Geoff Lang125deab2013-08-09 13:34:16 -04003358 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003359 readTexture, readSubresource, pSrcBox);
3360 result = true;
3361 }
3362 else
3363 {
3364 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003365 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003366
Geoff Lang975af372013-06-12 11:19:22 -04003367 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003368 {
Geoff Lang975af372013-06-12 11:19:22 -04003369 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003370 drawTexture, drawSubresource, drawArea, drawSize,
3371 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003372 }
3373 else if (depthBlit)
3374 {
Geoff Lang125deab2013-08-09 13:34:16 -04003375 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3376 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003377 }
3378 else if (stencilBlit)
3379 {
3380 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003381 drawTexture, drawSubresource, drawArea, drawSize,
3382 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003383 }
3384 else
3385 {
Geoff Lang685806d2013-06-12 11:16:36 -04003386 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003387 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3388 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003389 }
3390 }
3391
3392 SafeRelease(readTexture);
3393 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003394
3395 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003396}
3397
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003398ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3399{
3400 D3D11_TEXTURE2D_DESC textureDesc;
3401 source->GetDesc(&textureDesc);
3402
3403 if (textureDesc.SampleDesc.Count > 1)
3404 {
3405 D3D11_TEXTURE2D_DESC resolveDesc;
3406 resolveDesc.Width = textureDesc.Width;
3407 resolveDesc.Height = textureDesc.Height;
3408 resolveDesc.MipLevels = 1;
3409 resolveDesc.ArraySize = 1;
3410 resolveDesc.Format = textureDesc.Format;
3411 resolveDesc.SampleDesc.Count = 1;
3412 resolveDesc.SampleDesc.Quality = 0;
3413 resolveDesc.Usage = textureDesc.Usage;
3414 resolveDesc.BindFlags = textureDesc.BindFlags;
3415 resolveDesc.CPUAccessFlags = 0;
3416 resolveDesc.MiscFlags = 0;
3417
3418 ID3D11Texture2D *resolveTexture = NULL;
3419 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3420 if (FAILED(result))
3421 {
3422 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3423 return NULL;
3424 }
3425
3426 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3427 return resolveTexture;
3428 }
3429 else
3430 {
3431 source->AddRef();
3432 return source;
3433 }
3434}
3435
Jamie Madill3c7fa222014-06-05 13:08:51 -04003436void Renderer11::invalidateFBOAttachmentSwizzles(gl::FramebufferAttachment *attachment, int mipLevel)
Geoff Lang42477a42013-09-17 17:07:02 -04003437{
Jamie Madill3c7fa222014-06-05 13:08:51 -04003438 ASSERT(attachment->isTexture());
3439 TextureStorage *texStorage = attachment->getTextureStorage();
Geoff Lang42477a42013-09-17 17:07:02 -04003440 if (texStorage)
3441 {
3442 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3443 if (!texStorage11)
3444 {
3445 ERR("texture storage pointer unexpectedly null.");
3446 return;
3447 }
3448
3449 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3450 }
3451}
3452
3453void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3454{
3455 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3456 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003457 gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(colorAttachment);
3458 if (attachment && attachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003459 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003460 invalidateFBOAttachmentSwizzles(attachment, framebuffer->getColorbufferMipLevel(colorAttachment));
Geoff Lang42477a42013-09-17 17:07:02 -04003461 }
3462 }
3463
Jamie Madill3c7fa222014-06-05 13:08:51 -04003464 gl::FramebufferAttachment *depthAttachment = framebuffer->getDepthbuffer();
3465 if (depthAttachment && depthAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003466 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003467 invalidateFBOAttachmentSwizzles(depthAttachment, framebuffer->getDepthbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003468 }
3469
Jamie Madill3c7fa222014-06-05 13:08:51 -04003470 gl::FramebufferAttachment *stencilAttachment = framebuffer->getStencilbuffer();
3471 if (stencilAttachment && stencilAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003472 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003473 invalidateFBOAttachmentSwizzles(stencilAttachment, framebuffer->getStencilbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003474 }
3475}
3476
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003477bool Renderer11::getLUID(LUID *adapterLuid) const
3478{
3479 adapterLuid->HighPart = 0;
3480 adapterLuid->LowPart = 0;
3481
3482 if (!mDxgiAdapter)
3483 {
3484 return false;
3485 }
3486
3487 DXGI_ADAPTER_DESC adapterDesc;
3488 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3489 {
3490 return false;
3491 }
3492
3493 *adapterLuid = adapterDesc.AdapterLuid;
3494 return true;
3495}
3496
Geoff Lang005df412013-10-16 14:12:50 -04003497GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003498{
3499 int clientVersion = getCurrentClientVersion();
3500 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3501}
3502
Jamie Madill95ffb862014-01-29 09:26:59 -05003503rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3504{
3505 return gl_d3d11::GetVertexConversionType(vertexFormat);
3506}
3507
3508GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3509{
3510 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3511}
3512
Geoff Lang61e49a52013-05-29 10:22:58 -04003513Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3514{
3515 MultisampleSupportInfo supportInfo = { 0 };
3516
3517 UINT formatSupport;
3518 HRESULT result;
3519
3520 result = mDevice->CheckFormatSupport(format, &formatSupport);
3521 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3522 {
3523 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3524 {
3525 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3526 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3527 {
3528 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3529 }
3530 else
3531 {
3532 supportInfo.qualityLevels[i - 1] = 0;
3533 }
3534 }
3535 }
3536
3537 return supportInfo;
3538}
3539
Geoff Langcec35902014-04-16 10:52:36 -04003540gl::Caps Renderer11::generateCaps() const
3541{
3542 return d3d11_gl::GenerateCaps(mDevice);
3543}
3544
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003545}