blob: ddb623633d3420d668de940912575a7a36ea5f30 [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"
Jamie Madille261b442014-06-25 12:42:21 -040013#include "libGLESv2/FramebufferAttachment.h"
daniel@transgaming.com53670042012-11-28 20:55:51 +000014#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com80fc3322012-11-28 21:02:13 +000015#include "libGLESv2/Framebuffer.h"
Brandon Jonesc7a41042014-06-23 12:03:25 -070016#include "libGLESv2/renderer/d3d/d3d11/Renderer11.h"
17#include "libGLESv2/renderer/d3d/d3d11/RenderTarget11.h"
18#include "libGLESv2/renderer/d3d/d3d11/renderer11_utils.h"
19#include "libGLESv2/renderer/d3d/d3d11/formatutils11.h"
20#include "libGLESv2/renderer/d3d/d3d11/ShaderExecutable11.h"
21#include "libGLESv2/renderer/d3d/d3d11/SwapChain11.h"
22#include "libGLESv2/renderer/d3d/d3d11/Image11.h"
23#include "libGLESv2/renderer/d3d/d3d11/VertexBuffer11.h"
24#include "libGLESv2/renderer/d3d/d3d11/IndexBuffer11.h"
Brandon Jonesd38f9262014-06-18 16:26:45 -070025#include "libGLESv2/renderer/d3d/d3d11/Buffer11.h"
Brandon Jonesc7a41042014-06-23 12:03:25 -070026#include "libGLESv2/renderer/d3d/VertexDataManager.h"
27#include "libGLESv2/renderer/d3d/IndexDataManager.h"
28#include "libGLESv2/renderer/d3d/d3d11/TextureStorage11.h"
29#include "libGLESv2/renderer/d3d/d3d11/Query11.h"
30#include "libGLESv2/renderer/d3d/d3d11/Fence11.h"
31#include "libGLESv2/renderer/d3d/d3d11/Blit11.h"
32#include "libGLESv2/renderer/d3d/d3d11/Clear11.h"
33#include "libGLESv2/renderer/d3d/d3d11/PixelTransfer11.h"
34#include "libGLESv2/renderer/d3d/d3d11/VertexArray11.h"
Brandon Jonesd38f9262014-06-18 16:26:45 -070035#include "libGLESv2/renderer/d3d/d3d11/Buffer11.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000036#include "libEGL/Display.h"
37
Geoff Lang94a90892014-02-18 17:14:19 -050038// Enable ANGLE_SKIP_DXGI_1_2_CHECK if there is not a possibility of using cross-process
39// HWNDs or the Windows 7 Platform Update (KB2670838) is expected to be installed.
40#ifndef ANGLE_SKIP_DXGI_1_2_CHECK
41#define ANGLE_SKIP_DXGI_1_2_CHECK 0
42#endif
43
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000044#ifdef _DEBUG
45// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
46// and conformance tests. to enable all warnings, remove this define.
47#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
48#endif
49
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000050namespace rx
51{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000052static const DXGI_FORMAT RenderTargetFormats[] =
53 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000054 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000055 DXGI_FORMAT_R8G8B8A8_UNORM
56 };
57
58static const DXGI_FORMAT DepthStencilFormats[] =
59 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000060 DXGI_FORMAT_UNKNOWN,
61 DXGI_FORMAT_D24_UNORM_S8_UINT,
62 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000063 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000064
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000065enum
66{
67 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
68};
69
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000070Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
71{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000072 mVertexDataManager = NULL;
73 mIndexDataManager = NULL;
74
daniel@transgaming.comc5114302012-12-20 21:11:36 +000075 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000076 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000077
Geoff Langb86b9792013-06-04 16:32:05 -040078 mBlit = NULL;
Jamie Madilla21eea32013-09-18 14:36:25 -040079 mPixelTransfer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000080
Geoff Langda507fe2013-08-20 12:01:42 -040081 mClear = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000082
daniel@transgaming.combdf787f2013-02-01 03:20:36 +000083 mSyncQuery = NULL;
84
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000085 mD3d11Module = NULL;
86 mDxgiModule = NULL;
87
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000088 mDeviceLost = false;
89
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +000090 mMaxSupportedSamples = 0;
91
daniel@transgaming.com25072f62012-11-28 19:31:32 +000092 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000093 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000094 mDxgiAdapter = NULL;
95 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +000096
97 mDriverConstantBufferVS = NULL;
98 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +000099
Jamie Madill6246dc82014-01-29 09:26:47 -0500100 mAppliedVertexShader = NULL;
101 mAppliedGeometryShader = NULL;
Geoff Lang4c5c6bb2014-02-05 16:32:46 -0500102 mCurPointGeometryShader = NULL;
Jamie Madill6246dc82014-01-29 09:26:47 -0500103 mAppliedPixelShader = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000104}
105
106Renderer11::~Renderer11()
107{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000108 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000109}
110
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000111Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
112{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000113 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000114 return static_cast<rx::Renderer11*>(renderer);
115}
116
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000117#ifndef __d3d11_1_h__
118#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
119#endif
120
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000121EGLint Renderer11::initialize()
122{
Geoff Langdad5ed32014-02-10 12:59:17 -0500123 if (!mCompiler.initialize())
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000124 {
125 return EGL_NOT_INITIALIZED;
126 }
127
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000128 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
129 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000130
131 if (mD3d11Module == NULL || mDxgiModule == NULL)
132 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000133 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000134 return EGL_NOT_INITIALIZED;
135 }
136
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000137 // create the D3D11 device
138 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000139 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000140
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000141 if (D3D11CreateDevice == NULL)
142 {
143 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
144 return EGL_NOT_INITIALIZED;
145 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000146
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000147 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000148 {
149 D3D_FEATURE_LEVEL_11_0,
150 D3D_FEATURE_LEVEL_10_1,
151 D3D_FEATURE_LEVEL_10_0,
152 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000153
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000154 HRESULT result = S_OK;
155
156#ifdef _DEBUG
157 result = D3D11CreateDevice(NULL,
158 D3D_DRIVER_TYPE_HARDWARE,
159 NULL,
160 D3D11_CREATE_DEVICE_DEBUG,
161 featureLevels,
162 ArraySize(featureLevels),
163 D3D11_SDK_VERSION,
164 &mDevice,
165 &mFeatureLevel,
166 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000167
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000168 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000169 {
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000170 ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n");
171 }
172
173 if (!mDevice || FAILED(result))
174#endif
175 {
176 result = D3D11CreateDevice(NULL,
177 D3D_DRIVER_TYPE_HARDWARE,
178 NULL,
179 0,
180 featureLevels,
181 ArraySize(featureLevels),
182 D3D11_SDK_VERSION,
183 &mDevice,
184 &mFeatureLevel,
185 &mDeviceContext);
186
187 if (!mDevice || FAILED(result))
188 {
189 ERR("Could not create D3D11 device - aborting!\n");
190 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
191 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000192 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000193
Geoff Lang94a90892014-02-18 17:14:19 -0500194#if !ANGLE_SKIP_DXGI_1_2_CHECK
195 // In order to create a swap chain for an HWND owned by another process, DXGI 1.2 is required.
196 // The easiest way to check is to query for a IDXGIDevice2.
197 bool requireDXGI1_2 = false;
198 HWND hwnd = WindowFromDC(mDc);
199 if (hwnd)
200 {
201 DWORD currentProcessId = GetCurrentProcessId();
202 DWORD wndProcessId;
203 GetWindowThreadProcessId(hwnd, &wndProcessId);
204 requireDXGI1_2 = (currentProcessId != wndProcessId);
205 }
206 else
207 {
208 requireDXGI1_2 = true;
209 }
210
211 if (requireDXGI1_2)
212 {
213 IDXGIDevice2 *dxgiDevice2 = NULL;
214 result = mDevice->QueryInterface(__uuidof(IDXGIDevice2), (void**)&dxgiDevice2);
215 if (FAILED(result))
216 {
217 ERR("DXGI 1.2 required to present to HWNDs owned by another process.\n");
218 return EGL_NOT_INITIALIZED;
219 }
220 SafeRelease(dxgiDevice2);
221 }
222#endif
223
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000224 IDXGIDevice *dxgiDevice = NULL;
225 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
226
227 if (FAILED(result))
228 {
229 ERR("Could not query DXGI device - aborting!\n");
230 return EGL_NOT_INITIALIZED;
231 }
232
233 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
234
235 if (FAILED(result))
236 {
237 ERR("Could not retrieve DXGI adapter - aborting!\n");
238 return EGL_NOT_INITIALIZED;
239 }
240
Geoff Langea228632013-07-30 15:17:12 -0400241 SafeRelease(dxgiDevice);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000242
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000243 mDxgiAdapter->GetDesc(&mAdapterDescription);
244 memset(mDescription, 0, sizeof(mDescription));
245 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
246
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000247 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
248
249 if (!mDxgiFactory || FAILED(result))
250 {
251 ERR("Could not create DXGI factory - aborting!\n");
252 return EGL_NOT_INITIALIZED;
253 }
254
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000255 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
256#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
257 ID3D11InfoQueue *infoQueue;
258 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
259
260 if (SUCCEEDED(result))
261 {
262 D3D11_MESSAGE_ID hideMessages[] =
263 {
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000264 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000265 };
266
267 D3D11_INFO_QUEUE_FILTER filter = {0};
268 filter.DenyList.NumIDs = ArraySize(hideMessages);
269 filter.DenyList.pIDList = hideMessages;
270
271 infoQueue->AddStorageFilterEntries(&filter);
Geoff Langea228632013-07-30 15:17:12 -0400272 SafeRelease(infoQueue);
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000273 }
274#endif
275
Geoff Lang61e49a52013-05-29 10:22:58 -0400276 mMaxSupportedSamples = 0;
277
278 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
279 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000280 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400281 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
282 mMultisampleSupportMap.insert(std::make_pair(*i, support));
283 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000284 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000285
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000286 initializeDevice();
287
288 return EGL_SUCCESS;
289}
290
291// do any one-time device initialization
292// NOTE: this is also needed after a device lost/reset
293// to reset the scene status and ensure the default states are reset.
294void Renderer11::initializeDevice()
295{
Geoff Lange4a492b2014-06-19 14:14:41 -0400296 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000297 mInputLayoutCache.initialize(mDevice, mDeviceContext);
298
299 ASSERT(!mVertexDataManager && !mIndexDataManager);
300 mVertexDataManager = new VertexDataManager(this);
301 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000302
Geoff Langb86b9792013-06-04 16:32:05 -0400303 ASSERT(!mBlit);
304 mBlit = new Blit11(this);
305
Geoff Langda507fe2013-08-20 12:01:42 -0400306 ASSERT(!mClear);
307 mClear = new Clear11(this);
308
Jamie Madilla21eea32013-09-18 14:36:25 -0400309 ASSERT(!mPixelTransfer);
310 mPixelTransfer = new PixelTransfer11(this);
311
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000312 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000313}
314
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000315int Renderer11::generateConfigs(ConfigDesc **configDescList)
316{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000317 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
318 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000319 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
320 int numConfigs = 0;
321
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000322 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000323 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000324 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000325 {
326 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
327
328 UINT formatSupport = 0;
329 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000330
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000331 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
332 {
333 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
334
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000335 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000336
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000337 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
338 {
Geoff Lang5c9a29a2014-02-11 10:26:24 -0500339 UINT depthStencilSupport = 0;
340 result = mDevice->CheckFormatSupport(depthStencilFormat, &depthStencilSupport);
341 depthStencilFormatOK = SUCCEEDED(result) && (depthStencilSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000342 }
343
344 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000345 {
346 ConfigDesc newConfig;
Geoff Lange4a492b2014-06-19 14:14:41 -0400347 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat);
348 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000349 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
350 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000351 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000352
353 (*configDescList)[numConfigs++] = newConfig;
354 }
355 }
356 }
357 }
358
359 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000360}
361
362void Renderer11::deleteConfigs(ConfigDesc *configDescList)
363{
364 delete [] (configDescList);
365}
366
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000367void Renderer11::sync(bool block)
368{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000369 if (block)
370 {
371 HRESULT result;
372
373 if (!mSyncQuery)
374 {
375 D3D11_QUERY_DESC queryDesc;
376 queryDesc.Query = D3D11_QUERY_EVENT;
377 queryDesc.MiscFlags = 0;
378
379 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
380 ASSERT(SUCCEEDED(result));
381 }
382
383 mDeviceContext->End(mSyncQuery);
384 mDeviceContext->Flush();
385
386 do
387 {
388 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
389
390 // Keep polling, but allow other threads to do something useful first
391 Sleep(0);
392
393 if (testDeviceLost(true))
394 {
395 return;
396 }
397 }
398 while (result == S_FALSE);
399 }
400 else
401 {
402 mDeviceContext->Flush();
403 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000404}
405
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000406SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
407{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000408 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000409}
410
Geoff Lange2e0ce02013-09-17 17:05:08 -0400411void Renderer11::generateSwizzle(gl::Texture *texture)
412{
Geoff Lang42477a42013-09-17 17:07:02 -0400413 if (texture)
414 {
415 TextureStorageInterface *texStorage = texture->getNativeTexture();
416 if (texStorage)
417 {
418 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
419
420 storage11->generateSwizzles(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
421 texture->getSwizzleAlpha());
422 }
423 }
Geoff Lange2e0ce02013-09-17 17:05:08 -0400424}
425
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000426void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
427{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000428 if (type == gl::SAMPLER_PIXEL)
429 {
430 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
431 {
432 ERR("Pixel shader sampler index %i is not valid.", index);
433 return;
434 }
435
436 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
437 {
438 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
439
440 if (!dxSamplerState)
441 {
442 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
443 "sampler state for pixel shaders at slot %i.", index);
444 }
445
446 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
447
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000448 mCurPixelSamplerStates[index] = samplerState;
449 }
450
451 mForceSetPixelSamplerStates[index] = false;
452 }
453 else if (type == gl::SAMPLER_VERTEX)
454 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000455 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000456 {
457 ERR("Vertex shader sampler index %i is not valid.", index);
458 return;
459 }
460
461 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
462 {
463 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
464
465 if (!dxSamplerState)
466 {
467 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
468 "sampler state for vertex shaders at slot %i.", index);
469 }
470
471 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
472
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000473 mCurVertexSamplerStates[index] = samplerState;
474 }
475
476 mForceSetVertexSamplerStates[index] = false;
477 }
478 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000479}
480
481void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
482{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000483 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000484 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000485
486 if (texture)
487 {
488 TextureStorageInterface *texStorage = texture->getNativeTexture();
489 if (texStorage)
490 {
491 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
Nicolas Capensa10c1c72014-03-27 11:20:52 -0400492 gl::SamplerState samplerState;
493 texture->getSamplerState(&samplerState);
494 textureSRV = storage11->getSRV(samplerState);
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000495 }
496
497 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
498 // missing the shader resource view
499 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000500
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000501 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000502 }
503
504 if (type == gl::SAMPLER_PIXEL)
505 {
506 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
507 {
508 ERR("Pixel shader sampler index %i is not valid.", index);
509 return;
510 }
511
Geoff Lang91382e52014-01-07 16:16:30 -0500512 if (forceSetTexture || mCurPixelSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000513 {
514 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
515 }
516
Geoff Lang91382e52014-01-07 16:16:30 -0500517 mCurPixelSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000518 }
519 else if (type == gl::SAMPLER_VERTEX)
520 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000521 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000522 {
523 ERR("Vertex shader sampler index %i is not valid.", index);
524 return;
525 }
526
Geoff Lang91382e52014-01-07 16:16:30 -0500527 if (forceSetTexture || mCurVertexSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000528 {
529 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
530 }
531
Geoff Lang91382e52014-01-07 16:16:30 -0500532 mCurVertexSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000533 }
534 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000535}
536
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000537bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
538{
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000539 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
540 {
541 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
542 if (uniformBuffer)
543 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700544 Buffer11 *bufferStorage = Buffer11::makeBuffer11(uniformBuffer->getImplementation());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500545 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000546
547 if (!constantBuffer)
548 {
549 return false;
550 }
551
Geoff Langc6354ee2013-07-22 10:40:07 -0400552 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
553 {
554 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
555 1, &constantBuffer);
556 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
557 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000558 }
559 }
560
561 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
562 {
563 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
564 if (uniformBuffer)
565 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700566 Buffer11 *bufferStorage = Buffer11::makeBuffer11(uniformBuffer->getImplementation());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500567 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000568
569 if (!constantBuffer)
570 {
571 return false;
572 }
573
Geoff Langc6354ee2013-07-22 10:40:07 -0400574 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
575 {
576 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
577 1, &constantBuffer);
578 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
579 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000580 }
581 }
582
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000583 return true;
584}
585
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000586void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000587{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000588 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000589 {
Nicolas Capensaea8e942014-05-09 19:14:08 -0400590 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000591 if (!dxRasterState)
592 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000593 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000594 "rasterizer state.");
595 }
596
597 mDeviceContext->RSSetState(dxRasterState);
598
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000599 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000600 }
601
602 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000603}
604
Geoff Langc142e9d2013-09-30 15:19:47 -0400605void Renderer11::setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000606 unsigned int sampleMask)
607{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000608 if (mForceSetBlendState ||
609 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400610 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000611 sampleMask != mCurSampleMask)
612 {
Geoff Langc142e9d2013-09-30 15:19:47 -0400613 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(framebuffer, blendState);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000614 if (!dxBlendState)
615 {
616 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
617 "blend state.");
618 }
619
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000620 float blendColors[4] = {0.0f};
621 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
622 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
623 {
624 blendColors[0] = blendColor.red;
625 blendColors[1] = blendColor.green;
626 blendColors[2] = blendColor.blue;
627 blendColors[3] = blendColor.alpha;
628 }
629 else
630 {
631 blendColors[0] = blendColor.alpha;
632 blendColors[1] = blendColor.alpha;
633 blendColors[2] = blendColor.alpha;
634 blendColors[3] = blendColor.alpha;
635 }
636
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000637 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
638
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000639 mCurBlendState = blendState;
640 mCurBlendColor = blendColor;
641 mCurSampleMask = sampleMask;
642 }
643
644 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000645}
646
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000647void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000648 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000649{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000650 if (mForceSetDepthStencilState ||
651 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
652 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
653 {
Jamie Madillac528012014-06-20 13:21:23 -0400654 ASSERT(depthStencilState.stencilWritemask == depthStencilState.stencilBackWritemask);
655 ASSERT(stencilRef == stencilBackRef);
656 ASSERT(depthStencilState.stencilMask == depthStencilState.stencilBackMask);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000657
658 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
659 if (!dxDepthStencilState)
660 {
661 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
662 "setting the default depth stencil state.");
663 }
664
Jamie Madillec91cd32014-01-21 16:38:12 -0500665 // Max D3D11 stencil reference value is 0xFF, corresponding to the max 8 bits in a stencil buffer
666 // GL specifies we should clamp the ref value to the nearest bit depth when doing stencil ops
667 META_ASSERT(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF);
668 META_ASSERT(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF);
Geoff Lang0bf3a982014-02-11 09:40:48 -0500669 UINT dxStencilRef = std::min<UINT>(stencilRef, 0xFFu);
Jamie Madillec91cd32014-01-21 16:38:12 -0500670
Geoff Lang0bf3a982014-02-11 09:40:48 -0500671 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, dxStencilRef);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000672
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000673 mCurDepthStencilState = depthStencilState;
674 mCurStencilRef = stencilRef;
675 mCurStencilBackRef = stencilBackRef;
676 }
677
678 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000679}
680
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000681void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000682{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000683 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
684 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000685 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000686 if (enabled)
687 {
688 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000689 rect.left = std::max(0, scissor.x);
690 rect.top = std::max(0, scissor.y);
691 rect.right = scissor.x + std::max(0, scissor.width);
692 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000693
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000694 mDeviceContext->RSSetScissorRects(1, &rect);
695 }
696
697 if (enabled != mScissorEnabled)
698 {
699 mForceSetRasterState = true;
700 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000701
702 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000703 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000704 }
705
706 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000707}
708
daniel@transgaming.com12985182012-12-20 20:56:31 +0000709bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000710 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000711{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000712 gl::Rectangle actualViewport = viewport;
713 float actualZNear = gl::clamp01(zNear);
714 float actualZFar = gl::clamp01(zFar);
715 if (ignoreViewport)
716 {
717 actualViewport.x = 0;
718 actualViewport.y = 0;
719 actualViewport.width = mRenderTargetDesc.width;
720 actualViewport.height = mRenderTargetDesc.height;
721 actualZNear = 0.0f;
722 actualZFar = 1.0f;
723 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000724
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000725 // Get D3D viewport bounds, which depends on the feature level
726 const Range& viewportBounds = getViewportBounds();
727
728 // 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 +0000729 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000730 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
731 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
732 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
733 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
734 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
735 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000736 dxViewport.MinDepth = actualZNear;
737 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000738
739 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
740 {
741 return false; // Nothing to render
742 }
743
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000744 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
745 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000746
daniel@transgaming.com53670042012-11-28 20:55:51 +0000747 if (viewportChanged)
748 {
749 mDeviceContext->RSSetViewports(1, &dxViewport);
750
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000751 mCurViewport = actualViewport;
752 mCurNear = actualZNear;
753 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000754
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000755 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
756 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
757 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
758 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000759
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000760 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
761 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000762
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000763 mVertexConstants.depthRange[0] = actualZNear;
764 mVertexConstants.depthRange[1] = actualZFar;
765 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000766
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000767 mPixelConstants.depthRange[0] = actualZNear;
768 mPixelConstants.depthRange[1] = actualZFar;
769 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000770 }
771
772 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000773 return true;
774}
775
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000776bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
777{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000778 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000779
Geoff Lang57e713e2013-07-31 17:01:58 -0400780 GLsizei minCount = 0;
781
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000782 switch (mode)
783 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400784 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
785 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
786 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
787 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
788 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
789 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000790 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400791 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000792 default:
Jamie Madill1aeb1312014-06-20 13:21:25 -0400793 UNREACHABLE();
794 return false;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000795 }
796
Geoff Lang4c095862013-07-22 10:43:36 -0400797 if (primitiveTopology != mCurrentPrimitiveTopology)
798 {
799 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
800 mCurrentPrimitiveTopology = primitiveTopology;
801 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000802
Geoff Lang57e713e2013-07-31 17:01:58 -0400803 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000804}
805
806bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000807{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000808 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000809 // Also extract the render target dimensions and view
810 unsigned int renderTargetWidth = 0;
811 unsigned int renderTargetHeight = 0;
812 GLenum renderTargetFormat = 0;
813 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
814 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
815 bool missingColorRenderTarget = true;
816
817 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000818 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000819 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
820
821 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000822 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000823 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
824 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
825
Jamie Madill3c7fa222014-06-05 13:08:51 -0400826 gl::FramebufferAttachment *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000827
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000828 if (!colorbuffer)
829 {
830 ERR("render target pointer unexpectedly null.");
831 return false;
832 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000833
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000834 // check for zero-sized default framebuffer, which is a special case.
835 // in this case we do not wish to modify any state and just silently return false.
836 // this will not report any gl error but will cause the calling method to return.
837 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
838 {
839 return false;
840 }
841
842 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
843
844 // Extract the render target dimensions and view
845 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
846 if (!renderTarget)
847 {
848 ERR("render target pointer unexpectedly null.");
849 return false;
850 }
851
852 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
853 if (!framebufferRTVs[colorAttachment])
854 {
855 ERR("render target view pointer unexpectedly null.");
856 return false;
857 }
858
859 if (missingColorRenderTarget)
860 {
861 renderTargetWidth = colorbuffer->getWidth();
862 renderTargetHeight = colorbuffer->getHeight();
863 renderTargetFormat = colorbuffer->getActualFormat();
864 missingColorRenderTarget = false;
865 }
Jamie Madillba597af2013-10-22 13:12:15 -0400866
Geoff Lang91382e52014-01-07 16:16:30 -0500867 // TODO: Detect if this color buffer is already bound as a texture and unbind it first to prevent
868 // D3D11 warnings.
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000869 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000870 }
871
872 // Get the depth stencil render buffer and serials
Jamie Madill3c7fa222014-06-05 13:08:51 -0400873 gl::FramebufferAttachment *depthStencil = NULL;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000874 unsigned int depthbufferSerial = 0;
875 unsigned int stencilbufferSerial = 0;
876 if (framebuffer->getDepthbufferType() != GL_NONE)
877 {
878 depthStencil = framebuffer->getDepthbuffer();
879 if (!depthStencil)
880 {
881 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000882 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000883 return false;
884 }
885
886 depthbufferSerial = depthStencil->getSerial();
887 }
888 else if (framebuffer->getStencilbufferType() != GL_NONE)
889 {
890 depthStencil = framebuffer->getStencilbuffer();
891 if (!depthStencil)
892 {
893 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000894 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000895 return false;
896 }
897
898 stencilbufferSerial = depthStencil->getSerial();
899 }
900
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000901 ID3D11DepthStencilView* framebufferDSV = NULL;
902 if (depthStencil)
903 {
904 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
905 if (!depthStencilRenderTarget)
906 {
907 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000908 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000909 return false;
910 }
911
912 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
913 if (!framebufferDSV)
914 {
915 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000916 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000917 return false;
918 }
919
920 // If there is no render buffer, the width, height and format values come from
921 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000922 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000923 {
924 renderTargetWidth = depthStencil->getWidth();
925 renderTargetHeight = depthStencil->getHeight();
926 renderTargetFormat = depthStencil->getActualFormat();
927 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000928 }
929
930 // Apply the render target and depth stencil
931 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000932 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000933 depthbufferSerial != mAppliedDepthbufferSerial ||
934 stencilbufferSerial != mAppliedStencilbufferSerial)
935 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000936 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000937
938 mRenderTargetDesc.width = renderTargetWidth;
939 mRenderTargetDesc.height = renderTargetHeight;
940 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000941 mForceSetViewport = true;
942 mForceSetScissor = true;
Geoff Langc142e9d2013-09-30 15:19:47 -0400943 mForceSetBlendState = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000944
Nicolas Capensaea8e942014-05-09 19:14:08 -0400945 if (!mDepthStencilInitialized)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000946 {
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000947 mForceSetRasterState = true;
948 }
949
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000950 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
951 {
952 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
953 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000954 mAppliedDepthbufferSerial = depthbufferSerial;
955 mAppliedStencilbufferSerial = stencilbufferSerial;
956 mRenderTargetDescInitialized = true;
957 mDepthStencilInitialized = true;
958 }
959
Geoff Lang42477a42013-09-17 17:07:02 -0400960 invalidateFramebufferSwizzles(framebuffer);
961
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000962 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000963}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000964
Jamie Madill57a89722013-07-02 11:57:03 -0400965GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -0400966 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000967{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000968 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -0400969 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000970 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000971 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000972 return err;
973 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000974
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000975 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000976}
977
daniel@transgaming.com31240482012-11-28 21:06:41 +0000978GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000979{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000980 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000981
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000982 if (err == GL_NO_ERROR)
983 {
Geoff Lang7840b172014-03-13 11:20:44 -0400984 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
985
986 ID3D11Buffer *buffer = NULL;
987 DXGI_FORMAT bufferFormat = indexBuffer->getIndexFormat();
988
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000989 if (indexInfo->storage)
990 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700991 Buffer11 *storage = Buffer11::makeBuffer11(indexInfo->storage);
Geoff Lang7840b172014-03-13 11:20:44 -0400992 buffer = storage->getBuffer(BUFFER_USAGE_INDEX);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000993 }
Geoff Lang7840b172014-03-13 11:20:44 -0400994 else
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000995 {
Geoff Lang7840b172014-03-13 11:20:44 -0400996 buffer = indexBuffer->getBuffer();
997 }
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000998
Geoff Lang7840b172014-03-13 11:20:44 -0400999 if (buffer != mAppliedIB || bufferFormat != mAppliedIBFormat || indexInfo->startOffset != mAppliedIBOffset)
1000 {
1001 mDeviceContext->IASetIndexBuffer(buffer, bufferFormat, indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001002
Geoff Lang7840b172014-03-13 11:20:44 -04001003 mAppliedIB = buffer;
1004 mAppliedIBFormat = bufferFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001005 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001006 }
1007 }
1008
1009 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001010}
1011
Geoff Langeeba6e12014-02-03 13:12:30 -05001012void Renderer11::applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[])
1013{
1014 ID3D11Buffer* d3dBuffers[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1015 UINT d3dOffsets[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1016 bool requiresUpdate = false;
1017 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1018 {
1019 if (transformFeedbackBuffers[i])
1020 {
Brandon Jonesd38f9262014-06-18 16:26:45 -07001021 Buffer11 *storage = Buffer11::makeBuffer11(transformFeedbackBuffers[i]->getImplementation());
Geoff Langeeba6e12014-02-03 13:12:30 -05001022 ID3D11Buffer *buffer = storage->getBuffer(BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK);
1023
1024 d3dBuffers[i] = buffer;
1025 d3dOffsets[i] = (mAppliedTFBuffers[i] != buffer) ? static_cast<UINT>(offsets[i]) : -1;
1026 }
1027 else
1028 {
1029 d3dBuffers[i] = NULL;
1030 d3dOffsets[i] = 0;
1031 }
1032
1033 if (d3dBuffers[i] != mAppliedTFBuffers[i] || offsets[i] != mAppliedTFOffsets[i])
1034 {
1035 requiresUpdate = true;
1036 }
1037 }
1038
1039 if (requiresUpdate)
1040 {
1041 mDeviceContext->SOSetTargets(ArraySize(d3dBuffers), d3dBuffers, d3dOffsets);
1042 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1043 {
1044 mAppliedTFBuffers[i] = d3dBuffers[i];
1045 mAppliedTFOffsets[i] = offsets[i];
1046 }
1047 }
1048}
1049
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001050void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001051{
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001052 if (mode == GL_POINTS && transformFeedbackActive)
1053 {
1054 // Since point sprites are generated with a geometry shader, too many vertices will
1055 // be written if transform feedback is active. To work around this, draw only the points
1056 // with the stream out shader and no pixel shader to feed the stream out buffers and then
1057 // draw again with the point sprite geometry shader to rasterize the point sprites.
1058
1059 mDeviceContext->PSSetShader(NULL, NULL, 0);
1060
1061 if (instances > 0)
1062 {
1063 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1064 }
1065 else
1066 {
1067 mDeviceContext->Draw(count, 0);
1068 }
1069
1070 mDeviceContext->GSSetShader(mCurPointGeometryShader, NULL, 0);
1071 mDeviceContext->PSSetShader(mAppliedPixelShader, NULL, 0);
1072
1073 if (instances > 0)
1074 {
1075 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1076 }
1077 else
1078 {
1079 mDeviceContext->Draw(count, 0);
1080 }
1081
1082 mDeviceContext->GSSetShader(mAppliedGeometryShader, NULL, 0);
1083 }
1084 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001085 {
1086 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1087 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001088 else if (mode == GL_TRIANGLE_FAN)
1089 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001090 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001091 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001092 else if (instances > 0)
1093 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001094 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001095 }
1096 else
1097 {
1098 mDeviceContext->Draw(count, 0);
1099 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001100}
1101
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001102void Renderer11::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices,
1103 gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001104{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001105 if (mode == GL_LINE_LOOP)
1106 {
1107 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1108 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001109 else if (mode == GL_TRIANGLE_FAN)
1110 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001111 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1112 }
1113 else if (instances > 0)
1114 {
1115 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001116 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001117 else
1118 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001119 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001120 }
1121}
1122
1123void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1124{
1125 // Get the raw indices for an indexed draw
1126 if (type != GL_NONE && elementArrayBuffer)
1127 {
1128 gl::Buffer *indexBuffer = elementArrayBuffer;
Brandon Jonesd38f9262014-06-18 16:26:45 -07001129 BufferImpl *storage = indexBuffer->getImplementation();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001130 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001131 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001132 }
1133
1134 if (!mLineLoopIB)
1135 {
1136 mLineLoopIB = new StreamingIndexBufferInterface(this);
1137 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1138 {
1139 delete mLineLoopIB;
1140 mLineLoopIB = NULL;
1141
1142 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001143 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001144 }
1145 }
1146
Geoff Lang57e713e2013-07-31 17:01:58 -04001147 // Checked by Renderer11::applyPrimitiveType
1148 ASSERT(count >= 0);
1149
1150 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001151 {
1152 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1153 return gl::error(GL_OUT_OF_MEMORY);
1154 }
1155
Geoff Lang57e713e2013-07-31 17:01:58 -04001156 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001157 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1158 {
1159 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001160 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001161 }
1162
1163 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001164 unsigned int offset;
1165 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001166 {
1167 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001168 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001169 }
1170
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001171 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001172 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001173
1174 switch (type)
1175 {
1176 case GL_NONE: // Non-indexed draw
1177 for (int i = 0; i < count; i++)
1178 {
1179 data[i] = i;
1180 }
1181 data[count] = 0;
1182 break;
1183 case GL_UNSIGNED_BYTE:
1184 for (int i = 0; i < count; i++)
1185 {
1186 data[i] = static_cast<const GLubyte*>(indices)[i];
1187 }
1188 data[count] = static_cast<const GLubyte*>(indices)[0];
1189 break;
1190 case GL_UNSIGNED_SHORT:
1191 for (int i = 0; i < count; i++)
1192 {
1193 data[i] = static_cast<const GLushort*>(indices)[i];
1194 }
1195 data[count] = static_cast<const GLushort*>(indices)[0];
1196 break;
1197 case GL_UNSIGNED_INT:
1198 for (int i = 0; i < count; i++)
1199 {
1200 data[i] = static_cast<const GLuint*>(indices)[i];
1201 }
1202 data[count] = static_cast<const GLuint*>(indices)[0];
1203 break;
1204 default: UNREACHABLE();
1205 }
1206
1207 if (!mLineLoopIB->unmapBuffer())
1208 {
1209 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001210 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001211 }
1212
Geoff Lang7840b172014-03-13 11:20:44 -04001213 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1214 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1215 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001216
Geoff Lang7840b172014-03-13 11:20:44 -04001217 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1218 {
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001219 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001220 mAppliedIB = d3dIndexBuffer;
1221 mAppliedIBFormat = indexFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001222 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001223 }
1224
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001225 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001226}
1227
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001228void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001229{
1230 // Get the raw indices for an indexed draw
1231 if (type != GL_NONE && elementArrayBuffer)
1232 {
1233 gl::Buffer *indexBuffer = elementArrayBuffer;
Brandon Jonesd38f9262014-06-18 16:26:45 -07001234 BufferImpl *storage = indexBuffer->getImplementation();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001235 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001236 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001237 }
1238
1239 if (!mTriangleFanIB)
1240 {
1241 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1242 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1243 {
1244 delete mTriangleFanIB;
1245 mTriangleFanIB = NULL;
1246
1247 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001248 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001249 }
1250 }
1251
Geoff Lang57e713e2013-07-31 17:01:58 -04001252 // Checked by Renderer11::applyPrimitiveType
1253 ASSERT(count >= 3);
1254
Geoff Langeadfd572013-07-09 15:55:07 -04001255 const unsigned int numTris = count - 2;
1256
Geoff Lang57e713e2013-07-31 17:01:58 -04001257 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001258 {
1259 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1260 return gl::error(GL_OUT_OF_MEMORY);
1261 }
1262
1263 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001264 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1265 {
1266 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001267 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001268 }
1269
1270 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001271 unsigned int offset;
1272 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001273 {
1274 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001275 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001276 }
1277
1278 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001279 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001280
1281 switch (type)
1282 {
1283 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001284 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001285 {
1286 data[i*3 + 0] = 0;
1287 data[i*3 + 1] = i + 1;
1288 data[i*3 + 2] = i + 2;
1289 }
1290 break;
1291 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001292 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001293 {
1294 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1295 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1296 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1297 }
1298 break;
1299 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001300 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001301 {
1302 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1303 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1304 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1305 }
1306 break;
1307 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001308 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001309 {
1310 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1311 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1312 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1313 }
1314 break;
1315 default: UNREACHABLE();
1316 }
1317
1318 if (!mTriangleFanIB->unmapBuffer())
1319 {
1320 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001321 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001322 }
1323
Geoff Lang7840b172014-03-13 11:20:44 -04001324 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1325 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1326 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001327
Geoff Lang7840b172014-03-13 11:20:44 -04001328 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1329 {
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001330 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001331 mAppliedIB = d3dIndexBuffer;
1332 mAppliedIBFormat = indexFormat;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001333 mAppliedIBOffset = indexBufferOffset;
1334 }
1335
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001336 if (instances > 0)
1337 {
1338 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1339 }
1340 else
1341 {
1342 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1343 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001344}
1345
Geoff Lang04fb89a2014-06-09 15:05:36 -04001346void Renderer11::applyShaders(gl::ProgramBinary *programBinary, const gl::VertexFormat inputLayout[], const gl::Framebuffer *framebuffer,
1347 bool rasterizerDiscard, bool transformFeedbackActive)
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001348{
Jamie Madillc5a83002014-02-14 16:41:25 -05001349 ShaderExecutable *vertexExe = programBinary->getVertexExecutableForInputLayout(inputLayout);
Geoff Lang04fb89a2014-06-09 15:05:36 -04001350 ShaderExecutable *pixelExe = programBinary->getPixelExecutableForFramebuffer(framebuffer);
Jamie Madill6246dc82014-01-29 09:26:47 -05001351 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001352
Jamie Madill6246dc82014-01-29 09:26:47 -05001353 ID3D11VertexShader *vertexShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader() : NULL);
Jamie Madill6246dc82014-01-29 09:26:47 -05001354
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001355 ID3D11PixelShader *pixelShader = NULL;
1356 // Skip pixel shader if we're doing rasterizer discard.
1357 if (!rasterizerDiscard)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001358 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001359 pixelShader = (pixelExe ? ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader() : NULL);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001360 }
1361
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001362 ID3D11GeometryShader *geometryShader = NULL;
1363 if (transformFeedbackActive)
Geoff Lang0550d032014-01-30 11:29:07 -05001364 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001365 geometryShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getStreamOutShader() : NULL);
1366 }
1367 else if (mCurRasterState.pointDrawMode)
1368 {
1369 geometryShader = (geometryExe ? ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader() : NULL);
Geoff Lang0550d032014-01-30 11:29:07 -05001370 }
1371
Jamie Madill6246dc82014-01-29 09:26:47 -05001372 bool dirtyUniforms = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001373
Jamie Madill6246dc82014-01-29 09:26:47 -05001374 if (vertexShader != mAppliedVertexShader)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001375 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001376 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1377 mAppliedVertexShader = vertexShader;
1378 dirtyUniforms = true;
1379 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001380
Jamie Madill6246dc82014-01-29 09:26:47 -05001381 if (geometryShader != mAppliedGeometryShader)
1382 {
1383 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1384 mAppliedGeometryShader = geometryShader;
1385 dirtyUniforms = true;
1386 }
1387
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001388 if (geometryExe && mCurRasterState.pointDrawMode)
1389 {
1390 mCurPointGeometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
1391 }
1392 else
1393 {
1394 mCurPointGeometryShader = NULL;
1395 }
1396
Jamie Madill6246dc82014-01-29 09:26:47 -05001397 if (pixelShader != mAppliedPixelShader)
1398 {
1399 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1400 mAppliedPixelShader = pixelShader;
1401 dirtyUniforms = true;
1402 }
1403
1404 if (dirtyUniforms)
1405 {
1406 programBinary->dirtyAllUniforms();
daniel@transgaming.come4991412012-12-20 20:55:34 +00001407 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001408}
1409
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001410void Renderer11::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001411{
Jamie Madill834e8b72014-04-11 13:33:58 -04001412 const std::vector<gl::LinkedUniform*> &uniformArray = programBinary.getUniforms();
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001413
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001414 unsigned int totalRegisterCountVS = 0;
1415 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001416
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001417 bool vertexUniformsDirty = false;
1418 bool pixelUniformsDirty = false;
1419
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001420 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001421 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001422 const gl::LinkedUniform &uniform = *uniformArray[uniformIndex];
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001423
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001424 if (uniform.isReferencedByVertexShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001425 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001426 totalRegisterCountVS += uniform.registerCount;
1427 vertexUniformsDirty = (vertexUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001428 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001429
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001430 if (uniform.isReferencedByFragmentShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001431 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001432 totalRegisterCountPS += uniform.registerCount;
1433 pixelUniformsDirty = (pixelUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001434 }
1435 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001436
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001437 const UniformStorage11 *vertexUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getVertexUniformStorage());
1438 const UniformStorage11 *fragmentUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getFragmentUniformStorage());
1439 ASSERT(vertexUniformStorage);
1440 ASSERT(fragmentUniformStorage);
1441
1442 ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
1443 ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001444
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001445 float (*mapVS)[4] = NULL;
1446 float (*mapPS)[4] = NULL;
1447
Shannon Woods5ab33c82013-06-26 15:31:09 -04001448 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1449 {
1450 D3D11_MAPPED_SUBRESOURCE map = {0};
1451 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001452 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001453 ASSERT(SUCCEEDED(result));
1454 mapVS = (float(*)[4])map.pData;
1455 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001456
Shannon Woods5ab33c82013-06-26 15:31:09 -04001457 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1458 {
1459 D3D11_MAPPED_SUBRESOURCE map = {0};
1460 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001461 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001462 ASSERT(SUCCEEDED(result));
1463 mapPS = (float(*)[4])map.pData;
1464 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001465
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001466 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001467 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001468 gl::LinkedUniform *uniform = uniformArray[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001469
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001470 if (!uniform->isSampler())
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001471 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001472 unsigned int componentCount = (4 - uniform->registerElement);
1473
Jamie Madill71cc91f2013-09-18 12:51:22 -04001474 // 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 -04001475 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001476
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001477 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001478 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001479 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001480 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001481
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001482 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001483 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001484 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001485 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001486 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001487 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001488
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001489 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001490 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001491 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001492 }
1493
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001494 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001495 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001496 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001497 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001498
1499 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1500 {
1501 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1502 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1503 }
1504
1505 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1506 {
1507 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1508 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1509 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001510
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001511 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001512 if (!mDriverConstantBufferVS)
1513 {
1514 D3D11_BUFFER_DESC constantBufferDescription = {0};
1515 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1516 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1517 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1518 constantBufferDescription.CPUAccessFlags = 0;
1519 constantBufferDescription.MiscFlags = 0;
1520 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001521
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001522 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001523 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001524 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001525
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001526 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1527 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001528
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001529 if (!mDriverConstantBufferPS)
1530 {
1531 D3D11_BUFFER_DESC constantBufferDescription = {0};
1532 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1533 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1534 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1535 constantBufferDescription.CPUAccessFlags = 0;
1536 constantBufferDescription.MiscFlags = 0;
1537 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001538
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001539 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001540 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001541 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001542
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001543 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1544 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001545
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001546 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1547 {
1548 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1549 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1550 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001551
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001552 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1553 {
1554 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1555 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1556 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001557
1558 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001559 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1560 {
1561 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1562 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1563 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001564}
1565
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001566void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001567{
Geoff Langda507fe2013-08-20 12:01:42 -04001568 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001569 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001570}
1571
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001572void Renderer11::markAllStateDirty()
1573{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001574 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1575 {
1576 mAppliedRenderTargetSerials[rtIndex] = 0;
1577 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001578 mAppliedDepthbufferSerial = 0;
1579 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001580 mDepthStencilInitialized = false;
1581 mRenderTargetDescInitialized = false;
1582
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001583 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001584 {
1585 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001586 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001587 }
1588 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1589 {
1590 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001591 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001592 }
1593
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001594 mForceSetBlendState = true;
1595 mForceSetRasterState = true;
1596 mForceSetDepthStencilState = true;
1597 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001598 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001599
Geoff Lang7840b172014-03-13 11:20:44 -04001600 mAppliedIB = NULL;
1601 mAppliedIBFormat = DXGI_FORMAT_UNKNOWN;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001602 mAppliedIBOffset = 0;
1603
Jamie Madill6246dc82014-01-29 09:26:47 -05001604 mAppliedVertexShader = NULL;
1605 mAppliedGeometryShader = NULL;
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001606 mCurPointGeometryShader = NULL;
Jamie Madill6246dc82014-01-29 09:26:47 -05001607 mAppliedPixelShader = NULL;
Geoff Langeeba6e12014-02-03 13:12:30 -05001608
1609 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1610 {
1611 mAppliedTFBuffers[i] = NULL;
1612 mAppliedTFOffsets[i] = 0;
1613 }
1614
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001615 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1616 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001617
1618 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001619
1620 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1621 {
1622 mCurrentConstantBufferVS[i] = -1;
1623 mCurrentConstantBufferPS[i] = -1;
1624 }
1625
1626 mCurrentVertexConstantBuffer = NULL;
1627 mCurrentPixelConstantBuffer = NULL;
1628 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001629
1630 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001631}
1632
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001633void Renderer11::releaseDeviceResources()
1634{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001635 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001636 mInputLayoutCache.clear();
1637
Geoff Langea228632013-07-30 15:17:12 -04001638 SafeDelete(mVertexDataManager);
1639 SafeDelete(mIndexDataManager);
1640 SafeDelete(mLineLoopIB);
1641 SafeDelete(mTriangleFanIB);
1642 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001643 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001644 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001645
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001646 SafeRelease(mDriverConstantBufferVS);
1647 SafeRelease(mDriverConstantBufferPS);
1648 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001649}
1650
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001651void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001652{
1653 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001654 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001655}
1656
1657bool Renderer11::isDeviceLost()
1658{
1659 return mDeviceLost;
1660}
1661
1662// set notify to true to broadcast a message to all contexts of the device loss
1663bool Renderer11::testDeviceLost(bool notify)
1664{
1665 bool isLost = false;
1666
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001667 // GetRemovedReason is used to test if the device is removed
1668 HRESULT result = mDevice->GetDeviceRemovedReason();
1669 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001670
1671 if (isLost)
1672 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001673 // Log error if this is a new device lost event
1674 if (mDeviceLost == false)
1675 {
1676 ERR("The D3D11 device was removed: 0x%08X", result);
1677 }
1678
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001679 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001680 // we'll probably get this done again by notifyDeviceLost
1681 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001682 // Note that we don't want to clear the device loss status here
1683 // -- this needs to be done by resetDevice
1684 mDeviceLost = true;
1685 if (notify)
1686 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001687 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001688 }
1689 }
1690
1691 return isLost;
1692}
1693
1694bool Renderer11::testDeviceResettable()
1695{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001696 // determine if the device is resettable by creating a dummy device
1697 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001698
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001699 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001700 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001701 return false;
1702 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001703
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001704 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001705 {
1706 D3D_FEATURE_LEVEL_11_0,
1707 D3D_FEATURE_LEVEL_10_1,
1708 D3D_FEATURE_LEVEL_10_0,
1709 };
1710
1711 ID3D11Device* dummyDevice;
1712 D3D_FEATURE_LEVEL dummyFeatureLevel;
1713 ID3D11DeviceContext* dummyContext;
1714
1715 HRESULT result = D3D11CreateDevice(NULL,
1716 D3D_DRIVER_TYPE_HARDWARE,
1717 NULL,
1718 #if defined(_DEBUG)
1719 D3D11_CREATE_DEVICE_DEBUG,
1720 #else
1721 0,
1722 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001723 featureLevels,
1724 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001725 D3D11_SDK_VERSION,
1726 &dummyDevice,
1727 &dummyFeatureLevel,
1728 &dummyContext);
1729
1730 if (!mDevice || FAILED(result))
1731 {
1732 return false;
1733 }
1734
Geoff Langea228632013-07-30 15:17:12 -04001735 SafeRelease(dummyContext);
1736 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001737
1738 return true;
1739}
1740
1741void Renderer11::release()
1742{
1743 releaseDeviceResources();
1744
Geoff Langea228632013-07-30 15:17:12 -04001745 SafeRelease(mDxgiFactory);
1746 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001747
1748 if (mDeviceContext)
1749 {
1750 mDeviceContext->ClearState();
1751 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001752 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001753 }
1754
Geoff Langea228632013-07-30 15:17:12 -04001755 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001756
1757 if (mD3d11Module)
1758 {
1759 FreeLibrary(mD3d11Module);
1760 mD3d11Module = NULL;
1761 }
1762
1763 if (mDxgiModule)
1764 {
1765 FreeLibrary(mDxgiModule);
1766 mDxgiModule = NULL;
1767 }
Geoff Langdad5ed32014-02-10 12:59:17 -05001768
1769 mCompiler.release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001770}
1771
1772bool Renderer11::resetDevice()
1773{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001774 // recreate everything
1775 release();
1776 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001777
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001778 if (result != EGL_SUCCESS)
1779 {
1780 ERR("Could not reinitialize D3D11 device: %08X", result);
1781 return false;
1782 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001783
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001784 mDeviceLost = false;
1785
1786 return true;
1787}
1788
1789DWORD Renderer11::getAdapterVendor() const
1790{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001791 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001792}
1793
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001794std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001795{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001796 std::ostringstream rendererString;
1797
1798 rendererString << mDescription;
1799 rendererString << " Direct3D11";
1800
1801 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1802 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1803
1804 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001805}
1806
1807GUID Renderer11::getAdapterIdentifier() const
1808{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001809 // Use the adapter LUID as our adapter ID
1810 // This number is local to a machine is only guaranteed to be unique between restarts
1811 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1812 GUID adapterId = {0};
1813 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1814 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001815}
1816
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001817Range Renderer11::getViewportBounds() const
1818{
1819 switch (mFeatureLevel)
1820 {
1821 case D3D_FEATURE_LEVEL_11_0:
1822 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1823 case D3D_FEATURE_LEVEL_10_1:
1824 case D3D_FEATURE_LEVEL_10_0:
1825 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1826 default: UNREACHABLE();
1827 return Range(0, 0);
1828 }
1829}
1830
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001831unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001832{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001833 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1834 switch (mFeatureLevel)
1835 {
1836 case D3D_FEATURE_LEVEL_11_0:
1837 case D3D_FEATURE_LEVEL_10_1:
1838 case D3D_FEATURE_LEVEL_10_0:
1839 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1840 default: UNREACHABLE();
1841 return 0;
1842 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001843}
1844
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001845unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1846{
1847 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1848}
1849
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001850unsigned int Renderer11::getReservedVertexUniformVectors() const
1851{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001852 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001853}
1854
1855unsigned int Renderer11::getReservedFragmentUniformVectors() const
1856{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001857 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001858}
1859
1860unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001861{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001862 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1863 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1864 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001865}
1866
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001867unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001868{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001869 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1870 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1871 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001872}
1873
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001874unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001875{
1876 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001877 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1878 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001879 switch (mFeatureLevel)
1880 {
1881 case D3D_FEATURE_LEVEL_11_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001882 return D3D11_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001883 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001884 return D3D10_1_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001885 case D3D_FEATURE_LEVEL_10_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001886 return D3D10_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001887 default: UNREACHABLE();
1888 return 0;
1889 }
1890}
1891
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001892unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
1893{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001894 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1895 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1896
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001897 switch (mFeatureLevel)
1898 {
1899 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001900 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001901 case D3D_FEATURE_LEVEL_10_1:
1902 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001903 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001904 default: UNREACHABLE();
1905 return 0;
1906 }
1907}
1908
1909unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
1910{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001911 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1912 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1913
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001914 switch (mFeatureLevel)
1915 {
1916 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001917 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001918 case D3D_FEATURE_LEVEL_10_1:
1919 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001920 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001921 default: UNREACHABLE();
1922 return 0;
1923 }
1924}
1925
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001926unsigned int Renderer11::getReservedVertexUniformBuffers() const
1927{
1928 // we reserve one buffer for the application uniforms, and one for driver uniforms
1929 return 2;
1930}
1931
1932unsigned int Renderer11::getReservedFragmentUniformBuffers() const
1933{
1934 // we reserve one buffer for the application uniforms, and one for driver uniforms
1935 return 2;
1936}
1937
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001938unsigned int Renderer11::getReservedVaryings() const
1939{
Jamie Madill2bf8b372014-06-16 17:18:51 -04001940 // We potentially reserve varyings for gl_Position, dx_Position, gl_FragCoord and gl_PointSize
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001941 return 4;
1942}
1943
1944
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001945unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
1946{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001947 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
1948 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
1949
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001950 switch (mFeatureLevel)
1951 {
1952 case D3D_FEATURE_LEVEL_11_0:
1953 return D3D11_SO_BUFFER_SLOT_COUNT;
1954 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001955 return D3D10_1_SO_BUFFER_SLOT_COUNT;
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001956 case D3D_FEATURE_LEVEL_10_0:
1957 return D3D10_SO_BUFFER_SLOT_COUNT;
1958 default: UNREACHABLE();
1959 return 0;
1960 }
1961}
1962
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001963unsigned int Renderer11::getMaxTransformFeedbackSeparateComponents() const
1964{
1965 switch (mFeatureLevel)
1966 {
1967 case D3D_FEATURE_LEVEL_11_0:
1968 return getMaxTransformFeedbackInterleavedComponents() / getMaxTransformFeedbackBuffers();
1969 case D3D_FEATURE_LEVEL_10_1:
1970 case D3D_FEATURE_LEVEL_10_0:
1971 // D3D 10 and 10.1 only allow one output per output slot if an output slot other than zero
1972 // is used.
1973 return 4;
1974 default: UNREACHABLE();
1975 return 0;
1976 }
1977}
1978
1979unsigned int Renderer11::getMaxTransformFeedbackInterleavedComponents() const
1980{
1981 return (getMaxVaryingVectors() * 4);
1982}
1983
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00001984unsigned int Renderer11::getMaxUniformBufferSize() const
1985{
1986 // Each component is a 4-element vector of 4-byte units (floats)
1987 const unsigned int bytesPerComponent = 4 * sizeof(float);
1988
1989 switch (mFeatureLevel)
1990 {
1991 case D3D_FEATURE_LEVEL_11_0:
1992 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1993 case D3D_FEATURE_LEVEL_10_1:
1994 case D3D_FEATURE_LEVEL_10_0:
1995 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1996 default: UNREACHABLE();
1997 return 0;
1998 }
1999}
2000
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002001bool Renderer11::getShareHandleSupport() const
2002{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002003 // We only currently support share handles with BGRA surfaces, because
2004 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002005 // PIX doesn't seem to support using share handles, so disable them.
Geoff Langcec35902014-04-16 10:52:36 -04002006 return getCaps().extensions.textureFormatBGRA8888 && !gl::perfActive();
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002007}
2008
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002009bool Renderer11::getPostSubBufferSupport() const
2010{
2011 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2012 return false;
2013}
2014
Jamie Madill13a2f852013-12-11 16:35:08 -05002015int Renderer11::getMaxRecommendedElementsIndices() const
2016{
2017 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2018 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2019
2020 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2021 return std::numeric_limits<GLint>::max();
2022}
2023
2024int Renderer11::getMaxRecommendedElementsVertices() const
2025{
2026 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2027 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2028
2029 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2030 return std::numeric_limits<GLint>::max();
2031}
2032
Geoff Lang05b05022014-06-11 15:31:45 -04002033bool Renderer11::getSRGBTextureSupport() const
2034{
2035 return true;
2036}
2037
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002038int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002039{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002040 switch (mFeatureLevel)
2041 {
2042 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002043 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002044 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2045 default: UNREACHABLE(); return 0;
2046 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002047}
2048
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002049int Renderer11::getMinorShaderModel() const
2050{
2051 switch (mFeatureLevel)
2052 {
2053 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2054 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2055 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2056 default: UNREACHABLE(); return 0;
2057 }
2058}
2059
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002060float Renderer11::getMaxPointSize() const
2061{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002062 // choose a reasonable maximum. we enforce this in the shader.
2063 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2064 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002065}
2066
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002067int Renderer11::getMaxViewportDimension() const
2068{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002069 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2070 // In our case return the maximum texture size, which is the maximum render buffer size.
2071 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2072 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2073
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002074 switch (mFeatureLevel)
2075 {
2076 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002077 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002078 case D3D_FEATURE_LEVEL_10_1:
2079 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002080 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002081 default: UNREACHABLE();
2082 return 0;
2083 }
2084}
2085
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002086int Renderer11::getMaxTextureWidth() const
2087{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002088 switch (mFeatureLevel)
2089 {
2090 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2091 case D3D_FEATURE_LEVEL_10_1:
2092 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2093 default: UNREACHABLE(); return 0;
2094 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002095}
2096
2097int Renderer11::getMaxTextureHeight() const
2098{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002099 switch (mFeatureLevel)
2100 {
2101 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2102 case D3D_FEATURE_LEVEL_10_1:
2103 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2104 default: UNREACHABLE(); return 0;
2105 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002106}
2107
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002108int Renderer11::getMaxTextureDepth() const
2109{
2110 switch (mFeatureLevel)
2111 {
2112 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2113 case D3D_FEATURE_LEVEL_10_1:
2114 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2115 default: UNREACHABLE(); return 0;
2116 }
2117}
2118
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002119int Renderer11::getMaxTextureArrayLayers() const
2120{
2121 switch (mFeatureLevel)
2122 {
2123 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2124 case D3D_FEATURE_LEVEL_10_1:
2125 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2126 default: UNREACHABLE(); return 0;
2127 }
2128}
2129
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002130int Renderer11::getMinSwapInterval() const
2131{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002132 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002133}
2134
2135int Renderer11::getMaxSwapInterval() const
2136{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002137 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002138}
2139
2140int Renderer11::getMaxSupportedSamples() const
2141{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002142 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002143}
2144
Geoff Lang005df412013-10-16 14:12:50 -04002145GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002146{
Geoff Lange4a492b2014-06-19 14:14:41 -04002147 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat);
Geoff Lang0e120e32013-05-29 10:23:55 -04002148 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2149 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2150}
2151
Geoff Lang005df412013-10-16 14:12:50 -04002152GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002153{
2154 unsigned int numCounts = 0;
2155
2156 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Lange4a492b2014-06-19 14:14:41 -04002157 GLenum componentType = gl::GetComponentType(internalFormat);
Geoff Langb2f3d052013-08-13 12:49:27 -04002158 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002159 {
Geoff Lange4a492b2014-06-19 14:14:41 -04002160 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat);
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002161 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2162
2163 if (iter != mMultisampleSupportMap.end())
2164 {
2165 const MultisampleSupportInfo& info = iter->second;
2166 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2167 {
2168 if (info.qualityLevels[i] > 0)
2169 {
2170 numCounts++;
2171 }
2172 }
2173 }
2174 }
2175
2176 return numCounts;
2177}
2178
Geoff Lang005df412013-10-16 14:12:50 -04002179void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002180{
2181 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Lange4a492b2014-06-19 14:14:41 -04002182 GLenum componentType = gl::GetComponentType(internalFormat);
Geoff Langb2f3d052013-08-13 12:49:27 -04002183 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2184 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002185 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002186 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002187
Geoff Lange4a492b2014-06-19 14:14:41 -04002188 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat);
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002189 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2190
2191 if (iter != mMultisampleSupportMap.end())
2192 {
2193 const MultisampleSupportInfo& info = iter->second;
2194 int bufPos = 0;
2195 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2196 {
2197 if (info.qualityLevels[i] > 0)
2198 {
2199 params[bufPos++] = i + 1;
2200 }
2201 }
2202 }
2203}
2204
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002205int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2206{
2207 if (requested == 0)
2208 {
2209 return 0;
2210 }
2211
2212 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2213 if (iter != mMultisampleSupportMap.end())
2214 {
2215 const MultisampleSupportInfo& info = iter->second;
2216 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2217 {
2218 if (info.qualityLevels[i] > 0)
2219 {
2220 return i + 1;
2221 }
2222 }
2223 }
2224
2225 return -1;
2226}
2227
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002228unsigned int Renderer11::getMaxRenderTargets() const
2229{
Geoff Lang4ace4232014-06-18 19:12:48 -04002230 return d3d11::GetMaximumSimultaneousRenderTargets(mFeatureLevel);
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002231}
2232
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002233bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002234{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002235 if (source && dest)
2236 {
2237 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2238 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2239
Nicolas Capens76b258f2014-04-03 10:59:42 -04002240 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002241
2242 dest11->invalidateSwizzleCache();
2243
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002244 return true;
2245 }
2246
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002247 return false;
2248}
2249
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002250bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002251{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002252 if (source && dest)
2253 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002254 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2255 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002256
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
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002267bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2268{
2269 if (source && dest)
2270 {
2271 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2272 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2273
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
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002278 return true;
2279 }
2280
2281 return false;
2282}
2283
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002284bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2285{
2286 if (source && dest)
2287 {
2288 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2289 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(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.com6c86bd52013-04-13 03:45:45 +00002295 return true;
2296 }
2297
2298 return false;
2299}
2300
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002301bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002302 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002303{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002304 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002305 if (!colorbuffer)
2306 {
2307 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002308 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002309 }
2310
2311 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2312 if (!sourceRenderTarget)
2313 {
2314 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002315 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002316 }
2317
2318 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2319 if (!source)
2320 {
2321 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002322 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002323 }
2324
2325 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2326 if (!storage11)
2327 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002328 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002329 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002330 }
2331
2332 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2333 if (!destRenderTarget)
2334 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002335 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002336 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002337 }
2338
2339 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2340 if (!dest)
2341 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002342 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002343 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002344 }
2345
Geoff Langb86b9792013-06-04 16:32:05 -04002346 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2347 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002348
Geoff Langb86b9792013-06-04 16:32:05 -04002349 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2350 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002351
Geoff Langb86b9792013-06-04 16:32:05 -04002352 // Use nearest filtering because source and destination are the same size for the direct
2353 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002354 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002355 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002356
Geoff Lang42477a42013-09-17 17:07:02 -04002357 storage11->invalidateSwizzleCacheLevel(level);
2358
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002359 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002360}
2361
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002362bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002363 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002364{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002365 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002366 if (!colorbuffer)
2367 {
2368 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002369 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002370 }
2371
2372 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2373 if (!sourceRenderTarget)
2374 {
2375 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002376 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002377 }
2378
2379 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2380 if (!source)
2381 {
2382 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002383 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002384 }
2385
2386 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2387 if (!storage11)
2388 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002389 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002390 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002391 }
2392
Nicolas Capensb13f8662013-06-04 13:30:19 -04002393 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002394 if (!destRenderTarget)
2395 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002396 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002397 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002398 }
2399
2400 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2401 if (!dest)
2402 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002403 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002404 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002405 }
2406
Geoff Langb86b9792013-06-04 16:32:05 -04002407 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2408 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002409
Geoff Langb86b9792013-06-04 16:32:05 -04002410 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2411 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002412
Geoff Langb86b9792013-06-04 16:32:05 -04002413 // Use nearest filtering because source and destination are the same size for the direct
2414 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002415 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002416 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002417
Geoff Lang42477a42013-09-17 17:07:02 -04002418 storage11->invalidateSwizzleCacheLevel(level);
2419
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002420 return ret;
2421}
2422
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002423bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2424 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2425{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002426 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002427 if (!colorbuffer)
2428 {
2429 ERR("Failed to retrieve the color buffer from the frame buffer.");
2430 return gl::error(GL_OUT_OF_MEMORY, false);
2431 }
2432
2433 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2434 if (!sourceRenderTarget)
2435 {
2436 ERR("Failed to retrieve the render target from the frame buffer.");
2437 return gl::error(GL_OUT_OF_MEMORY, false);
2438 }
2439
2440 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2441 if (!source)
2442 {
2443 ERR("Failed to retrieve the render target view from the render target.");
2444 return gl::error(GL_OUT_OF_MEMORY, false);
2445 }
2446
2447 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2448 if (!storage11)
2449 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002450 ERR("Failed to retrieve the texture storage from the destination.");
2451 return gl::error(GL_OUT_OF_MEMORY, false);
2452 }
2453
2454 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2455 if (!destRenderTarget)
2456 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002457 ERR("Failed to retrieve the render target from the destination storage.");
2458 return gl::error(GL_OUT_OF_MEMORY, false);
2459 }
2460
2461 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2462 if (!dest)
2463 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002464 ERR("Failed to retrieve the render target view from the destination render target.");
2465 return gl::error(GL_OUT_OF_MEMORY, false);
2466 }
2467
Geoff Langb86b9792013-06-04 16:32:05 -04002468 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2469 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002470
Geoff Langb86b9792013-06-04 16:32:05 -04002471 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2472 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002473
Geoff Langb86b9792013-06-04 16:32:05 -04002474 // Use nearest filtering because source and destination are the same size for the direct
2475 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002476 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002477 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002478
Geoff Lang42477a42013-09-17 17:07:02 -04002479 storage11->invalidateSwizzleCacheLevel(level);
2480
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002481 return ret;
2482}
2483
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002484bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2485 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2486{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002487 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002488 if (!colorbuffer)
2489 {
2490 ERR("Failed to retrieve the color buffer from the frame buffer.");
2491 return gl::error(GL_OUT_OF_MEMORY, false);
2492 }
2493
2494 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2495 if (!sourceRenderTarget)
2496 {
2497 ERR("Failed to retrieve the render target from the frame buffer.");
2498 return gl::error(GL_OUT_OF_MEMORY, false);
2499 }
2500
2501 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2502 if (!source)
2503 {
2504 ERR("Failed to retrieve the render target view from the render target.");
2505 return gl::error(GL_OUT_OF_MEMORY, false);
2506 }
2507
2508 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2509 if (!storage11)
2510 {
Geoff Langea228632013-07-30 15:17:12 -04002511 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002512 ERR("Failed to retrieve the texture storage from the destination.");
2513 return gl::error(GL_OUT_OF_MEMORY, false);
2514 }
2515
2516 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2517 if (!destRenderTarget)
2518 {
Geoff Langea228632013-07-30 15:17:12 -04002519 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002520 ERR("Failed to retrieve the render target from the destination storage.");
2521 return gl::error(GL_OUT_OF_MEMORY, false);
2522 }
2523
2524 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2525 if (!dest)
2526 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002527 ERR("Failed to retrieve the render target view from the destination render target.");
2528 return gl::error(GL_OUT_OF_MEMORY, false);
2529 }
2530
Geoff Langb86b9792013-06-04 16:32:05 -04002531 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2532 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002533
Geoff Langb86b9792013-06-04 16:32:05 -04002534 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2535 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002536
Geoff Langb86b9792013-06-04 16:32:05 -04002537 // Use nearest filtering because source and destination are the same size for the direct
2538 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002539 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002540 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002541
Geoff Lang42477a42013-09-17 17:07:02 -04002542 storage11->invalidateSwizzleCacheLevel(level);
2543
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002544 return ret;
2545}
2546
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002547void Renderer11::unapplyRenderTargets()
2548{
2549 setOneTimeRenderTarget(NULL);
2550}
2551
2552void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2553{
2554 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2555
2556 rtvArray[0] = renderTargetView;
2557
2558 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2559
2560 // Do not preserve the serial for this one-time-use render target
2561 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2562 {
2563 mAppliedRenderTargetSerials[rtIndex] = 0;
2564 }
2565}
2566
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002567RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2568{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002569 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002570 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002571
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002572 if (depth)
2573 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002574 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002575 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002576 swapChain11->getDepthStencilTexture(),
2577 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002578 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002579 }
2580 else
2581 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002582 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002583 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002584 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002585 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002586 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002587 }
2588 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002589}
2590
Geoff Langa2d97f12013-06-11 11:44:02 -04002591RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002592{
Geoff Langa2d97f12013-06-11 11:44:02 -04002593 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002594 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002595}
2596
Geoff Lang48dcae72014-02-05 16:28:24 -05002597ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type,
2598 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2599 bool separatedOutputBuffers)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002600{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002601 ShaderExecutable11 *executable = NULL;
Geoff Lang48dcae72014-02-05 16:28:24 -05002602 HRESULT result;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002603
2604 switch (type)
2605 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002606 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002607 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002608 ID3D11VertexShader *vertexShader = NULL;
2609 ID3D11GeometryShader *streamOutShader = NULL;
2610
2611 result = mDevice->CreateVertexShader(function, length, NULL, &vertexShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002612 ASSERT(SUCCEEDED(result));
2613
Geoff Lang48dcae72014-02-05 16:28:24 -05002614 if (transformFeedbackVaryings.size() > 0)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002615 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002616 std::vector<D3D11_SO_DECLARATION_ENTRY> soDeclaration;
2617 for (size_t i = 0; i < transformFeedbackVaryings.size(); i++)
2618 {
2619 const gl::LinkedVarying &varying = transformFeedbackVaryings[i];
Jamie Madill6195ef82014-06-18 10:09:43 -04002620 GLenum transposedType = gl::TransposeMatrixType(varying.type);
2621
Geoff Langcebb5aa2014-04-07 14:13:40 -04002622 for (size_t j = 0; j < varying.semanticIndexCount; j++)
Geoff Lang48dcae72014-02-05 16:28:24 -05002623 {
2624 D3D11_SO_DECLARATION_ENTRY entry = { 0 };
2625 entry.Stream = 0;
Geoff Langcebb5aa2014-04-07 14:13:40 -04002626 entry.SemanticName = varying.semanticName.c_str();
2627 entry.SemanticIndex = varying.semanticIndex + j;
Geoff Lang48dcae72014-02-05 16:28:24 -05002628 entry.StartComponent = 0;
Jamie Madill6195ef82014-06-18 10:09:43 -04002629 entry.ComponentCount = gl::VariableColumnCount(transposedType);
Geoff Lang48dcae72014-02-05 16:28:24 -05002630 entry.OutputSlot = (separatedOutputBuffers ? i : 0);
2631 soDeclaration.push_back(entry);
2632 }
2633 }
2634
2635 result = mDevice->CreateGeometryShaderWithStreamOutput(function, length, soDeclaration.data(), soDeclaration.size(),
2636 NULL, 0, 0, NULL, &streamOutShader);
2637 ASSERT(SUCCEEDED(result));
2638 }
2639
2640 if (vertexShader)
2641 {
2642 executable = new ShaderExecutable11(function, length, vertexShader, streamOutShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002643 }
2644 }
2645 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002646 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002647 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002648 ID3D11PixelShader *pixelShader = NULL;
2649
2650 result = mDevice->CreatePixelShader(function, length, NULL, &pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002651 ASSERT(SUCCEEDED(result));
2652
Geoff Lang48dcae72014-02-05 16:28:24 -05002653 if (pixelShader)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002654 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002655 executable = new ShaderExecutable11(function, length, pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002656 }
2657 }
2658 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002659 case rx::SHADER_GEOMETRY:
2660 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002661 ID3D11GeometryShader *geometryShader = NULL;
2662
2663 result = mDevice->CreateGeometryShader(function, length, NULL, &geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002664 ASSERT(SUCCEEDED(result));
2665
Geoff Lang48dcae72014-02-05 16:28:24 -05002666 if (geometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002667 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002668 executable = new ShaderExecutable11(function, length, geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002669 }
2670 }
2671 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002672 default:
2673 UNREACHABLE();
2674 break;
2675 }
2676
2677 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002678}
2679
Geoff Lang48dcae72014-02-05 16:28:24 -05002680ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
2681 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2682 bool separatedOutputBuffers, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002683{
Geoff Lang6e05c272014-03-17 10:46:54 -07002684 const char *profileType = NULL;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002685 switch (type)
2686 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002687 case rx::SHADER_VERTEX:
Geoff Lang6e05c272014-03-17 10:46:54 -07002688 profileType = "vs";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002689 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002690 case rx::SHADER_PIXEL:
Geoff Lang6e05c272014-03-17 10:46:54 -07002691 profileType = "ps";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002692 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002693 case rx::SHADER_GEOMETRY:
Geoff Lang6e05c272014-03-17 10:46:54 -07002694 profileType = "gs";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002695 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002696 default:
2697 UNREACHABLE();
2698 return NULL;
2699 }
2700
Geoff Lang6e05c272014-03-17 10:46:54 -07002701 const char *profileVersion = NULL;
2702 switch (mFeatureLevel)
2703 {
2704 case D3D_FEATURE_LEVEL_11_0:
2705 profileVersion = "5_0";
2706 break;
2707 case D3D_FEATURE_LEVEL_10_1:
2708 profileVersion = "4_1";
2709 break;
2710 case D3D_FEATURE_LEVEL_10_0:
2711 profileVersion = "4_0";
2712 break;
2713 default:
2714 UNREACHABLE();
2715 return NULL;
2716 }
2717
2718 char profile[32];
2719 snprintf(profile, ArraySize(profile), "%s_%s", profileType, profileVersion);
2720
Nicolas Capens93faad92014-05-10 12:14:13 -04002721 UINT flags = D3DCOMPILE_OPTIMIZATION_LEVEL0;
2722
2723 if (gl::perfActive())
2724 {
2725#ifndef NDEBUG
2726 flags = D3DCOMPILE_SKIP_OPTIMIZATION;
2727#endif
2728
2729 flags |= D3DCOMPILE_DEBUG;
2730
2731 std::string sourcePath = getTempPath();
2732 std::string sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(shaderHLSL);
2733 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
2734 }
2735
2736 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
2737 // Try the default flags first and if compilation fails, try some alternatives.
2738 const UINT extraFlags[] =
2739 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002740 flags,
Nicolas Capens2c27db62014-05-10 12:21:11 -04002741 flags | D3DCOMPILE_SKIP_VALIDATION,
2742 flags | D3DCOMPILE_SKIP_OPTIMIZATION
Nicolas Capens93faad92014-05-10 12:14:13 -04002743 };
2744
2745 const static char *extraFlagNames[] =
2746 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002747 "default",
Nicolas Capens2c27db62014-05-10 12:21:11 -04002748 "skip validation",
2749 "skip optimization"
Nicolas Capens93faad92014-05-10 12:14:13 -04002750 };
2751
2752 int attempts = ArraySize(extraFlags);
2753
2754 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, extraFlags, extraFlagNames, attempts);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002755 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002756 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002757 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002758 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002759
Geoff Lang48dcae72014-02-05 16:28:24 -05002760 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type,
2761 transformFeedbackVaryings, separatedOutputBuffers);
Geoff Langea228632013-07-30 15:17:12 -04002762 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002763
2764 return executable;
2765}
2766
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002767rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2768{
2769 return new UniformStorage11(this, storageSize);
2770}
2771
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002772VertexBuffer *Renderer11::createVertexBuffer()
2773{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002774 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002775}
2776
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002777IndexBuffer *Renderer11::createIndexBuffer()
2778{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002779 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002780}
2781
Brandon Jonesd38f9262014-06-18 16:26:45 -07002782BufferImpl *Renderer11::createBuffer()
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002783{
Brandon Jonesd38f9262014-06-18 16:26:45 -07002784 return new Buffer11(this);
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002785}
2786
Brandon Jones5bf98292014-06-06 17:19:38 -07002787VertexArrayImpl *Renderer11::createVertexArray()
2788{
2789 return new VertexArray11(this);
2790}
2791
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002792QueryImpl *Renderer11::createQuery(GLenum type)
2793{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002794 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002795}
2796
2797FenceImpl *Renderer11::createFence()
2798{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002799 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002800}
2801
Geoff Lang005df412013-10-16 14:12:50 -04002802bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002803{
Geoff Lang2b31af22014-05-23 14:45:40 -04002804 ASSERT(getCaps().extensions.pixelBufferObject);
Jamie Madill4461f092013-10-10 15:10:39 -04002805
Jamie Madill4461f092013-10-10 15:10:39 -04002806 // sRGB formats do not work with D3D11 buffer SRVs
Geoff Lange4a492b2014-06-19 14:14:41 -04002807 if (gl::GetColorEncoding(internalFormat) == GL_SRGB)
Jamie Madill4461f092013-10-10 15:10:39 -04002808 {
2809 return false;
2810 }
2811
2812 // We cannot support direct copies to non-color-renderable formats
Geoff Langcec35902014-04-16 10:52:36 -04002813 if (!getCaps().textureCaps.get(internalFormat).colorRendering)
Jamie Madill4461f092013-10-10 15:10:39 -04002814 {
2815 return false;
2816 }
2817
2818 // We skip all 3-channel formats since sometimes format support is missing
Geoff Lange4a492b2014-06-19 14:14:41 -04002819 if (gl::GetComponentCount(internalFormat) == 3)
Jamie Madill4461f092013-10-10 15:10:39 -04002820 {
2821 return false;
2822 }
2823
2824 // We don't support formats which we can't represent without conversion
2825 if (getNativeTextureFormat(internalFormat) != internalFormat)
2826 {
2827 return false;
2828 }
2829
2830 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002831}
2832
Jamie Madilla21eea32013-09-18 14:36:25 -04002833bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2834 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2835{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002836 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002837 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2838}
2839
Jamie Madill3c7fa222014-06-05 13:08:51 -04002840bool Renderer11::getRenderTargetResource(gl::FramebufferAttachment *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002841{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002842 ASSERT(colorbuffer != NULL);
2843
2844 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2845 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002846 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002847 *subresourceIndex = renderTarget->getSubresourceIndex();
2848
2849 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2850 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002851 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002852 ID3D11Resource *textureResource = NULL;
2853 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002854
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002855 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002856 {
Geoff Lang8e328842014-02-10 13:11:20 -05002857 HRESULT result = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002858 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002859
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002860 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002861 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002862 return true;
2863 }
2864 else
2865 {
2866 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2867 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002868 }
2869 }
2870 }
2871 }
2872
2873 return false;
2874}
2875
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002876bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002877 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002878{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002879 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002880 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002881 gl::FramebufferAttachment *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002882
2883 if (!readBuffer)
2884 {
2885 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2886 return gl::error(GL_OUT_OF_MEMORY, false);
2887 }
2888
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002889 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002890
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002891 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002892 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002893 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2894 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002895 gl::FramebufferAttachment *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002896
2897 if (!drawBuffer)
2898 {
2899 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2900 return gl::error(GL_OUT_OF_MEMORY, false);
2901 }
2902
2903 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2904
Geoff Lang125deab2013-08-09 13:34:16 -04002905 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002906 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002907 {
2908 return false;
2909 }
2910 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002911 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002912 }
2913
Geoff Lang685806d2013-06-12 11:16:36 -04002914 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002915 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002916 gl::FramebufferAttachment *readBuffer = readTarget->getDepthOrStencilbuffer();
2917 gl::FramebufferAttachment *drawBuffer = drawTarget->getDepthOrStencilbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002918
2919 if (!readBuffer)
2920 {
2921 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2922 return gl::error(GL_OUT_OF_MEMORY, false);
2923 }
2924
2925 if (!drawBuffer)
2926 {
2927 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2928 return gl::error(GL_OUT_OF_MEMORY, false);
2929 }
2930
2931 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2932 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2933
Geoff Lang125deab2013-08-09 13:34:16 -04002934 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002935 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002936 {
2937 return false;
2938 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002939 }
2940
Geoff Lang42477a42013-09-17 17:07:02 -04002941 invalidateFramebufferSwizzles(drawTarget);
2942
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002943 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002944}
2945
Jamie Madilleb9baab2014-03-24 13:19:43 -04002946void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
2947 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002948{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002949 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002950 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002951
Jamie Madill3c7fa222014-06-05 13:08:51 -04002952 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002953
2954 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002955 {
2956 gl::Rectangle area;
2957 area.x = x;
2958 area.y = y;
2959 area.width = width;
2960 area.height = height;
2961
Jamie Madill1ef6fe72014-05-01 14:51:05 -04002962 if (pack.pixelBuffer.get() != NULL)
2963 {
Brandon Jonesd38f9262014-06-18 16:26:45 -07002964 rx::Buffer11 *packBufferStorage = Buffer11::makeBuffer11(pack.pixelBuffer.get()->getImplementation());
Jamie Madill1ef6fe72014-05-01 14:51:05 -04002965 PackPixelsParams packParams(area, format, type, outputPitch, pack, reinterpret_cast<ptrdiff_t>(pixels));
2966 packBufferStorage->packPixels(colorBufferTexture, subresourceIndex, packParams);
2967 }
2968 else
2969 {
2970 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch, pack, pixels);
2971 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002972
Geoff Langea228632013-07-30 15:17:12 -04002973 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002974 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002975}
2976
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002977Image *Renderer11::createImage()
2978{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002979 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002980}
2981
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002982void Renderer11::generateMipmap(Image *dest, Image *src)
2983{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002984 Image11 *dest11 = Image11::makeImage11(dest);
2985 Image11 *src11 = Image11::makeImage11(src);
Geoff Lange4a492b2014-06-19 14:14:41 -04002986 Image11::generateMipmap(dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002987}
2988
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002989TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
2990{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002991 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
2992 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002993}
2994
Nicolas Capensbf712d02014-03-31 14:23:35 -04002995TextureStorage *Renderer11::createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002996{
Nicolas Capensbf712d02014-03-31 14:23:35 -04002997 return new TextureStorage11_2D(this, internalformat, renderTarget, width, height, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002998}
2999
Nicolas Capensbf712d02014-03-31 14:23:35 -04003000TextureStorage *Renderer11::createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003001{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003002 return new TextureStorage11_Cube(this, internalformat, renderTarget, size, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003003}
3004
Nicolas Capensbf712d02014-03-31 14:23:35 -04003005TextureStorage *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 +00003006{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003007 return new TextureStorage11_3D(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003008}
3009
Nicolas Capensbf712d02014-03-31 14:23:35 -04003010TextureStorage *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 +00003011{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003012 return new TextureStorage11_2DArray(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003013}
3014
Jamie Madilleb9baab2014-03-24 13:19:43 -04003015void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format,
3016 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void *pixels)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003017{
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003018 ASSERT(area.width >= 0);
3019 ASSERT(area.height >= 0);
3020
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003021 D3D11_TEXTURE2D_DESC textureDesc;
3022 texture->GetDesc(&textureDesc);
3023
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003024 // Clamp read region to the defined texture boundaries, preventing out of bounds reads
3025 // and reads of uninitialized data.
3026 gl::Rectangle safeArea;
3027 safeArea.x = gl::clamp(area.x, 0, static_cast<int>(textureDesc.Width));
3028 safeArea.y = gl::clamp(area.y, 0, static_cast<int>(textureDesc.Height));
3029 safeArea.width = gl::clamp(area.width + std::min(area.x, 0), 0,
3030 static_cast<int>(textureDesc.Width) - safeArea.x);
3031 safeArea.height = gl::clamp(area.height + std::min(area.y, 0), 0,
3032 static_cast<int>(textureDesc.Height) - safeArea.y);
3033
3034 ASSERT(safeArea.x >= 0 && safeArea.y >= 0);
3035 ASSERT(safeArea.x + safeArea.width <= static_cast<int>(textureDesc.Width));
3036 ASSERT(safeArea.y + safeArea.height <= static_cast<int>(textureDesc.Height));
3037
3038 if (safeArea.width == 0 || safeArea.height == 0)
3039 {
3040 // no work to do
3041 return;
3042 }
3043
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003044 D3D11_TEXTURE2D_DESC stagingDesc;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003045 stagingDesc.Width = safeArea.width;
3046 stagingDesc.Height = safeArea.height;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003047 stagingDesc.MipLevels = 1;
3048 stagingDesc.ArraySize = 1;
3049 stagingDesc.Format = textureDesc.Format;
3050 stagingDesc.SampleDesc.Count = 1;
3051 stagingDesc.SampleDesc.Quality = 0;
3052 stagingDesc.Usage = D3D11_USAGE_STAGING;
3053 stagingDesc.BindFlags = 0;
3054 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3055 stagingDesc.MiscFlags = 0;
3056
3057 ID3D11Texture2D* stagingTex = NULL;
3058 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3059 if (FAILED(result))
3060 {
3061 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3062 return;
3063 }
3064
3065 ID3D11Texture2D* srcTex = NULL;
3066 if (textureDesc.SampleDesc.Count > 1)
3067 {
3068 D3D11_TEXTURE2D_DESC resolveDesc;
3069 resolveDesc.Width = textureDesc.Width;
3070 resolveDesc.Height = textureDesc.Height;
3071 resolveDesc.MipLevels = 1;
3072 resolveDesc.ArraySize = 1;
3073 resolveDesc.Format = textureDesc.Format;
3074 resolveDesc.SampleDesc.Count = 1;
3075 resolveDesc.SampleDesc.Quality = 0;
3076 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3077 resolveDesc.BindFlags = 0;
3078 resolveDesc.CPUAccessFlags = 0;
3079 resolveDesc.MiscFlags = 0;
3080
3081 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3082 if (FAILED(result))
3083 {
3084 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003085 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003086 return;
3087 }
3088
3089 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3090 subResource = 0;
3091 }
3092 else
3093 {
3094 srcTex = texture;
3095 srcTex->AddRef();
3096 }
3097
3098 D3D11_BOX srcBox;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003099 srcBox.left = static_cast<UINT>(safeArea.x);
3100 srcBox.right = static_cast<UINT>(safeArea.x + safeArea.width);
3101 srcBox.top = static_cast<UINT>(safeArea.y);
3102 srcBox.bottom = static_cast<UINT>(safeArea.y + safeArea.height);
3103 srcBox.front = 0;
3104 srcBox.back = 1;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003105
3106 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3107
Geoff Langea228632013-07-30 15:17:12 -04003108 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003109
Jamie Madill7538f7f2014-04-17 11:53:39 -04003110 PackPixelsParams packParams(safeArea, format, type, outputPitch, pack, 0);
3111 packPixels(stagingTex, packParams, pixels);
3112
3113 SafeRelease(stagingTex);
3114}
3115
3116void Renderer11::packPixels(ID3D11Texture2D *readTexture, const PackPixelsParams &params, void *pixelsOut)
3117{
3118 D3D11_TEXTURE2D_DESC textureDesc;
3119 readTexture->GetDesc(&textureDesc);
3120
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003121 D3D11_MAPPED_SUBRESOURCE mapping;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003122 HRESULT hr = mDeviceContext->Map(readTexture, 0, D3D11_MAP_READ, 0, &mapping);
Geoff Lang9cd19152014-05-28 15:54:34 -04003123 UNUSED_ASSERTION_VARIABLE(hr);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003124 ASSERT(SUCCEEDED(hr));
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003125
3126 unsigned char *source;
3127 int inputPitch;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003128 if (params.pack.reverseRowOrder)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003129 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003130 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (params.area.height - 1);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003131 inputPitch = -static_cast<int>(mapping.RowPitch);
3132 }
3133 else
3134 {
3135 source = static_cast<unsigned char*>(mapping.pData);
3136 inputPitch = static_cast<int>(mapping.RowPitch);
3137 }
3138
Geoff Lange4a492b2014-06-19 14:14:41 -04003139 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format);
3140 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat);
3141 GLenum sourceType = gl::GetType(sourceInternalFormat);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003142
Geoff Lange4a492b2014-06-19 14:14:41 -04003143 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003144
Jamie Madill7538f7f2014-04-17 11:53:39 -04003145 if (sourceFormat == params.format && sourceType == params.type)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003146 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003147 unsigned char *dest = static_cast<unsigned char*>(pixelsOut) + params.offset;
3148 for (int y = 0; y < params.area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003149 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003150 memcpy(dest + y * params.outputPitch, source + y * inputPitch, params.area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003151 }
3152 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003153 else
3154 {
Geoff Lange4a492b2014-06-19 14:14:41 -04003155 GLenum destInternalFormat = gl::GetSizedInternalFormat(params.format, params.type);
3156 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003157
Jamie Madill7538f7f2014-04-17 11:53:39 -04003158 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, params.format, params.type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003159 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003160 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003161 // Fast copy is possible through some special function
Jamie Madill7538f7f2014-04-17 11:53:39 -04003162 for (int y = 0; y < params.area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003163 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003164 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003165 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003166 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003167 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3168
3169 fastCopyFunc(src, dest);
3170 }
3171 }
3172 }
3173 else
3174 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003175 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Geoff Lange4a492b2014-06-19 14:14:41 -04003176 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(params.format, params.type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003177
3178 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3179 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3180 sizeof(temp) >= sizeof(gl::ColorUI) &&
3181 sizeof(temp) >= sizeof(gl::ColorI));
3182
Jamie Madill7538f7f2014-04-17 11:53:39 -04003183 for (int y = 0; y < params.area.height; y++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003184 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003185 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003186 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003187 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003188 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3189
3190 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3191 // will not allow the copy otherwise.
3192 readFunc(src, temp);
3193 writeFunc(temp, dest);
3194 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003195 }
3196 }
3197 }
3198
Jamie Madill7538f7f2014-04-17 11:53:39 -04003199 mDeviceContext->Unmap(readTexture, 0);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003200}
3201
Geoff Lang758d5b22013-06-11 11:42:50 -04003202bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003203 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3204 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003205{
Geoff Lang975af372013-06-12 11:19:22 -04003206 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3207 // it should never be the case that both color and depth/stencil need to be blitted at
3208 // at the same time.
3209 ASSERT(colorBlit != (depthBlit || stencilBlit));
3210
Geoff Langc1f51be2013-06-11 11:49:14 -04003211 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003212
Geoff Lang4d782732013-07-22 10:44:18 -04003213 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3214 if (!drawRenderTarget)
3215 {
3216 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3217 return gl::error(GL_OUT_OF_MEMORY, false);
3218 }
3219
3220 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3221 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3222 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3223 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3224
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003225 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3226 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003227 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003228 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003229 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003230 }
3231
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003232 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003233 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003234 unsigned int readSubresource = 0;
3235 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003236 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003237 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3238 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003239
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003240 if (unresolvedTexture)
3241 {
3242 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3243 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003244
Geoff Langea228632013-07-30 15:17:12 -04003245 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003246
Geoff Lang5c9a29a2014-02-11 10:26:24 -05003247 HRESULT hresult = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3248 if (FAILED(hresult))
Geoff Langc1f51be2013-06-11 11:49:14 -04003249 {
Geoff Langea228632013-07-30 15:17:12 -04003250 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003251 return gl::error(GL_OUT_OF_MEMORY, false);
3252 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003253 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003254 }
3255 else
3256 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003257 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003258 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003259 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003260 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003261 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003262 }
3263
Geoff Lang4d782732013-07-22 10:44:18 -04003264 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003265 {
Geoff Lang4d782732013-07-22 10:44:18 -04003266 SafeRelease(readTexture);
3267 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003268 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003269 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003270 }
3271
Geoff Lang125deab2013-08-09 13:34:16 -04003272 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3273 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3274
3275 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3276
3277 bool wholeBufferCopy = !scissorNeeded &&
3278 readRect.x == 0 && readRect.width == readSize.width &&
3279 readRect.y == 0 && readRect.height == readSize.height &&
3280 drawRect.x == 0 && drawRect.width == drawSize.width &&
3281 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003282
Geoff Langc1f51be2013-06-11 11:49:14 -04003283 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003284
Geoff Lang1cd1b212014-02-11 09:42:27 -05003285 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003286
3287 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3288 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3289 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3290 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3291
Geoff Lange4a492b2014-06-19 14:14:41 -04003292 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat()) > 0;
3293 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat()) > 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003294 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3295
Geoff Langc1f51be2013-06-11 11:49:14 -04003296 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003297 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3298 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003299 {
Geoff Lang125deab2013-08-09 13:34:16 -04003300 UINT dstX = drawRect.x;
3301 UINT dstY = drawRect.y;
3302
Geoff Langc1f51be2013-06-11 11:49:14 -04003303 D3D11_BOX readBox;
3304 readBox.left = readRect.x;
3305 readBox.right = readRect.x + readRect.width;
3306 readBox.top = readRect.y;
3307 readBox.bottom = readRect.y + readRect.height;
3308 readBox.front = 0;
3309 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003310
Geoff Lang125deab2013-08-09 13:34:16 -04003311 if (scissorNeeded)
3312 {
3313 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3314 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3315
3316 if (drawRect.x < scissor->x)
3317 {
3318 dstX = scissor->x;
3319 readBox.left += (scissor->x - drawRect.x);
3320 }
3321 if (drawRect.y < scissor->y)
3322 {
3323 dstY = scissor->y;
3324 readBox.top += (scissor->y - drawRect.y);
3325 }
3326 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3327 {
3328 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3329 }
3330 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3331 {
3332 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3333 }
3334 }
3335
Geoff Langc1f51be2013-06-11 11:49:14 -04003336 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3337 // We also require complete framebuffer copies for depth-stencil blit.
3338 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003339
Geoff Lang125deab2013-08-09 13:34:16 -04003340 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003341 readTexture, readSubresource, pSrcBox);
3342 result = true;
3343 }
3344 else
3345 {
3346 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003347 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003348
Geoff Lang975af372013-06-12 11:19:22 -04003349 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003350 {
Geoff Lang975af372013-06-12 11:19:22 -04003351 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003352 drawTexture, drawSubresource, drawArea, drawSize,
3353 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003354 }
3355 else if (depthBlit)
3356 {
Geoff Lang125deab2013-08-09 13:34:16 -04003357 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3358 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003359 }
3360 else if (stencilBlit)
3361 {
3362 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003363 drawTexture, drawSubresource, drawArea, drawSize,
3364 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003365 }
3366 else
3367 {
Geoff Lange4a492b2014-06-19 14:14:41 -04003368 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat());
Geoff Lang125deab2013-08-09 13:34:16 -04003369 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3370 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003371 }
3372 }
3373
3374 SafeRelease(readTexture);
3375 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003376
3377 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003378}
3379
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003380ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3381{
3382 D3D11_TEXTURE2D_DESC textureDesc;
3383 source->GetDesc(&textureDesc);
3384
3385 if (textureDesc.SampleDesc.Count > 1)
3386 {
3387 D3D11_TEXTURE2D_DESC resolveDesc;
3388 resolveDesc.Width = textureDesc.Width;
3389 resolveDesc.Height = textureDesc.Height;
3390 resolveDesc.MipLevels = 1;
3391 resolveDesc.ArraySize = 1;
3392 resolveDesc.Format = textureDesc.Format;
3393 resolveDesc.SampleDesc.Count = 1;
3394 resolveDesc.SampleDesc.Quality = 0;
3395 resolveDesc.Usage = textureDesc.Usage;
3396 resolveDesc.BindFlags = textureDesc.BindFlags;
3397 resolveDesc.CPUAccessFlags = 0;
3398 resolveDesc.MiscFlags = 0;
3399
3400 ID3D11Texture2D *resolveTexture = NULL;
3401 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3402 if (FAILED(result))
3403 {
3404 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3405 return NULL;
3406 }
3407
3408 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3409 return resolveTexture;
3410 }
3411 else
3412 {
3413 source->AddRef();
3414 return source;
3415 }
3416}
3417
Jamie Madill3c7fa222014-06-05 13:08:51 -04003418void Renderer11::invalidateFBOAttachmentSwizzles(gl::FramebufferAttachment *attachment, int mipLevel)
Geoff Lang42477a42013-09-17 17:07:02 -04003419{
Jamie Madill3c7fa222014-06-05 13:08:51 -04003420 ASSERT(attachment->isTexture());
3421 TextureStorage *texStorage = attachment->getTextureStorage();
Geoff Lang42477a42013-09-17 17:07:02 -04003422 if (texStorage)
3423 {
3424 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3425 if (!texStorage11)
3426 {
3427 ERR("texture storage pointer unexpectedly null.");
3428 return;
3429 }
3430
3431 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3432 }
3433}
3434
3435void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3436{
3437 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3438 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003439 gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(colorAttachment);
3440 if (attachment && attachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003441 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003442 invalidateFBOAttachmentSwizzles(attachment, framebuffer->getColorbufferMipLevel(colorAttachment));
Geoff Lang42477a42013-09-17 17:07:02 -04003443 }
3444 }
3445
Jamie Madill3c7fa222014-06-05 13:08:51 -04003446 gl::FramebufferAttachment *depthAttachment = framebuffer->getDepthbuffer();
3447 if (depthAttachment && depthAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003448 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003449 invalidateFBOAttachmentSwizzles(depthAttachment, framebuffer->getDepthbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003450 }
3451
Jamie Madill3c7fa222014-06-05 13:08:51 -04003452 gl::FramebufferAttachment *stencilAttachment = framebuffer->getStencilbuffer();
3453 if (stencilAttachment && stencilAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003454 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003455 invalidateFBOAttachmentSwizzles(stencilAttachment, framebuffer->getStencilbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003456 }
3457}
3458
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003459bool Renderer11::getLUID(LUID *adapterLuid) const
3460{
3461 adapterLuid->HighPart = 0;
3462 adapterLuid->LowPart = 0;
3463
3464 if (!mDxgiAdapter)
3465 {
3466 return false;
3467 }
3468
3469 DXGI_ADAPTER_DESC adapterDesc;
3470 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3471 {
3472 return false;
3473 }
3474
3475 *adapterLuid = adapterDesc.AdapterLuid;
3476 return true;
3477}
3478
Geoff Lang005df412013-10-16 14:12:50 -04003479GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003480{
Geoff Lange4a492b2014-06-19 14:14:41 -04003481 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat));
Jamie Madillc8c102b2013-10-10 15:10:24 -04003482}
3483
Jamie Madill95ffb862014-01-29 09:26:59 -05003484rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3485{
3486 return gl_d3d11::GetVertexConversionType(vertexFormat);
3487}
3488
3489GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3490{
3491 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3492}
3493
Geoff Lang61e49a52013-05-29 10:22:58 -04003494Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3495{
3496 MultisampleSupportInfo supportInfo = { 0 };
3497
3498 UINT formatSupport;
3499 HRESULT result;
3500
3501 result = mDevice->CheckFormatSupport(format, &formatSupport);
3502 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3503 {
3504 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3505 {
3506 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3507 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3508 {
3509 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3510 }
3511 else
3512 {
3513 supportInfo.qualityLevels[i - 1] = 0;
3514 }
3515 }
3516 }
3517
3518 return supportInfo;
3519}
3520
Geoff Langcec35902014-04-16 10:52:36 -04003521gl::Caps Renderer11::generateCaps() const
3522{
3523 return d3d11_gl::GenerateCaps(mDevice);
3524}
3525
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003526}