blob: 646bd9b7a040bb72bd69dab0e2e28a1569cfd029 [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
Brandon Jonesa328d562014-07-01 13:52:40 -0700420 storage11->generateSwizzles(texture->getSamplerState().swizzleRed,
421 texture->getSamplerState().swizzleGreen,
422 texture->getSamplerState().swizzleBlue,
423 texture->getSamplerState().swizzleAlpha);
Geoff Lang42477a42013-09-17 17:07:02 -0400424 }
425 }
Geoff Lange2e0ce02013-09-17 17:05:08 -0400426}
427
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000428void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
429{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000430 if (type == gl::SAMPLER_PIXEL)
431 {
432 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
433 {
434 ERR("Pixel shader sampler index %i is not valid.", index);
435 return;
436 }
437
438 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
439 {
440 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
441
442 if (!dxSamplerState)
443 {
444 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
445 "sampler state for pixel shaders at slot %i.", index);
446 }
447
448 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
449
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000450 mCurPixelSamplerStates[index] = samplerState;
451 }
452
453 mForceSetPixelSamplerStates[index] = false;
454 }
455 else if (type == gl::SAMPLER_VERTEX)
456 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000457 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000458 {
459 ERR("Vertex shader sampler index %i is not valid.", index);
460 return;
461 }
462
463 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
464 {
465 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
466
467 if (!dxSamplerState)
468 {
469 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
470 "sampler state for vertex shaders at slot %i.", index);
471 }
472
473 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
474
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000475 mCurVertexSamplerStates[index] = samplerState;
476 }
477
478 mForceSetVertexSamplerStates[index] = false;
479 }
480 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000481}
482
483void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
484{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000485 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000486 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000487
488 if (texture)
489 {
490 TextureStorageInterface *texStorage = texture->getNativeTexture();
491 if (texStorage)
492 {
493 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
Nicolas Capensa10c1c72014-03-27 11:20:52 -0400494 gl::SamplerState samplerState;
Brandon Jonesa328d562014-07-01 13:52:40 -0700495 texture->getSamplerStateWithNativeOffset(&samplerState);
Nicolas Capensa10c1c72014-03-27 11:20:52 -0400496 textureSRV = storage11->getSRV(samplerState);
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000497 }
498
499 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
500 // missing the shader resource view
501 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000502
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000503 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000504 }
505
506 if (type == gl::SAMPLER_PIXEL)
507 {
508 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
509 {
510 ERR("Pixel shader sampler index %i is not valid.", index);
511 return;
512 }
513
Geoff Lang91382e52014-01-07 16:16:30 -0500514 if (forceSetTexture || mCurPixelSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000515 {
516 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
517 }
518
Geoff Lang91382e52014-01-07 16:16:30 -0500519 mCurPixelSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000520 }
521 else if (type == gl::SAMPLER_VERTEX)
522 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000523 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000524 {
525 ERR("Vertex shader sampler index %i is not valid.", index);
526 return;
527 }
528
Geoff Lang91382e52014-01-07 16:16:30 -0500529 if (forceSetTexture || mCurVertexSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000530 {
531 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
532 }
533
Geoff Lang91382e52014-01-07 16:16:30 -0500534 mCurVertexSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000535 }
536 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000537}
538
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000539bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
540{
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000541 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
542 {
543 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
544 if (uniformBuffer)
545 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700546 Buffer11 *bufferStorage = Buffer11::makeBuffer11(uniformBuffer->getImplementation());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500547 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000548
549 if (!constantBuffer)
550 {
551 return false;
552 }
553
Geoff Langc6354ee2013-07-22 10:40:07 -0400554 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
555 {
556 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
557 1, &constantBuffer);
558 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
559 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000560 }
561 }
562
563 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
564 {
565 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
566 if (uniformBuffer)
567 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700568 Buffer11 *bufferStorage = Buffer11::makeBuffer11(uniformBuffer->getImplementation());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500569 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000570
571 if (!constantBuffer)
572 {
573 return false;
574 }
575
Geoff Langc6354ee2013-07-22 10:40:07 -0400576 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
577 {
578 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
579 1, &constantBuffer);
580 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
581 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000582 }
583 }
584
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000585 return true;
586}
587
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000588void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000589{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000590 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000591 {
Nicolas Capensaea8e942014-05-09 19:14:08 -0400592 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000593 if (!dxRasterState)
594 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000595 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000596 "rasterizer state.");
597 }
598
599 mDeviceContext->RSSetState(dxRasterState);
600
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000601 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000602 }
603
604 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000605}
606
Geoff Langc142e9d2013-09-30 15:19:47 -0400607void Renderer11::setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000608 unsigned int sampleMask)
609{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000610 if (mForceSetBlendState ||
611 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400612 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000613 sampleMask != mCurSampleMask)
614 {
Geoff Langc142e9d2013-09-30 15:19:47 -0400615 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(framebuffer, blendState);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000616 if (!dxBlendState)
617 {
618 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
619 "blend state.");
620 }
621
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000622 float blendColors[4] = {0.0f};
623 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
624 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
625 {
626 blendColors[0] = blendColor.red;
627 blendColors[1] = blendColor.green;
628 blendColors[2] = blendColor.blue;
629 blendColors[3] = blendColor.alpha;
630 }
631 else
632 {
633 blendColors[0] = blendColor.alpha;
634 blendColors[1] = blendColor.alpha;
635 blendColors[2] = blendColor.alpha;
636 blendColors[3] = blendColor.alpha;
637 }
638
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000639 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
640
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000641 mCurBlendState = blendState;
642 mCurBlendColor = blendColor;
643 mCurSampleMask = sampleMask;
644 }
645
646 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000647}
648
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000649void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000650 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000651{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000652 if (mForceSetDepthStencilState ||
653 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
654 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
655 {
Jamie Madillac528012014-06-20 13:21:23 -0400656 ASSERT(depthStencilState.stencilWritemask == depthStencilState.stencilBackWritemask);
657 ASSERT(stencilRef == stencilBackRef);
658 ASSERT(depthStencilState.stencilMask == depthStencilState.stencilBackMask);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000659
660 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
661 if (!dxDepthStencilState)
662 {
663 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
664 "setting the default depth stencil state.");
665 }
666
Jamie Madillec91cd32014-01-21 16:38:12 -0500667 // Max D3D11 stencil reference value is 0xFF, corresponding to the max 8 bits in a stencil buffer
668 // GL specifies we should clamp the ref value to the nearest bit depth when doing stencil ops
669 META_ASSERT(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF);
670 META_ASSERT(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF);
Geoff Lang0bf3a982014-02-11 09:40:48 -0500671 UINT dxStencilRef = std::min<UINT>(stencilRef, 0xFFu);
Jamie Madillec91cd32014-01-21 16:38:12 -0500672
Geoff Lang0bf3a982014-02-11 09:40:48 -0500673 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, dxStencilRef);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000674
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000675 mCurDepthStencilState = depthStencilState;
676 mCurStencilRef = stencilRef;
677 mCurStencilBackRef = stencilBackRef;
678 }
679
680 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000681}
682
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000683void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000684{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000685 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
686 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000687 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000688 if (enabled)
689 {
690 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000691 rect.left = std::max(0, scissor.x);
692 rect.top = std::max(0, scissor.y);
693 rect.right = scissor.x + std::max(0, scissor.width);
694 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000695
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000696 mDeviceContext->RSSetScissorRects(1, &rect);
697 }
698
699 if (enabled != mScissorEnabled)
700 {
701 mForceSetRasterState = true;
702 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000703
704 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000705 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000706 }
707
708 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000709}
710
daniel@transgaming.com12985182012-12-20 20:56:31 +0000711bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000712 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000713{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000714 gl::Rectangle actualViewport = viewport;
715 float actualZNear = gl::clamp01(zNear);
716 float actualZFar = gl::clamp01(zFar);
717 if (ignoreViewport)
718 {
719 actualViewport.x = 0;
720 actualViewport.y = 0;
721 actualViewport.width = mRenderTargetDesc.width;
722 actualViewport.height = mRenderTargetDesc.height;
723 actualZNear = 0.0f;
724 actualZFar = 1.0f;
725 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000726
Geoff Langaae65a42014-05-26 12:43:44 -0400727 const gl::Caps& caps = getCaps();
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000728
729 // 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 +0000730 D3D11_VIEWPORT dxViewport;
Geoff Langaae65a42014-05-26 12:43:44 -0400731 dxViewport.TopLeftX = gl::clamp(actualViewport.x, -static_cast<int>(caps.maxViewportWidth), static_cast<int>(caps.maxViewportWidth));
732 dxViewport.TopLeftY = gl::clamp(actualViewport.y, -static_cast<int>(caps.maxViewportHeight), static_cast<int>(caps.maxViewportHeight));
733 dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(caps.maxViewportWidth - dxViewport.TopLeftX));
734 dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(caps.maxViewportHeight - dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000735 dxViewport.MinDepth = actualZNear;
736 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000737
738 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
739 {
740 return false; // Nothing to render
741 }
742
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000743 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
744 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000745
daniel@transgaming.com53670042012-11-28 20:55:51 +0000746 if (viewportChanged)
747 {
748 mDeviceContext->RSSetViewports(1, &dxViewport);
749
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000750 mCurViewport = actualViewport;
751 mCurNear = actualZNear;
752 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000753
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000754 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
755 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
756 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
757 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000758
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000759 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
760 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000761
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000762 mVertexConstants.depthRange[0] = actualZNear;
763 mVertexConstants.depthRange[1] = actualZFar;
764 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000765
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000766 mPixelConstants.depthRange[0] = actualZNear;
767 mPixelConstants.depthRange[1] = actualZFar;
768 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000769 }
770
771 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000772 return true;
773}
774
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000775bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
776{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000777 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000778
Geoff Lang57e713e2013-07-31 17:01:58 -0400779 GLsizei minCount = 0;
780
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000781 switch (mode)
782 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400783 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
784 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
785 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
786 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
787 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
788 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000789 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400790 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000791 default:
Jamie Madill1aeb1312014-06-20 13:21:25 -0400792 UNREACHABLE();
793 return false;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000794 }
795
Geoff Lang4c095862013-07-22 10:43:36 -0400796 if (primitiveTopology != mCurrentPrimitiveTopology)
797 {
798 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
799 mCurrentPrimitiveTopology = primitiveTopology;
800 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000801
Geoff Lang57e713e2013-07-31 17:01:58 -0400802 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000803}
804
805bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000806{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000807 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000808 // Also extract the render target dimensions and view
809 unsigned int renderTargetWidth = 0;
810 unsigned int renderTargetHeight = 0;
811 GLenum renderTargetFormat = 0;
812 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
813 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
814 bool missingColorRenderTarget = true;
815
816 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000817 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000818 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
Jamie Madille92a3542014-07-03 10:38:58 -0400819 gl::FramebufferAttachment *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000820
Jamie Madille92a3542014-07-03 10:38:58 -0400821 if (colorbuffer && 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
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000826 // check for zero-sized default framebuffer, which is a special case.
827 // in this case we do not wish to modify any state and just silently return false.
828 // this will not report any gl error but will cause the calling method to return.
829 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
830 {
831 return false;
832 }
833
834 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
835
836 // Extract the render target dimensions and view
837 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
838 if (!renderTarget)
839 {
840 ERR("render target pointer unexpectedly null.");
841 return false;
842 }
843
844 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
845 if (!framebufferRTVs[colorAttachment])
846 {
847 ERR("render target view pointer unexpectedly null.");
848 return false;
849 }
850
851 if (missingColorRenderTarget)
852 {
853 renderTargetWidth = colorbuffer->getWidth();
854 renderTargetHeight = colorbuffer->getHeight();
855 renderTargetFormat = colorbuffer->getActualFormat();
856 missingColorRenderTarget = false;
857 }
Jamie Madillba597af2013-10-22 13:12:15 -0400858
Geoff Lang91382e52014-01-07 16:16:30 -0500859 // TODO: Detect if this color buffer is already bound as a texture and unbind it first to prevent
860 // D3D11 warnings.
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000861 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000862 }
863
864 // Get the depth stencil render buffer and serials
Jamie Madille92a3542014-07-03 10:38:58 -0400865 gl::FramebufferAttachment *depthStencil = framebuffer->getDepthbuffer();
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000866 unsigned int depthbufferSerial = 0;
867 unsigned int stencilbufferSerial = 0;
Jamie Madille92a3542014-07-03 10:38:58 -0400868 if (depthStencil)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000869 {
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000870 depthbufferSerial = depthStencil->getSerial();
871 }
Jamie Madille92a3542014-07-03 10:38:58 -0400872 else if (framebuffer->getStencilbuffer())
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000873 {
874 depthStencil = framebuffer->getStencilbuffer();
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000875 stencilbufferSerial = depthStencil->getSerial();
876 }
877
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000878 ID3D11DepthStencilView* framebufferDSV = NULL;
879 if (depthStencil)
880 {
881 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
882 if (!depthStencilRenderTarget)
883 {
884 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000885 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000886 return false;
887 }
888
889 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
890 if (!framebufferDSV)
891 {
892 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000893 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000894 return false;
895 }
896
897 // If there is no render buffer, the width, height and format values come from
898 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000899 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000900 {
901 renderTargetWidth = depthStencil->getWidth();
902 renderTargetHeight = depthStencil->getHeight();
903 renderTargetFormat = depthStencil->getActualFormat();
904 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000905 }
906
907 // Apply the render target and depth stencil
908 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000909 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000910 depthbufferSerial != mAppliedDepthbufferSerial ||
911 stencilbufferSerial != mAppliedStencilbufferSerial)
912 {
Geoff Langaae65a42014-05-26 12:43:44 -0400913 mDeviceContext->OMSetRenderTargets(getCaps().maxDrawBuffers, framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000914
915 mRenderTargetDesc.width = renderTargetWidth;
916 mRenderTargetDesc.height = renderTargetHeight;
917 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000918 mForceSetViewport = true;
919 mForceSetScissor = true;
Geoff Langc142e9d2013-09-30 15:19:47 -0400920 mForceSetBlendState = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000921
Nicolas Capensaea8e942014-05-09 19:14:08 -0400922 if (!mDepthStencilInitialized)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000923 {
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000924 mForceSetRasterState = true;
925 }
926
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000927 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
928 {
929 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
930 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000931 mAppliedDepthbufferSerial = depthbufferSerial;
932 mAppliedStencilbufferSerial = stencilbufferSerial;
933 mRenderTargetDescInitialized = true;
934 mDepthStencilInitialized = true;
935 }
936
Geoff Lang42477a42013-09-17 17:07:02 -0400937 invalidateFramebufferSwizzles(framebuffer);
938
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000939 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000940}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000941
Jamie Madill57a89722013-07-02 11:57:03 -0400942GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -0400943 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000944{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000945 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -0400946 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000947 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000948 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000949 return err;
950 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000951
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000952 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000953}
954
daniel@transgaming.com31240482012-11-28 21:06:41 +0000955GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000956{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000957 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000958
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000959 if (err == GL_NO_ERROR)
960 {
Geoff Lang7840b172014-03-13 11:20:44 -0400961 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
962
963 ID3D11Buffer *buffer = NULL;
964 DXGI_FORMAT bufferFormat = indexBuffer->getIndexFormat();
965
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000966 if (indexInfo->storage)
967 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700968 Buffer11 *storage = Buffer11::makeBuffer11(indexInfo->storage);
Geoff Lang7840b172014-03-13 11:20:44 -0400969 buffer = storage->getBuffer(BUFFER_USAGE_INDEX);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000970 }
Geoff Lang7840b172014-03-13 11:20:44 -0400971 else
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000972 {
Geoff Lang7840b172014-03-13 11:20:44 -0400973 buffer = indexBuffer->getBuffer();
974 }
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000975
Geoff Lang7840b172014-03-13 11:20:44 -0400976 if (buffer != mAppliedIB || bufferFormat != mAppliedIBFormat || indexInfo->startOffset != mAppliedIBOffset)
977 {
978 mDeviceContext->IASetIndexBuffer(buffer, bufferFormat, indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000979
Geoff Lang7840b172014-03-13 11:20:44 -0400980 mAppliedIB = buffer;
981 mAppliedIBFormat = bufferFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +0000982 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000983 }
984 }
985
986 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000987}
988
Geoff Langeeba6e12014-02-03 13:12:30 -0500989void Renderer11::applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[])
990{
991 ID3D11Buffer* d3dBuffers[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
992 UINT d3dOffsets[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
993 bool requiresUpdate = false;
994 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
995 {
996 if (transformFeedbackBuffers[i])
997 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700998 Buffer11 *storage = Buffer11::makeBuffer11(transformFeedbackBuffers[i]->getImplementation());
Geoff Langeeba6e12014-02-03 13:12:30 -0500999 ID3D11Buffer *buffer = storage->getBuffer(BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK);
1000
1001 d3dBuffers[i] = buffer;
1002 d3dOffsets[i] = (mAppliedTFBuffers[i] != buffer) ? static_cast<UINT>(offsets[i]) : -1;
1003 }
1004 else
1005 {
1006 d3dBuffers[i] = NULL;
1007 d3dOffsets[i] = 0;
1008 }
1009
1010 if (d3dBuffers[i] != mAppliedTFBuffers[i] || offsets[i] != mAppliedTFOffsets[i])
1011 {
1012 requiresUpdate = true;
1013 }
1014 }
1015
1016 if (requiresUpdate)
1017 {
1018 mDeviceContext->SOSetTargets(ArraySize(d3dBuffers), d3dBuffers, d3dOffsets);
1019 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1020 {
1021 mAppliedTFBuffers[i] = d3dBuffers[i];
1022 mAppliedTFOffsets[i] = offsets[i];
1023 }
1024 }
1025}
1026
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001027void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001028{
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001029 if (mode == GL_POINTS && transformFeedbackActive)
1030 {
1031 // Since point sprites are generated with a geometry shader, too many vertices will
1032 // be written if transform feedback is active. To work around this, draw only the points
1033 // with the stream out shader and no pixel shader to feed the stream out buffers and then
1034 // draw again with the point sprite geometry shader to rasterize the point sprites.
1035
1036 mDeviceContext->PSSetShader(NULL, NULL, 0);
1037
1038 if (instances > 0)
1039 {
1040 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1041 }
1042 else
1043 {
1044 mDeviceContext->Draw(count, 0);
1045 }
1046
1047 mDeviceContext->GSSetShader(mCurPointGeometryShader, NULL, 0);
1048 mDeviceContext->PSSetShader(mAppliedPixelShader, NULL, 0);
1049
1050 if (instances > 0)
1051 {
1052 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1053 }
1054 else
1055 {
1056 mDeviceContext->Draw(count, 0);
1057 }
1058
1059 mDeviceContext->GSSetShader(mAppliedGeometryShader, NULL, 0);
1060 }
1061 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001062 {
1063 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1064 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001065 else if (mode == GL_TRIANGLE_FAN)
1066 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001067 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001068 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001069 else if (instances > 0)
1070 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001071 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001072 }
1073 else
1074 {
1075 mDeviceContext->Draw(count, 0);
1076 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001077}
1078
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001079void Renderer11::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices,
1080 gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001081{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001082 if (mode == GL_LINE_LOOP)
1083 {
1084 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1085 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001086 else if (mode == GL_TRIANGLE_FAN)
1087 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001088 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1089 }
1090 else if (instances > 0)
1091 {
1092 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001093 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001094 else
1095 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001096 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001097 }
1098}
1099
1100void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1101{
1102 // Get the raw indices for an indexed draw
1103 if (type != GL_NONE && elementArrayBuffer)
1104 {
1105 gl::Buffer *indexBuffer = elementArrayBuffer;
Brandon Jonesd38f9262014-06-18 16:26:45 -07001106 BufferImpl *storage = indexBuffer->getImplementation();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001107 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001108 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001109 }
1110
1111 if (!mLineLoopIB)
1112 {
1113 mLineLoopIB = new StreamingIndexBufferInterface(this);
1114 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1115 {
1116 delete mLineLoopIB;
1117 mLineLoopIB = NULL;
1118
1119 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001120 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001121 }
1122 }
1123
Geoff Lang57e713e2013-07-31 17:01:58 -04001124 // Checked by Renderer11::applyPrimitiveType
1125 ASSERT(count >= 0);
1126
1127 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001128 {
1129 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1130 return gl::error(GL_OUT_OF_MEMORY);
1131 }
1132
Geoff Lang57e713e2013-07-31 17:01:58 -04001133 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001134 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1135 {
1136 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001137 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001138 }
1139
1140 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001141 unsigned int offset;
1142 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001143 {
1144 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001145 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001146 }
1147
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001148 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001149 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001150
1151 switch (type)
1152 {
1153 case GL_NONE: // Non-indexed draw
1154 for (int i = 0; i < count; i++)
1155 {
1156 data[i] = i;
1157 }
1158 data[count] = 0;
1159 break;
1160 case GL_UNSIGNED_BYTE:
1161 for (int i = 0; i < count; i++)
1162 {
1163 data[i] = static_cast<const GLubyte*>(indices)[i];
1164 }
1165 data[count] = static_cast<const GLubyte*>(indices)[0];
1166 break;
1167 case GL_UNSIGNED_SHORT:
1168 for (int i = 0; i < count; i++)
1169 {
1170 data[i] = static_cast<const GLushort*>(indices)[i];
1171 }
1172 data[count] = static_cast<const GLushort*>(indices)[0];
1173 break;
1174 case GL_UNSIGNED_INT:
1175 for (int i = 0; i < count; i++)
1176 {
1177 data[i] = static_cast<const GLuint*>(indices)[i];
1178 }
1179 data[count] = static_cast<const GLuint*>(indices)[0];
1180 break;
1181 default: UNREACHABLE();
1182 }
1183
1184 if (!mLineLoopIB->unmapBuffer())
1185 {
1186 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001187 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001188 }
1189
Geoff Lang7840b172014-03-13 11:20:44 -04001190 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1191 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1192 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001193
Geoff Lang7840b172014-03-13 11:20:44 -04001194 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1195 {
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001196 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001197 mAppliedIB = d3dIndexBuffer;
1198 mAppliedIBFormat = indexFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001199 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001200 }
1201
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001202 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001203}
1204
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001205void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001206{
1207 // Get the raw indices for an indexed draw
1208 if (type != GL_NONE && elementArrayBuffer)
1209 {
1210 gl::Buffer *indexBuffer = elementArrayBuffer;
Brandon Jonesd38f9262014-06-18 16:26:45 -07001211 BufferImpl *storage = indexBuffer->getImplementation();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001212 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001213 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001214 }
1215
1216 if (!mTriangleFanIB)
1217 {
1218 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1219 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1220 {
1221 delete mTriangleFanIB;
1222 mTriangleFanIB = NULL;
1223
1224 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001225 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001226 }
1227 }
1228
Geoff Lang57e713e2013-07-31 17:01:58 -04001229 // Checked by Renderer11::applyPrimitiveType
1230 ASSERT(count >= 3);
1231
Geoff Langeadfd572013-07-09 15:55:07 -04001232 const unsigned int numTris = count - 2;
1233
Geoff Lang57e713e2013-07-31 17:01:58 -04001234 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001235 {
1236 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1237 return gl::error(GL_OUT_OF_MEMORY);
1238 }
1239
1240 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001241 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1242 {
1243 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001244 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001245 }
1246
1247 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001248 unsigned int offset;
1249 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001250 {
1251 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001252 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001253 }
1254
1255 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001256 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001257
1258 switch (type)
1259 {
1260 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001261 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001262 {
1263 data[i*3 + 0] = 0;
1264 data[i*3 + 1] = i + 1;
1265 data[i*3 + 2] = i + 2;
1266 }
1267 break;
1268 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001269 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001270 {
1271 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1272 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1273 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1274 }
1275 break;
1276 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001277 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001278 {
1279 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1280 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1281 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1282 }
1283 break;
1284 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001285 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001286 {
1287 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1288 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1289 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1290 }
1291 break;
1292 default: UNREACHABLE();
1293 }
1294
1295 if (!mTriangleFanIB->unmapBuffer())
1296 {
1297 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001298 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001299 }
1300
Geoff Lang7840b172014-03-13 11:20:44 -04001301 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1302 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1303 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001304
Geoff Lang7840b172014-03-13 11:20:44 -04001305 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1306 {
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001307 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001308 mAppliedIB = d3dIndexBuffer;
1309 mAppliedIBFormat = indexFormat;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001310 mAppliedIBOffset = indexBufferOffset;
1311 }
1312
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001313 if (instances > 0)
1314 {
1315 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1316 }
1317 else
1318 {
1319 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1320 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001321}
1322
Geoff Lang04fb89a2014-06-09 15:05:36 -04001323void Renderer11::applyShaders(gl::ProgramBinary *programBinary, const gl::VertexFormat inputLayout[], const gl::Framebuffer *framebuffer,
1324 bool rasterizerDiscard, bool transformFeedbackActive)
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001325{
Jamie Madillc5a83002014-02-14 16:41:25 -05001326 ShaderExecutable *vertexExe = programBinary->getVertexExecutableForInputLayout(inputLayout);
Geoff Lang04fb89a2014-06-09 15:05:36 -04001327 ShaderExecutable *pixelExe = programBinary->getPixelExecutableForFramebuffer(framebuffer);
Jamie Madill6246dc82014-01-29 09:26:47 -05001328 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001329
Jamie Madill6246dc82014-01-29 09:26:47 -05001330 ID3D11VertexShader *vertexShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader() : NULL);
Jamie Madill6246dc82014-01-29 09:26:47 -05001331
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001332 ID3D11PixelShader *pixelShader = NULL;
1333 // Skip pixel shader if we're doing rasterizer discard.
1334 if (!rasterizerDiscard)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001335 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001336 pixelShader = (pixelExe ? ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader() : NULL);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001337 }
1338
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001339 ID3D11GeometryShader *geometryShader = NULL;
1340 if (transformFeedbackActive)
Geoff Lang0550d032014-01-30 11:29:07 -05001341 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001342 geometryShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getStreamOutShader() : NULL);
1343 }
1344 else if (mCurRasterState.pointDrawMode)
1345 {
1346 geometryShader = (geometryExe ? ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader() : NULL);
Geoff Lang0550d032014-01-30 11:29:07 -05001347 }
1348
Jamie Madill6246dc82014-01-29 09:26:47 -05001349 bool dirtyUniforms = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001350
Jamie Madill6246dc82014-01-29 09:26:47 -05001351 if (vertexShader != mAppliedVertexShader)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001352 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001353 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1354 mAppliedVertexShader = vertexShader;
1355 dirtyUniforms = true;
1356 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001357
Jamie Madill6246dc82014-01-29 09:26:47 -05001358 if (geometryShader != mAppliedGeometryShader)
1359 {
1360 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1361 mAppliedGeometryShader = geometryShader;
1362 dirtyUniforms = true;
1363 }
1364
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001365 if (geometryExe && mCurRasterState.pointDrawMode)
1366 {
1367 mCurPointGeometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
1368 }
1369 else
1370 {
1371 mCurPointGeometryShader = NULL;
1372 }
1373
Jamie Madill6246dc82014-01-29 09:26:47 -05001374 if (pixelShader != mAppliedPixelShader)
1375 {
1376 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1377 mAppliedPixelShader = pixelShader;
1378 dirtyUniforms = true;
1379 }
1380
1381 if (dirtyUniforms)
1382 {
1383 programBinary->dirtyAllUniforms();
daniel@transgaming.come4991412012-12-20 20:55:34 +00001384 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001385}
1386
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001387void Renderer11::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001388{
Jamie Madill834e8b72014-04-11 13:33:58 -04001389 const std::vector<gl::LinkedUniform*> &uniformArray = programBinary.getUniforms();
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001390
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001391 unsigned int totalRegisterCountVS = 0;
1392 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001393
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001394 bool vertexUniformsDirty = false;
1395 bool pixelUniformsDirty = false;
1396
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001397 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001398 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001399 const gl::LinkedUniform &uniform = *uniformArray[uniformIndex];
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001400
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001401 if (uniform.isReferencedByVertexShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001402 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001403 totalRegisterCountVS += uniform.registerCount;
1404 vertexUniformsDirty = (vertexUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001405 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001406
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001407 if (uniform.isReferencedByFragmentShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001408 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001409 totalRegisterCountPS += uniform.registerCount;
1410 pixelUniformsDirty = (pixelUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001411 }
1412 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001413
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001414 const UniformStorage11 *vertexUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getVertexUniformStorage());
1415 const UniformStorage11 *fragmentUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getFragmentUniformStorage());
1416 ASSERT(vertexUniformStorage);
1417 ASSERT(fragmentUniformStorage);
1418
1419 ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
1420 ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001421
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001422 float (*mapVS)[4] = NULL;
1423 float (*mapPS)[4] = NULL;
1424
Shannon Woods5ab33c82013-06-26 15:31:09 -04001425 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1426 {
1427 D3D11_MAPPED_SUBRESOURCE map = {0};
1428 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001429 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001430 ASSERT(SUCCEEDED(result));
1431 mapVS = (float(*)[4])map.pData;
1432 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001433
Shannon Woods5ab33c82013-06-26 15:31:09 -04001434 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1435 {
1436 D3D11_MAPPED_SUBRESOURCE map = {0};
1437 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001438 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001439 ASSERT(SUCCEEDED(result));
1440 mapPS = (float(*)[4])map.pData;
1441 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001442
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001443 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001444 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001445 gl::LinkedUniform *uniform = uniformArray[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001446
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001447 if (!uniform->isSampler())
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001448 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001449 unsigned int componentCount = (4 - uniform->registerElement);
1450
Jamie Madill71cc91f2013-09-18 12:51:22 -04001451 // 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 -04001452 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001453
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001454 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001455 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001456 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001457 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001458
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001459 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001460 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001461 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001462 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001463 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001464 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001465
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001466 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001467 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001468 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001469 }
1470
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001471 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001472 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001473 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001474 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001475
1476 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1477 {
1478 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1479 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1480 }
1481
1482 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1483 {
1484 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1485 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1486 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001487
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001488 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001489 if (!mDriverConstantBufferVS)
1490 {
1491 D3D11_BUFFER_DESC constantBufferDescription = {0};
1492 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1493 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1494 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1495 constantBufferDescription.CPUAccessFlags = 0;
1496 constantBufferDescription.MiscFlags = 0;
1497 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001498
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001499 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001500 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001501 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001502
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001503 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1504 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001505
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001506 if (!mDriverConstantBufferPS)
1507 {
1508 D3D11_BUFFER_DESC constantBufferDescription = {0};
1509 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1510 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1511 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1512 constantBufferDescription.CPUAccessFlags = 0;
1513 constantBufferDescription.MiscFlags = 0;
1514 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001515
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001516 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001517 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001518 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001519
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001520 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1521 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001522
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001523 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1524 {
1525 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1526 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1527 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001528
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001529 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1530 {
1531 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1532 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1533 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001534
1535 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001536 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1537 {
1538 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1539 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1540 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001541}
1542
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001543void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001544{
Geoff Langda507fe2013-08-20 12:01:42 -04001545 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001546 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001547}
1548
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001549void Renderer11::markAllStateDirty()
1550{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001551 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1552 {
1553 mAppliedRenderTargetSerials[rtIndex] = 0;
1554 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001555 mAppliedDepthbufferSerial = 0;
1556 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001557 mDepthStencilInitialized = false;
1558 mRenderTargetDescInitialized = false;
1559
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001560 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001561 {
1562 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001563 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001564 }
1565 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1566 {
1567 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001568 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001569 }
1570
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001571 mForceSetBlendState = true;
1572 mForceSetRasterState = true;
1573 mForceSetDepthStencilState = true;
1574 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001575 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001576
Geoff Lang7840b172014-03-13 11:20:44 -04001577 mAppliedIB = NULL;
1578 mAppliedIBFormat = DXGI_FORMAT_UNKNOWN;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001579 mAppliedIBOffset = 0;
1580
Jamie Madill6246dc82014-01-29 09:26:47 -05001581 mAppliedVertexShader = NULL;
1582 mAppliedGeometryShader = NULL;
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001583 mCurPointGeometryShader = NULL;
Jamie Madill6246dc82014-01-29 09:26:47 -05001584 mAppliedPixelShader = NULL;
Geoff Langeeba6e12014-02-03 13:12:30 -05001585
1586 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1587 {
1588 mAppliedTFBuffers[i] = NULL;
1589 mAppliedTFOffsets[i] = 0;
1590 }
1591
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001592 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1593 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001594
1595 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001596
1597 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1598 {
1599 mCurrentConstantBufferVS[i] = -1;
1600 mCurrentConstantBufferPS[i] = -1;
1601 }
1602
1603 mCurrentVertexConstantBuffer = NULL;
1604 mCurrentPixelConstantBuffer = NULL;
1605 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001606
1607 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001608}
1609
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001610void Renderer11::releaseDeviceResources()
1611{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001612 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001613 mInputLayoutCache.clear();
1614
Geoff Langea228632013-07-30 15:17:12 -04001615 SafeDelete(mVertexDataManager);
1616 SafeDelete(mIndexDataManager);
1617 SafeDelete(mLineLoopIB);
1618 SafeDelete(mTriangleFanIB);
1619 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001620 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001621 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001622
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001623 SafeRelease(mDriverConstantBufferVS);
1624 SafeRelease(mDriverConstantBufferPS);
1625 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001626}
1627
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001628void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001629{
1630 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001631 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001632}
1633
1634bool Renderer11::isDeviceLost()
1635{
1636 return mDeviceLost;
1637}
1638
1639// set notify to true to broadcast a message to all contexts of the device loss
1640bool Renderer11::testDeviceLost(bool notify)
1641{
1642 bool isLost = false;
1643
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001644 // GetRemovedReason is used to test if the device is removed
1645 HRESULT result = mDevice->GetDeviceRemovedReason();
1646 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001647
1648 if (isLost)
1649 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001650 // Log error if this is a new device lost event
1651 if (mDeviceLost == false)
1652 {
1653 ERR("The D3D11 device was removed: 0x%08X", result);
1654 }
1655
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001656 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001657 // we'll probably get this done again by notifyDeviceLost
1658 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001659 // Note that we don't want to clear the device loss status here
1660 // -- this needs to be done by resetDevice
1661 mDeviceLost = true;
1662 if (notify)
1663 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001664 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001665 }
1666 }
1667
1668 return isLost;
1669}
1670
1671bool Renderer11::testDeviceResettable()
1672{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001673 // determine if the device is resettable by creating a dummy device
1674 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001675
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001676 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001677 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001678 return false;
1679 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001680
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001681 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001682 {
1683 D3D_FEATURE_LEVEL_11_0,
1684 D3D_FEATURE_LEVEL_10_1,
1685 D3D_FEATURE_LEVEL_10_0,
1686 };
1687
1688 ID3D11Device* dummyDevice;
1689 D3D_FEATURE_LEVEL dummyFeatureLevel;
1690 ID3D11DeviceContext* dummyContext;
1691
1692 HRESULT result = D3D11CreateDevice(NULL,
1693 D3D_DRIVER_TYPE_HARDWARE,
1694 NULL,
1695 #if defined(_DEBUG)
1696 D3D11_CREATE_DEVICE_DEBUG,
1697 #else
1698 0,
1699 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001700 featureLevels,
1701 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001702 D3D11_SDK_VERSION,
1703 &dummyDevice,
1704 &dummyFeatureLevel,
1705 &dummyContext);
1706
1707 if (!mDevice || FAILED(result))
1708 {
1709 return false;
1710 }
1711
Geoff Langea228632013-07-30 15:17:12 -04001712 SafeRelease(dummyContext);
1713 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001714
1715 return true;
1716}
1717
1718void Renderer11::release()
1719{
1720 releaseDeviceResources();
1721
Geoff Langea228632013-07-30 15:17:12 -04001722 SafeRelease(mDxgiFactory);
1723 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001724
1725 if (mDeviceContext)
1726 {
1727 mDeviceContext->ClearState();
1728 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001729 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001730 }
1731
Geoff Langea228632013-07-30 15:17:12 -04001732 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001733
1734 if (mD3d11Module)
1735 {
1736 FreeLibrary(mD3d11Module);
1737 mD3d11Module = NULL;
1738 }
1739
1740 if (mDxgiModule)
1741 {
1742 FreeLibrary(mDxgiModule);
1743 mDxgiModule = NULL;
1744 }
Geoff Langdad5ed32014-02-10 12:59:17 -05001745
1746 mCompiler.release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001747}
1748
1749bool Renderer11::resetDevice()
1750{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001751 // recreate everything
1752 release();
1753 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001754
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001755 if (result != EGL_SUCCESS)
1756 {
1757 ERR("Could not reinitialize D3D11 device: %08X", result);
1758 return false;
1759 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001760
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001761 mDeviceLost = false;
1762
1763 return true;
1764}
1765
1766DWORD Renderer11::getAdapterVendor() const
1767{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001768 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001769}
1770
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001771std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001772{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001773 std::ostringstream rendererString;
1774
1775 rendererString << mDescription;
1776 rendererString << " Direct3D11";
1777
1778 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1779 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1780
1781 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001782}
1783
1784GUID Renderer11::getAdapterIdentifier() const
1785{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001786 // Use the adapter LUID as our adapter ID
1787 // This number is local to a machine is only guaranteed to be unique between restarts
1788 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1789 GUID adapterId = {0};
1790 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1791 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001792}
1793
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001794unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001795{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001796 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1797 switch (mFeatureLevel)
1798 {
1799 case D3D_FEATURE_LEVEL_11_0:
1800 case D3D_FEATURE_LEVEL_10_1:
1801 case D3D_FEATURE_LEVEL_10_0:
1802 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1803 default: UNREACHABLE();
1804 return 0;
1805 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001806}
1807
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001808unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1809{
1810 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1811}
1812
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001813unsigned int Renderer11::getReservedVertexUniformVectors() const
1814{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001815 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001816}
1817
1818unsigned int Renderer11::getReservedFragmentUniformVectors() const
1819{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001820 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001821}
1822
1823unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001824{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001825 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1826 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1827 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001828}
1829
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001830unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001831{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001832 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1833 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1834 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001835}
1836
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001837unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001838{
1839 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001840 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1841 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001842 switch (mFeatureLevel)
1843 {
1844 case D3D_FEATURE_LEVEL_11_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001845 return D3D11_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001846 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001847 return D3D10_1_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001848 case D3D_FEATURE_LEVEL_10_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001849 return D3D10_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001850 default: UNREACHABLE();
1851 return 0;
1852 }
1853}
1854
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001855unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
1856{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001857 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1858 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1859
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001860 switch (mFeatureLevel)
1861 {
1862 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001863 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001864 case D3D_FEATURE_LEVEL_10_1:
1865 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001866 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001867 default: UNREACHABLE();
1868 return 0;
1869 }
1870}
1871
1872unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
1873{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001874 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1875 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1876
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001877 switch (mFeatureLevel)
1878 {
1879 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001880 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001881 case D3D_FEATURE_LEVEL_10_1:
1882 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001883 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001884 default: UNREACHABLE();
1885 return 0;
1886 }
1887}
1888
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001889unsigned int Renderer11::getReservedVertexUniformBuffers() const
1890{
1891 // we reserve one buffer for the application uniforms, and one for driver uniforms
1892 return 2;
1893}
1894
1895unsigned int Renderer11::getReservedFragmentUniformBuffers() const
1896{
1897 // we reserve one buffer for the application uniforms, and one for driver uniforms
1898 return 2;
1899}
1900
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001901unsigned int Renderer11::getReservedVaryings() const
1902{
Jamie Madill2bf8b372014-06-16 17:18:51 -04001903 // We potentially reserve varyings for gl_Position, dx_Position, gl_FragCoord and gl_PointSize
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001904 return 4;
1905}
1906
1907
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001908unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
1909{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001910 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
1911 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
1912
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001913 switch (mFeatureLevel)
1914 {
1915 case D3D_FEATURE_LEVEL_11_0:
1916 return D3D11_SO_BUFFER_SLOT_COUNT;
1917 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001918 return D3D10_1_SO_BUFFER_SLOT_COUNT;
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001919 case D3D_FEATURE_LEVEL_10_0:
1920 return D3D10_SO_BUFFER_SLOT_COUNT;
1921 default: UNREACHABLE();
1922 return 0;
1923 }
1924}
1925
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001926unsigned int Renderer11::getMaxTransformFeedbackSeparateComponents() const
1927{
1928 switch (mFeatureLevel)
1929 {
1930 case D3D_FEATURE_LEVEL_11_0:
1931 return getMaxTransformFeedbackInterleavedComponents() / getMaxTransformFeedbackBuffers();
1932 case D3D_FEATURE_LEVEL_10_1:
1933 case D3D_FEATURE_LEVEL_10_0:
1934 // D3D 10 and 10.1 only allow one output per output slot if an output slot other than zero
1935 // is used.
1936 return 4;
1937 default: UNREACHABLE();
1938 return 0;
1939 }
1940}
1941
1942unsigned int Renderer11::getMaxTransformFeedbackInterleavedComponents() const
1943{
1944 return (getMaxVaryingVectors() * 4);
1945}
1946
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00001947unsigned int Renderer11::getMaxUniformBufferSize() const
1948{
1949 // Each component is a 4-element vector of 4-byte units (floats)
1950 const unsigned int bytesPerComponent = 4 * sizeof(float);
1951
1952 switch (mFeatureLevel)
1953 {
1954 case D3D_FEATURE_LEVEL_11_0:
1955 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1956 case D3D_FEATURE_LEVEL_10_1:
1957 case D3D_FEATURE_LEVEL_10_0:
1958 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1959 default: UNREACHABLE();
1960 return 0;
1961 }
1962}
1963
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001964bool Renderer11::getShareHandleSupport() const
1965{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00001966 // We only currently support share handles with BGRA surfaces, because
1967 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001968 // PIX doesn't seem to support using share handles, so disable them.
Geoff Langcec35902014-04-16 10:52:36 -04001969 return getCaps().extensions.textureFormatBGRA8888 && !gl::perfActive();
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00001970}
1971
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00001972bool Renderer11::getPostSubBufferSupport() const
1973{
1974 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
1975 return false;
1976}
1977
Jamie Madill13a2f852013-12-11 16:35:08 -05001978int Renderer11::getMaxRecommendedElementsIndices() const
1979{
1980 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
1981 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
1982
1983 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
1984 return std::numeric_limits<GLint>::max();
1985}
1986
1987int Renderer11::getMaxRecommendedElementsVertices() const
1988{
1989 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
1990 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
1991
1992 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
1993 return std::numeric_limits<GLint>::max();
1994}
1995
Geoff Lang05b05022014-06-11 15:31:45 -04001996bool Renderer11::getSRGBTextureSupport() const
1997{
1998 return true;
1999}
2000
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002001int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002002{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002003 switch (mFeatureLevel)
2004 {
2005 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002006 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002007 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2008 default: UNREACHABLE(); return 0;
2009 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002010}
2011
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002012int Renderer11::getMinorShaderModel() const
2013{
2014 switch (mFeatureLevel)
2015 {
2016 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2017 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2018 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2019 default: UNREACHABLE(); return 0;
2020 }
2021}
2022
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002023int Renderer11::getMinSwapInterval() const
2024{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002025 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002026}
2027
2028int Renderer11::getMaxSwapInterval() const
2029{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002030 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002031}
2032
2033int Renderer11::getMaxSupportedSamples() const
2034{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002035 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002036}
2037
Geoff Lang005df412013-10-16 14:12:50 -04002038GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002039{
Geoff Lange4a492b2014-06-19 14:14:41 -04002040 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat);
Geoff Lang0e120e32013-05-29 10:23:55 -04002041 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2042 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2043}
2044
Geoff Lang005df412013-10-16 14:12:50 -04002045GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002046{
2047 unsigned int numCounts = 0;
2048
2049 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Lange4a492b2014-06-19 14:14:41 -04002050 GLenum componentType = gl::GetComponentType(internalFormat);
Geoff Langb2f3d052013-08-13 12:49:27 -04002051 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002052 {
Geoff Lange4a492b2014-06-19 14:14:41 -04002053 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat);
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002054 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2055
2056 if (iter != mMultisampleSupportMap.end())
2057 {
2058 const MultisampleSupportInfo& info = iter->second;
2059 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2060 {
2061 if (info.qualityLevels[i] > 0)
2062 {
2063 numCounts++;
2064 }
2065 }
2066 }
2067 }
2068
2069 return numCounts;
2070}
2071
Geoff Lang005df412013-10-16 14:12:50 -04002072void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002073{
2074 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Lange4a492b2014-06-19 14:14:41 -04002075 GLenum componentType = gl::GetComponentType(internalFormat);
Geoff Langb2f3d052013-08-13 12:49:27 -04002076 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2077 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002078 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002079 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002080
Geoff Lange4a492b2014-06-19 14:14:41 -04002081 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat);
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002082 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2083
2084 if (iter != mMultisampleSupportMap.end())
2085 {
2086 const MultisampleSupportInfo& info = iter->second;
2087 int bufPos = 0;
2088 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2089 {
2090 if (info.qualityLevels[i] > 0)
2091 {
2092 params[bufPos++] = i + 1;
2093 }
2094 }
2095 }
2096}
2097
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002098int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2099{
2100 if (requested == 0)
2101 {
2102 return 0;
2103 }
2104
2105 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2106 if (iter != mMultisampleSupportMap.end())
2107 {
2108 const MultisampleSupportInfo& info = iter->second;
2109 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2110 {
2111 if (info.qualityLevels[i] > 0)
2112 {
2113 return i + 1;
2114 }
2115 }
2116 }
2117
2118 return -1;
2119}
2120
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002121bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002122{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002123 if (source && dest)
2124 {
2125 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2126 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2127
Nicolas Capens76b258f2014-04-03 10:59:42 -04002128 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002129
2130 dest11->invalidateSwizzleCache();
2131
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002132 return true;
2133 }
2134
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002135 return false;
2136}
2137
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002138bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002139{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002140 if (source && dest)
2141 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002142 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2143 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002144
Nicolas Capens76b258f2014-04-03 10:59:42 -04002145 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002146
2147 dest11->invalidateSwizzleCache();
2148
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002149 return true;
2150 }
2151
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002152 return false;
2153}
2154
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002155bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2156{
2157 if (source && dest)
2158 {
2159 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2160 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2161
Nicolas Capens76b258f2014-04-03 10:59:42 -04002162 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002163
2164 dest11->invalidateSwizzleCache();
2165
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002166 return true;
2167 }
2168
2169 return false;
2170}
2171
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002172bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2173{
2174 if (source && dest)
2175 {
2176 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2177 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2178
Nicolas Capens76b258f2014-04-03 10:59:42 -04002179 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002180
2181 dest11->invalidateSwizzleCache();
2182
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002183 return true;
2184 }
2185
2186 return false;
2187}
2188
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002189bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002190 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002191{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002192 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002193 if (!colorbuffer)
2194 {
2195 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002196 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002197 }
2198
2199 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2200 if (!sourceRenderTarget)
2201 {
2202 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002203 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002204 }
2205
2206 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2207 if (!source)
2208 {
2209 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002210 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002211 }
2212
2213 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2214 if (!storage11)
2215 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002216 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002217 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002218 }
2219
2220 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2221 if (!destRenderTarget)
2222 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002223 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002224 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002225 }
2226
2227 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2228 if (!dest)
2229 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002230 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002231 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002232 }
2233
Geoff Langb86b9792013-06-04 16:32:05 -04002234 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2235 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002236
Geoff Langb86b9792013-06-04 16:32:05 -04002237 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2238 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002239
Geoff Langb86b9792013-06-04 16:32:05 -04002240 // Use nearest filtering because source and destination are the same size for the direct
2241 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002242 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002243 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002244
Geoff Lang42477a42013-09-17 17:07:02 -04002245 storage11->invalidateSwizzleCacheLevel(level);
2246
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002247 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002248}
2249
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002250bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002251 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002252{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002253 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002254 if (!colorbuffer)
2255 {
2256 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002257 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002258 }
2259
2260 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2261 if (!sourceRenderTarget)
2262 {
2263 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002264 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002265 }
2266
2267 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2268 if (!source)
2269 {
2270 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002271 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002272 }
2273
2274 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2275 if (!storage11)
2276 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002277 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002278 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002279 }
2280
Nicolas Capensb13f8662013-06-04 13:30:19 -04002281 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002282 if (!destRenderTarget)
2283 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002284 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002285 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002286 }
2287
2288 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2289 if (!dest)
2290 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002291 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002292 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002293 }
2294
Geoff Langb86b9792013-06-04 16:32:05 -04002295 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2296 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002297
Geoff Langb86b9792013-06-04 16:32:05 -04002298 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2299 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002300
Geoff Langb86b9792013-06-04 16:32:05 -04002301 // Use nearest filtering because source and destination are the same size for the direct
2302 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002303 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002304 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002305
Geoff Lang42477a42013-09-17 17:07:02 -04002306 storage11->invalidateSwizzleCacheLevel(level);
2307
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002308 return ret;
2309}
2310
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002311bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2312 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2313{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002314 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002315 if (!colorbuffer)
2316 {
2317 ERR("Failed to retrieve the color buffer from the frame buffer.");
2318 return gl::error(GL_OUT_OF_MEMORY, false);
2319 }
2320
2321 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2322 if (!sourceRenderTarget)
2323 {
2324 ERR("Failed to retrieve the render target from the frame buffer.");
2325 return gl::error(GL_OUT_OF_MEMORY, false);
2326 }
2327
2328 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2329 if (!source)
2330 {
2331 ERR("Failed to retrieve the render target view from the render target.");
2332 return gl::error(GL_OUT_OF_MEMORY, false);
2333 }
2334
2335 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2336 if (!storage11)
2337 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002338 ERR("Failed to retrieve the texture storage from the destination.");
2339 return gl::error(GL_OUT_OF_MEMORY, false);
2340 }
2341
2342 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2343 if (!destRenderTarget)
2344 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002345 ERR("Failed to retrieve the render target from the destination storage.");
2346 return gl::error(GL_OUT_OF_MEMORY, false);
2347 }
2348
2349 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2350 if (!dest)
2351 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002352 ERR("Failed to retrieve the render target view from the destination render target.");
2353 return gl::error(GL_OUT_OF_MEMORY, false);
2354 }
2355
Geoff Langb86b9792013-06-04 16:32:05 -04002356 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2357 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002358
Geoff Langb86b9792013-06-04 16:32:05 -04002359 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2360 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002361
Geoff Langb86b9792013-06-04 16:32:05 -04002362 // Use nearest filtering because source and destination are the same size for the direct
2363 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002364 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002365 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002366
Geoff Lang42477a42013-09-17 17:07:02 -04002367 storage11->invalidateSwizzleCacheLevel(level);
2368
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002369 return ret;
2370}
2371
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002372bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2373 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2374{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002375 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002376 if (!colorbuffer)
2377 {
2378 ERR("Failed to retrieve the color buffer from the frame buffer.");
2379 return gl::error(GL_OUT_OF_MEMORY, false);
2380 }
2381
2382 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2383 if (!sourceRenderTarget)
2384 {
2385 ERR("Failed to retrieve the render target from the frame buffer.");
2386 return gl::error(GL_OUT_OF_MEMORY, false);
2387 }
2388
2389 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2390 if (!source)
2391 {
2392 ERR("Failed to retrieve the render target view from the render target.");
2393 return gl::error(GL_OUT_OF_MEMORY, false);
2394 }
2395
2396 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2397 if (!storage11)
2398 {
Geoff Langea228632013-07-30 15:17:12 -04002399 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002400 ERR("Failed to retrieve the texture storage from the destination.");
2401 return gl::error(GL_OUT_OF_MEMORY, false);
2402 }
2403
2404 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2405 if (!destRenderTarget)
2406 {
Geoff Langea228632013-07-30 15:17:12 -04002407 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002408 ERR("Failed to retrieve the render target from the destination storage.");
2409 return gl::error(GL_OUT_OF_MEMORY, false);
2410 }
2411
2412 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2413 if (!dest)
2414 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002415 ERR("Failed to retrieve the render target view from the destination render target.");
2416 return gl::error(GL_OUT_OF_MEMORY, false);
2417 }
2418
Geoff Langb86b9792013-06-04 16:32:05 -04002419 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2420 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002421
Geoff Langb86b9792013-06-04 16:32:05 -04002422 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2423 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002424
Geoff Langb86b9792013-06-04 16:32:05 -04002425 // Use nearest filtering because source and destination are the same size for the direct
2426 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002427 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002428 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002429
Geoff Lang42477a42013-09-17 17:07:02 -04002430 storage11->invalidateSwizzleCacheLevel(level);
2431
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002432 return ret;
2433}
2434
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002435void Renderer11::unapplyRenderTargets()
2436{
2437 setOneTimeRenderTarget(NULL);
2438}
2439
2440void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2441{
2442 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2443
2444 rtvArray[0] = renderTargetView;
2445
Geoff Langaae65a42014-05-26 12:43:44 -04002446 mDeviceContext->OMSetRenderTargets(getCaps().maxDrawBuffers, rtvArray, NULL);
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002447
2448 // Do not preserve the serial for this one-time-use render target
2449 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2450 {
2451 mAppliedRenderTargetSerials[rtIndex] = 0;
2452 }
2453}
2454
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002455RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2456{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002457 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002458 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002459
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002460 if (depth)
2461 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002462 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002463 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002464 swapChain11->getDepthStencilTexture(),
2465 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002466 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002467 }
2468 else
2469 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002470 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002471 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002472 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002473 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002474 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002475 }
2476 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002477}
2478
Geoff Langa2d97f12013-06-11 11:44:02 -04002479RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002480{
Geoff Langa2d97f12013-06-11 11:44:02 -04002481 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002482 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002483}
2484
Geoff Lang48dcae72014-02-05 16:28:24 -05002485ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type,
2486 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2487 bool separatedOutputBuffers)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002488{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002489 ShaderExecutable11 *executable = NULL;
Geoff Lang48dcae72014-02-05 16:28:24 -05002490 HRESULT result;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002491
2492 switch (type)
2493 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002494 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002495 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002496 ID3D11VertexShader *vertexShader = NULL;
2497 ID3D11GeometryShader *streamOutShader = NULL;
2498
2499 result = mDevice->CreateVertexShader(function, length, NULL, &vertexShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002500 ASSERT(SUCCEEDED(result));
2501
Geoff Lang48dcae72014-02-05 16:28:24 -05002502 if (transformFeedbackVaryings.size() > 0)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002503 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002504 std::vector<D3D11_SO_DECLARATION_ENTRY> soDeclaration;
2505 for (size_t i = 0; i < transformFeedbackVaryings.size(); i++)
2506 {
2507 const gl::LinkedVarying &varying = transformFeedbackVaryings[i];
Jamie Madill6195ef82014-06-18 10:09:43 -04002508 GLenum transposedType = gl::TransposeMatrixType(varying.type);
2509
Geoff Langcebb5aa2014-04-07 14:13:40 -04002510 for (size_t j = 0; j < varying.semanticIndexCount; j++)
Geoff Lang48dcae72014-02-05 16:28:24 -05002511 {
2512 D3D11_SO_DECLARATION_ENTRY entry = { 0 };
2513 entry.Stream = 0;
Geoff Langcebb5aa2014-04-07 14:13:40 -04002514 entry.SemanticName = varying.semanticName.c_str();
2515 entry.SemanticIndex = varying.semanticIndex + j;
Geoff Lang48dcae72014-02-05 16:28:24 -05002516 entry.StartComponent = 0;
Jamie Madill6195ef82014-06-18 10:09:43 -04002517 entry.ComponentCount = gl::VariableColumnCount(transposedType);
Geoff Lang48dcae72014-02-05 16:28:24 -05002518 entry.OutputSlot = (separatedOutputBuffers ? i : 0);
2519 soDeclaration.push_back(entry);
2520 }
2521 }
2522
2523 result = mDevice->CreateGeometryShaderWithStreamOutput(function, length, soDeclaration.data(), soDeclaration.size(),
2524 NULL, 0, 0, NULL, &streamOutShader);
2525 ASSERT(SUCCEEDED(result));
2526 }
2527
2528 if (vertexShader)
2529 {
2530 executable = new ShaderExecutable11(function, length, vertexShader, streamOutShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002531 }
2532 }
2533 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002534 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002535 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002536 ID3D11PixelShader *pixelShader = NULL;
2537
2538 result = mDevice->CreatePixelShader(function, length, NULL, &pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002539 ASSERT(SUCCEEDED(result));
2540
Geoff Lang48dcae72014-02-05 16:28:24 -05002541 if (pixelShader)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002542 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002543 executable = new ShaderExecutable11(function, length, pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002544 }
2545 }
2546 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002547 case rx::SHADER_GEOMETRY:
2548 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002549 ID3D11GeometryShader *geometryShader = NULL;
2550
2551 result = mDevice->CreateGeometryShader(function, length, NULL, &geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002552 ASSERT(SUCCEEDED(result));
2553
Geoff Lang48dcae72014-02-05 16:28:24 -05002554 if (geometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002555 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002556 executable = new ShaderExecutable11(function, length, geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002557 }
2558 }
2559 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002560 default:
2561 UNREACHABLE();
2562 break;
2563 }
2564
2565 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002566}
2567
Geoff Lang48dcae72014-02-05 16:28:24 -05002568ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
2569 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2570 bool separatedOutputBuffers, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002571{
Geoff Lang6e05c272014-03-17 10:46:54 -07002572 const char *profileType = NULL;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002573 switch (type)
2574 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002575 case rx::SHADER_VERTEX:
Geoff Lang6e05c272014-03-17 10:46:54 -07002576 profileType = "vs";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002577 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002578 case rx::SHADER_PIXEL:
Geoff Lang6e05c272014-03-17 10:46:54 -07002579 profileType = "ps";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002580 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002581 case rx::SHADER_GEOMETRY:
Geoff Lang6e05c272014-03-17 10:46:54 -07002582 profileType = "gs";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002583 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002584 default:
2585 UNREACHABLE();
2586 return NULL;
2587 }
2588
Geoff Lang6e05c272014-03-17 10:46:54 -07002589 const char *profileVersion = NULL;
2590 switch (mFeatureLevel)
2591 {
2592 case D3D_FEATURE_LEVEL_11_0:
2593 profileVersion = "5_0";
2594 break;
2595 case D3D_FEATURE_LEVEL_10_1:
2596 profileVersion = "4_1";
2597 break;
2598 case D3D_FEATURE_LEVEL_10_0:
2599 profileVersion = "4_0";
2600 break;
2601 default:
2602 UNREACHABLE();
2603 return NULL;
2604 }
2605
2606 char profile[32];
2607 snprintf(profile, ArraySize(profile), "%s_%s", profileType, profileVersion);
2608
Nicolas Capens93faad92014-05-10 12:14:13 -04002609 UINT flags = D3DCOMPILE_OPTIMIZATION_LEVEL0;
2610
2611 if (gl::perfActive())
2612 {
2613#ifndef NDEBUG
2614 flags = D3DCOMPILE_SKIP_OPTIMIZATION;
2615#endif
2616
2617 flags |= D3DCOMPILE_DEBUG;
2618
2619 std::string sourcePath = getTempPath();
2620 std::string sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(shaderHLSL);
2621 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
2622 }
2623
2624 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
2625 // Try the default flags first and if compilation fails, try some alternatives.
2626 const UINT extraFlags[] =
2627 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002628 flags,
Nicolas Capens2c27db62014-05-10 12:21:11 -04002629 flags | D3DCOMPILE_SKIP_VALIDATION,
2630 flags | D3DCOMPILE_SKIP_OPTIMIZATION
Nicolas Capens93faad92014-05-10 12:14:13 -04002631 };
2632
2633 const static char *extraFlagNames[] =
2634 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002635 "default",
Nicolas Capens2c27db62014-05-10 12:21:11 -04002636 "skip validation",
2637 "skip optimization"
Nicolas Capens93faad92014-05-10 12:14:13 -04002638 };
2639
2640 int attempts = ArraySize(extraFlags);
2641
2642 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, extraFlags, extraFlagNames, attempts);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002643 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002644 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002645 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002646 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002647
Geoff Lang48dcae72014-02-05 16:28:24 -05002648 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type,
2649 transformFeedbackVaryings, separatedOutputBuffers);
Geoff Langea228632013-07-30 15:17:12 -04002650 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002651
2652 return executable;
2653}
2654
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002655rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2656{
2657 return new UniformStorage11(this, storageSize);
2658}
2659
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002660VertexBuffer *Renderer11::createVertexBuffer()
2661{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002662 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002663}
2664
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002665IndexBuffer *Renderer11::createIndexBuffer()
2666{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002667 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002668}
2669
Brandon Jonesd38f9262014-06-18 16:26:45 -07002670BufferImpl *Renderer11::createBuffer()
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002671{
Brandon Jonesd38f9262014-06-18 16:26:45 -07002672 return new Buffer11(this);
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002673}
2674
Brandon Jones5bf98292014-06-06 17:19:38 -07002675VertexArrayImpl *Renderer11::createVertexArray()
2676{
2677 return new VertexArray11(this);
2678}
2679
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002680QueryImpl *Renderer11::createQuery(GLenum type)
2681{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002682 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002683}
2684
2685FenceImpl *Renderer11::createFence()
2686{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002687 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002688}
2689
Geoff Lang005df412013-10-16 14:12:50 -04002690bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002691{
Geoff Lang2b31af22014-05-23 14:45:40 -04002692 ASSERT(getCaps().extensions.pixelBufferObject);
Jamie Madill4461f092013-10-10 15:10:39 -04002693
Jamie Madill4461f092013-10-10 15:10:39 -04002694 // sRGB formats do not work with D3D11 buffer SRVs
Geoff Lange4a492b2014-06-19 14:14:41 -04002695 if (gl::GetColorEncoding(internalFormat) == GL_SRGB)
Jamie Madill4461f092013-10-10 15:10:39 -04002696 {
2697 return false;
2698 }
2699
2700 // We cannot support direct copies to non-color-renderable formats
Geoff Langcec35902014-04-16 10:52:36 -04002701 if (!getCaps().textureCaps.get(internalFormat).colorRendering)
Jamie Madill4461f092013-10-10 15:10:39 -04002702 {
2703 return false;
2704 }
2705
2706 // We skip all 3-channel formats since sometimes format support is missing
Geoff Lange4a492b2014-06-19 14:14:41 -04002707 if (gl::GetComponentCount(internalFormat) == 3)
Jamie Madill4461f092013-10-10 15:10:39 -04002708 {
2709 return false;
2710 }
2711
2712 // We don't support formats which we can't represent without conversion
2713 if (getNativeTextureFormat(internalFormat) != internalFormat)
2714 {
2715 return false;
2716 }
2717
2718 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002719}
2720
Jamie Madilla21eea32013-09-18 14:36:25 -04002721bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2722 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2723{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002724 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002725 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2726}
2727
Jamie Madill3c7fa222014-06-05 13:08:51 -04002728bool Renderer11::getRenderTargetResource(gl::FramebufferAttachment *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002729{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002730 ASSERT(colorbuffer != NULL);
2731
2732 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2733 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002734 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002735 *subresourceIndex = renderTarget->getSubresourceIndex();
2736
2737 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2738 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002739 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002740 ID3D11Resource *textureResource = NULL;
2741 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002742
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002743 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002744 {
Geoff Lang8e328842014-02-10 13:11:20 -05002745 HRESULT result = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002746 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002747
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002748 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002749 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002750 return true;
2751 }
2752 else
2753 {
2754 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2755 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002756 }
2757 }
2758 }
2759 }
2760
2761 return false;
2762}
2763
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002764bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002765 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002766{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002767 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002768 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002769 gl::FramebufferAttachment *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002770
2771 if (!readBuffer)
2772 {
2773 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2774 return gl::error(GL_OUT_OF_MEMORY, false);
2775 }
2776
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002777 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002778
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002779 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002780 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002781 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2782 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002783 gl::FramebufferAttachment *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002784
2785 if (!drawBuffer)
2786 {
2787 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2788 return gl::error(GL_OUT_OF_MEMORY, false);
2789 }
2790
2791 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2792
Geoff Lang125deab2013-08-09 13:34:16 -04002793 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002794 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002795 {
2796 return false;
2797 }
2798 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002799 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002800 }
2801
Geoff Lang685806d2013-06-12 11:16:36 -04002802 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002803 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002804 gl::FramebufferAttachment *readBuffer = readTarget->getDepthOrStencilbuffer();
2805 gl::FramebufferAttachment *drawBuffer = drawTarget->getDepthOrStencilbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002806
2807 if (!readBuffer)
2808 {
2809 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2810 return gl::error(GL_OUT_OF_MEMORY, false);
2811 }
2812
2813 if (!drawBuffer)
2814 {
2815 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2816 return gl::error(GL_OUT_OF_MEMORY, false);
2817 }
2818
2819 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2820 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2821
Geoff Lang125deab2013-08-09 13:34:16 -04002822 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002823 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002824 {
2825 return false;
2826 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002827 }
2828
Geoff Lang42477a42013-09-17 17:07:02 -04002829 invalidateFramebufferSwizzles(drawTarget);
2830
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002831 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002832}
2833
Jamie Madilleb9baab2014-03-24 13:19:43 -04002834void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
2835 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002836{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002837 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002838 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002839
Jamie Madill3c7fa222014-06-05 13:08:51 -04002840 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002841
2842 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002843 {
2844 gl::Rectangle area;
2845 area.x = x;
2846 area.y = y;
2847 area.width = width;
2848 area.height = height;
2849
Jamie Madill1ef6fe72014-05-01 14:51:05 -04002850 if (pack.pixelBuffer.get() != NULL)
2851 {
Brandon Jonesd38f9262014-06-18 16:26:45 -07002852 rx::Buffer11 *packBufferStorage = Buffer11::makeBuffer11(pack.pixelBuffer.get()->getImplementation());
Jamie Madill1ef6fe72014-05-01 14:51:05 -04002853 PackPixelsParams packParams(area, format, type, outputPitch, pack, reinterpret_cast<ptrdiff_t>(pixels));
2854 packBufferStorage->packPixels(colorBufferTexture, subresourceIndex, packParams);
2855 }
2856 else
2857 {
2858 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch, pack, pixels);
2859 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002860
Geoff Langea228632013-07-30 15:17:12 -04002861 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002862 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002863}
2864
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002865Image *Renderer11::createImage()
2866{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002867 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002868}
2869
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002870void Renderer11::generateMipmap(Image *dest, Image *src)
2871{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002872 Image11 *dest11 = Image11::makeImage11(dest);
2873 Image11 *src11 = Image11::makeImage11(src);
Geoff Lange4a492b2014-06-19 14:14:41 -04002874 Image11::generateMipmap(dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002875}
2876
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002877TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
2878{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002879 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
2880 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002881}
2882
Nicolas Capensbf712d02014-03-31 14:23:35 -04002883TextureStorage *Renderer11::createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002884{
Nicolas Capensbf712d02014-03-31 14:23:35 -04002885 return new TextureStorage11_2D(this, internalformat, renderTarget, width, height, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002886}
2887
Nicolas Capensbf712d02014-03-31 14:23:35 -04002888TextureStorage *Renderer11::createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002889{
Nicolas Capensbf712d02014-03-31 14:23:35 -04002890 return new TextureStorage11_Cube(this, internalformat, renderTarget, size, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002891}
2892
Nicolas Capensbf712d02014-03-31 14:23:35 -04002893TextureStorage *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 +00002894{
Nicolas Capensbf712d02014-03-31 14:23:35 -04002895 return new TextureStorage11_3D(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00002896}
2897
Nicolas Capensbf712d02014-03-31 14:23:35 -04002898TextureStorage *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 +00002899{
Nicolas Capensbf712d02014-03-31 14:23:35 -04002900 return new TextureStorage11_2DArray(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002901}
2902
Jamie Madilleb9baab2014-03-24 13:19:43 -04002903void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format,
2904 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void *pixels)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002905{
Jamie Madill1eb5bd72014-03-28 10:43:39 -04002906 ASSERT(area.width >= 0);
2907 ASSERT(area.height >= 0);
2908
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002909 D3D11_TEXTURE2D_DESC textureDesc;
2910 texture->GetDesc(&textureDesc);
2911
Jamie Madill1eb5bd72014-03-28 10:43:39 -04002912 // Clamp read region to the defined texture boundaries, preventing out of bounds reads
2913 // and reads of uninitialized data.
2914 gl::Rectangle safeArea;
2915 safeArea.x = gl::clamp(area.x, 0, static_cast<int>(textureDesc.Width));
2916 safeArea.y = gl::clamp(area.y, 0, static_cast<int>(textureDesc.Height));
2917 safeArea.width = gl::clamp(area.width + std::min(area.x, 0), 0,
2918 static_cast<int>(textureDesc.Width) - safeArea.x);
2919 safeArea.height = gl::clamp(area.height + std::min(area.y, 0), 0,
2920 static_cast<int>(textureDesc.Height) - safeArea.y);
2921
2922 ASSERT(safeArea.x >= 0 && safeArea.y >= 0);
2923 ASSERT(safeArea.x + safeArea.width <= static_cast<int>(textureDesc.Width));
2924 ASSERT(safeArea.y + safeArea.height <= static_cast<int>(textureDesc.Height));
2925
2926 if (safeArea.width == 0 || safeArea.height == 0)
2927 {
2928 // no work to do
2929 return;
2930 }
2931
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002932 D3D11_TEXTURE2D_DESC stagingDesc;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04002933 stagingDesc.Width = safeArea.width;
2934 stagingDesc.Height = safeArea.height;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002935 stagingDesc.MipLevels = 1;
2936 stagingDesc.ArraySize = 1;
2937 stagingDesc.Format = textureDesc.Format;
2938 stagingDesc.SampleDesc.Count = 1;
2939 stagingDesc.SampleDesc.Quality = 0;
2940 stagingDesc.Usage = D3D11_USAGE_STAGING;
2941 stagingDesc.BindFlags = 0;
2942 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
2943 stagingDesc.MiscFlags = 0;
2944
2945 ID3D11Texture2D* stagingTex = NULL;
2946 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
2947 if (FAILED(result))
2948 {
2949 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
2950 return;
2951 }
2952
2953 ID3D11Texture2D* srcTex = NULL;
2954 if (textureDesc.SampleDesc.Count > 1)
2955 {
2956 D3D11_TEXTURE2D_DESC resolveDesc;
2957 resolveDesc.Width = textureDesc.Width;
2958 resolveDesc.Height = textureDesc.Height;
2959 resolveDesc.MipLevels = 1;
2960 resolveDesc.ArraySize = 1;
2961 resolveDesc.Format = textureDesc.Format;
2962 resolveDesc.SampleDesc.Count = 1;
2963 resolveDesc.SampleDesc.Quality = 0;
2964 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
2965 resolveDesc.BindFlags = 0;
2966 resolveDesc.CPUAccessFlags = 0;
2967 resolveDesc.MiscFlags = 0;
2968
2969 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
2970 if (FAILED(result))
2971 {
2972 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04002973 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002974 return;
2975 }
2976
2977 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
2978 subResource = 0;
2979 }
2980 else
2981 {
2982 srcTex = texture;
2983 srcTex->AddRef();
2984 }
2985
2986 D3D11_BOX srcBox;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04002987 srcBox.left = static_cast<UINT>(safeArea.x);
2988 srcBox.right = static_cast<UINT>(safeArea.x + safeArea.width);
2989 srcBox.top = static_cast<UINT>(safeArea.y);
2990 srcBox.bottom = static_cast<UINT>(safeArea.y + safeArea.height);
2991 srcBox.front = 0;
2992 srcBox.back = 1;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002993
2994 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
2995
Geoff Langea228632013-07-30 15:17:12 -04002996 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002997
Jamie Madill7538f7f2014-04-17 11:53:39 -04002998 PackPixelsParams packParams(safeArea, format, type, outputPitch, pack, 0);
2999 packPixels(stagingTex, packParams, pixels);
3000
3001 SafeRelease(stagingTex);
3002}
3003
3004void Renderer11::packPixels(ID3D11Texture2D *readTexture, const PackPixelsParams &params, void *pixelsOut)
3005{
3006 D3D11_TEXTURE2D_DESC textureDesc;
3007 readTexture->GetDesc(&textureDesc);
3008
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003009 D3D11_MAPPED_SUBRESOURCE mapping;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003010 HRESULT hr = mDeviceContext->Map(readTexture, 0, D3D11_MAP_READ, 0, &mapping);
Geoff Lang9cd19152014-05-28 15:54:34 -04003011 UNUSED_ASSERTION_VARIABLE(hr);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003012 ASSERT(SUCCEEDED(hr));
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003013
3014 unsigned char *source;
3015 int inputPitch;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003016 if (params.pack.reverseRowOrder)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003017 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003018 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (params.area.height - 1);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003019 inputPitch = -static_cast<int>(mapping.RowPitch);
3020 }
3021 else
3022 {
3023 source = static_cast<unsigned char*>(mapping.pData);
3024 inputPitch = static_cast<int>(mapping.RowPitch);
3025 }
3026
Geoff Lange4a492b2014-06-19 14:14:41 -04003027 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format);
3028 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat);
3029 GLenum sourceType = gl::GetType(sourceInternalFormat);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003030
Geoff Lange4a492b2014-06-19 14:14:41 -04003031 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003032
Jamie Madill7538f7f2014-04-17 11:53:39 -04003033 if (sourceFormat == params.format && sourceType == params.type)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003034 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003035 unsigned char *dest = static_cast<unsigned char*>(pixelsOut) + params.offset;
3036 for (int y = 0; y < params.area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003037 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003038 memcpy(dest + y * params.outputPitch, source + y * inputPitch, params.area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003039 }
3040 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003041 else
3042 {
Geoff Lange4a492b2014-06-19 14:14:41 -04003043 GLenum destInternalFormat = gl::GetSizedInternalFormat(params.format, params.type);
3044 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003045
Jamie Madill7538f7f2014-04-17 11:53:39 -04003046 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, params.format, params.type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003047 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003048 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003049 // Fast copy is possible through some special function
Jamie Madill7538f7f2014-04-17 11:53:39 -04003050 for (int y = 0; y < params.area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003051 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003052 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003053 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003054 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003055 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3056
3057 fastCopyFunc(src, dest);
3058 }
3059 }
3060 }
3061 else
3062 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003063 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Geoff Lange4a492b2014-06-19 14:14:41 -04003064 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(params.format, params.type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003065
3066 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3067 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3068 sizeof(temp) >= sizeof(gl::ColorUI) &&
3069 sizeof(temp) >= sizeof(gl::ColorI));
3070
Jamie Madill7538f7f2014-04-17 11:53:39 -04003071 for (int y = 0; y < params.area.height; y++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003072 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003073 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003074 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003075 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003076 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3077
3078 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3079 // will not allow the copy otherwise.
3080 readFunc(src, temp);
3081 writeFunc(temp, dest);
3082 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003083 }
3084 }
3085 }
3086
Jamie Madill7538f7f2014-04-17 11:53:39 -04003087 mDeviceContext->Unmap(readTexture, 0);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003088}
3089
Geoff Lang758d5b22013-06-11 11:42:50 -04003090bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003091 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3092 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003093{
Geoff Lang975af372013-06-12 11:19:22 -04003094 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3095 // it should never be the case that both color and depth/stencil need to be blitted at
3096 // at the same time.
3097 ASSERT(colorBlit != (depthBlit || stencilBlit));
3098
Geoff Langc1f51be2013-06-11 11:49:14 -04003099 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003100
Geoff Lang4d782732013-07-22 10:44:18 -04003101 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3102 if (!drawRenderTarget)
3103 {
3104 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3105 return gl::error(GL_OUT_OF_MEMORY, false);
3106 }
3107
3108 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3109 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3110 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3111 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3112
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003113 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3114 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003115 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003116 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003117 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003118 }
3119
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003120 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003121 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003122 unsigned int readSubresource = 0;
3123 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003124 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003125 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3126 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003127
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003128 if (unresolvedTexture)
3129 {
3130 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3131 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003132
Geoff Langea228632013-07-30 15:17:12 -04003133 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003134
Geoff Lang5c9a29a2014-02-11 10:26:24 -05003135 HRESULT hresult = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3136 if (FAILED(hresult))
Geoff Langc1f51be2013-06-11 11:49:14 -04003137 {
Geoff Langea228632013-07-30 15:17:12 -04003138 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003139 return gl::error(GL_OUT_OF_MEMORY, false);
3140 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003141 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003142 }
3143 else
3144 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003145 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003146 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003147 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003148 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003149 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003150 }
3151
Geoff Lang4d782732013-07-22 10:44:18 -04003152 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003153 {
Geoff Lang4d782732013-07-22 10:44:18 -04003154 SafeRelease(readTexture);
3155 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003156 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003157 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003158 }
3159
Geoff Lang125deab2013-08-09 13:34:16 -04003160 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3161 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3162
3163 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3164
3165 bool wholeBufferCopy = !scissorNeeded &&
3166 readRect.x == 0 && readRect.width == readSize.width &&
3167 readRect.y == 0 && readRect.height == readSize.height &&
3168 drawRect.x == 0 && drawRect.width == drawSize.width &&
3169 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003170
Geoff Langc1f51be2013-06-11 11:49:14 -04003171 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003172
Geoff Lang1cd1b212014-02-11 09:42:27 -05003173 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003174
3175 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3176 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3177 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3178 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3179
Geoff Lange4a492b2014-06-19 14:14:41 -04003180 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat()) > 0;
3181 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat()) > 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003182 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3183
Geoff Langc1f51be2013-06-11 11:49:14 -04003184 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003185 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3186 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003187 {
Geoff Lang125deab2013-08-09 13:34:16 -04003188 UINT dstX = drawRect.x;
3189 UINT dstY = drawRect.y;
3190
Geoff Langc1f51be2013-06-11 11:49:14 -04003191 D3D11_BOX readBox;
3192 readBox.left = readRect.x;
3193 readBox.right = readRect.x + readRect.width;
3194 readBox.top = readRect.y;
3195 readBox.bottom = readRect.y + readRect.height;
3196 readBox.front = 0;
3197 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003198
Geoff Lang125deab2013-08-09 13:34:16 -04003199 if (scissorNeeded)
3200 {
3201 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3202 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3203
3204 if (drawRect.x < scissor->x)
3205 {
3206 dstX = scissor->x;
3207 readBox.left += (scissor->x - drawRect.x);
3208 }
3209 if (drawRect.y < scissor->y)
3210 {
3211 dstY = scissor->y;
3212 readBox.top += (scissor->y - drawRect.y);
3213 }
3214 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3215 {
3216 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3217 }
3218 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3219 {
3220 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3221 }
3222 }
3223
Geoff Langc1f51be2013-06-11 11:49:14 -04003224 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3225 // We also require complete framebuffer copies for depth-stencil blit.
3226 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003227
Geoff Lang125deab2013-08-09 13:34:16 -04003228 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003229 readTexture, readSubresource, pSrcBox);
3230 result = true;
3231 }
3232 else
3233 {
3234 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003235 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003236
Geoff Lang975af372013-06-12 11:19:22 -04003237 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003238 {
Geoff Lang975af372013-06-12 11:19:22 -04003239 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003240 drawTexture, drawSubresource, drawArea, drawSize,
3241 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003242 }
3243 else if (depthBlit)
3244 {
Geoff Lang125deab2013-08-09 13:34:16 -04003245 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3246 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003247 }
3248 else if (stencilBlit)
3249 {
3250 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003251 drawTexture, drawSubresource, drawArea, drawSize,
3252 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003253 }
3254 else
3255 {
Geoff Lange4a492b2014-06-19 14:14:41 -04003256 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat());
Geoff Lang125deab2013-08-09 13:34:16 -04003257 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3258 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003259 }
3260 }
3261
3262 SafeRelease(readTexture);
3263 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003264
3265 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003266}
3267
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003268ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3269{
3270 D3D11_TEXTURE2D_DESC textureDesc;
3271 source->GetDesc(&textureDesc);
3272
3273 if (textureDesc.SampleDesc.Count > 1)
3274 {
3275 D3D11_TEXTURE2D_DESC resolveDesc;
3276 resolveDesc.Width = textureDesc.Width;
3277 resolveDesc.Height = textureDesc.Height;
3278 resolveDesc.MipLevels = 1;
3279 resolveDesc.ArraySize = 1;
3280 resolveDesc.Format = textureDesc.Format;
3281 resolveDesc.SampleDesc.Count = 1;
3282 resolveDesc.SampleDesc.Quality = 0;
3283 resolveDesc.Usage = textureDesc.Usage;
3284 resolveDesc.BindFlags = textureDesc.BindFlags;
3285 resolveDesc.CPUAccessFlags = 0;
3286 resolveDesc.MiscFlags = 0;
3287
3288 ID3D11Texture2D *resolveTexture = NULL;
3289 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3290 if (FAILED(result))
3291 {
3292 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3293 return NULL;
3294 }
3295
3296 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3297 return resolveTexture;
3298 }
3299 else
3300 {
3301 source->AddRef();
3302 return source;
3303 }
3304}
3305
Jamie Madill3c7fa222014-06-05 13:08:51 -04003306void Renderer11::invalidateFBOAttachmentSwizzles(gl::FramebufferAttachment *attachment, int mipLevel)
Geoff Lang42477a42013-09-17 17:07:02 -04003307{
Jamie Madill3c7fa222014-06-05 13:08:51 -04003308 ASSERT(attachment->isTexture());
3309 TextureStorage *texStorage = attachment->getTextureStorage();
Geoff Lang42477a42013-09-17 17:07:02 -04003310 if (texStorage)
3311 {
3312 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3313 if (!texStorage11)
3314 {
3315 ERR("texture storage pointer unexpectedly null.");
3316 return;
3317 }
3318
3319 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3320 }
3321}
3322
3323void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3324{
3325 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3326 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003327 gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(colorAttachment);
3328 if (attachment && attachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003329 {
Jamie Madille92a3542014-07-03 10:38:58 -04003330 invalidateFBOAttachmentSwizzles(attachment, attachment->mipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003331 }
3332 }
3333
Jamie Madill3c7fa222014-06-05 13:08:51 -04003334 gl::FramebufferAttachment *depthAttachment = framebuffer->getDepthbuffer();
3335 if (depthAttachment && depthAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003336 {
Jamie Madille92a3542014-07-03 10:38:58 -04003337 invalidateFBOAttachmentSwizzles(depthAttachment, depthAttachment->mipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003338 }
3339
Jamie Madill3c7fa222014-06-05 13:08:51 -04003340 gl::FramebufferAttachment *stencilAttachment = framebuffer->getStencilbuffer();
3341 if (stencilAttachment && stencilAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003342 {
Jamie Madille92a3542014-07-03 10:38:58 -04003343 invalidateFBOAttachmentSwizzles(stencilAttachment, stencilAttachment->mipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003344 }
3345}
3346
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003347bool Renderer11::getLUID(LUID *adapterLuid) const
3348{
3349 adapterLuid->HighPart = 0;
3350 adapterLuid->LowPart = 0;
3351
3352 if (!mDxgiAdapter)
3353 {
3354 return false;
3355 }
3356
3357 DXGI_ADAPTER_DESC adapterDesc;
3358 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3359 {
3360 return false;
3361 }
3362
3363 *adapterLuid = adapterDesc.AdapterLuid;
3364 return true;
3365}
3366
Geoff Lang005df412013-10-16 14:12:50 -04003367GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003368{
Geoff Lange4a492b2014-06-19 14:14:41 -04003369 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat));
Jamie Madillc8c102b2013-10-10 15:10:24 -04003370}
3371
Jamie Madill95ffb862014-01-29 09:26:59 -05003372rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3373{
3374 return gl_d3d11::GetVertexConversionType(vertexFormat);
3375}
3376
3377GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3378{
3379 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3380}
3381
Geoff Lang61e49a52013-05-29 10:22:58 -04003382Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3383{
3384 MultisampleSupportInfo supportInfo = { 0 };
3385
3386 UINT formatSupport;
3387 HRESULT result;
3388
3389 result = mDevice->CheckFormatSupport(format, &formatSupport);
3390 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3391 {
3392 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3393 {
3394 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3395 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3396 {
3397 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3398 }
3399 else
3400 {
3401 supportInfo.qualityLevels[i - 1] = 0;
3402 }
3403 }
3404 }
3405
3406 return supportInfo;
3407}
3408
Geoff Langcec35902014-04-16 10:52:36 -04003409gl::Caps Renderer11::generateCaps() const
3410{
3411 return d3d11_gl::GenerateCaps(mDevice);
3412}
3413
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003414}