blob: b9e7404c04049241e71bea585c534889021e3b4a [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);
819
820 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000821 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000822 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
823 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
824
Jamie Madill3c7fa222014-06-05 13:08:51 -0400825 gl::FramebufferAttachment *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000826
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000827 if (!colorbuffer)
828 {
829 ERR("render target pointer unexpectedly null.");
830 return false;
831 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000832
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000833 // check for zero-sized default framebuffer, which is a special case.
834 // in this case we do not wish to modify any state and just silently return false.
835 // this will not report any gl error but will cause the calling method to return.
836 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
837 {
838 return false;
839 }
840
841 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
842
843 // Extract the render target dimensions and view
844 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
845 if (!renderTarget)
846 {
847 ERR("render target pointer unexpectedly null.");
848 return false;
849 }
850
851 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
852 if (!framebufferRTVs[colorAttachment])
853 {
854 ERR("render target view pointer unexpectedly null.");
855 return false;
856 }
857
858 if (missingColorRenderTarget)
859 {
860 renderTargetWidth = colorbuffer->getWidth();
861 renderTargetHeight = colorbuffer->getHeight();
862 renderTargetFormat = colorbuffer->getActualFormat();
863 missingColorRenderTarget = false;
864 }
Jamie Madillba597af2013-10-22 13:12:15 -0400865
Geoff Lang91382e52014-01-07 16:16:30 -0500866 // TODO: Detect if this color buffer is already bound as a texture and unbind it first to prevent
867 // D3D11 warnings.
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000868 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000869 }
870
871 // Get the depth stencil render buffer and serials
Jamie Madill3c7fa222014-06-05 13:08:51 -0400872 gl::FramebufferAttachment *depthStencil = NULL;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000873 unsigned int depthbufferSerial = 0;
874 unsigned int stencilbufferSerial = 0;
875 if (framebuffer->getDepthbufferType() != GL_NONE)
876 {
877 depthStencil = framebuffer->getDepthbuffer();
878 if (!depthStencil)
879 {
880 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000881 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000882 return false;
883 }
884
885 depthbufferSerial = depthStencil->getSerial();
886 }
887 else if (framebuffer->getStencilbufferType() != GL_NONE)
888 {
889 depthStencil = framebuffer->getStencilbuffer();
890 if (!depthStencil)
891 {
892 ERR("Depth stencil 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 stencilbufferSerial = depthStencil->getSerial();
898 }
899
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000900 ID3D11DepthStencilView* framebufferDSV = NULL;
901 if (depthStencil)
902 {
903 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
904 if (!depthStencilRenderTarget)
905 {
906 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000907 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000908 return false;
909 }
910
911 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
912 if (!framebufferDSV)
913 {
914 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000915 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000916 return false;
917 }
918
919 // If there is no render buffer, the width, height and format values come from
920 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000921 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000922 {
923 renderTargetWidth = depthStencil->getWidth();
924 renderTargetHeight = depthStencil->getHeight();
925 renderTargetFormat = depthStencil->getActualFormat();
926 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000927 }
928
929 // Apply the render target and depth stencil
930 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000931 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000932 depthbufferSerial != mAppliedDepthbufferSerial ||
933 stencilbufferSerial != mAppliedStencilbufferSerial)
934 {
Geoff Langaae65a42014-05-26 12:43:44 -0400935 mDeviceContext->OMSetRenderTargets(getCaps().maxDrawBuffers, framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000936
937 mRenderTargetDesc.width = renderTargetWidth;
938 mRenderTargetDesc.height = renderTargetHeight;
939 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000940 mForceSetViewport = true;
941 mForceSetScissor = true;
Geoff Langc142e9d2013-09-30 15:19:47 -0400942 mForceSetBlendState = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000943
Nicolas Capensaea8e942014-05-09 19:14:08 -0400944 if (!mDepthStencilInitialized)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000945 {
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000946 mForceSetRasterState = true;
947 }
948
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000949 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
950 {
951 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
952 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000953 mAppliedDepthbufferSerial = depthbufferSerial;
954 mAppliedStencilbufferSerial = stencilbufferSerial;
955 mRenderTargetDescInitialized = true;
956 mDepthStencilInitialized = true;
957 }
958
Geoff Lang42477a42013-09-17 17:07:02 -0400959 invalidateFramebufferSwizzles(framebuffer);
960
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000961 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000962}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000963
Jamie Madill57a89722013-07-02 11:57:03 -0400964GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -0400965 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000966{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000967 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -0400968 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000969 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000970 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000971 return err;
972 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000973
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000974 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000975}
976
daniel@transgaming.com31240482012-11-28 21:06:41 +0000977GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000978{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000979 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000980
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000981 if (err == GL_NO_ERROR)
982 {
Geoff Lang7840b172014-03-13 11:20:44 -0400983 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
984
985 ID3D11Buffer *buffer = NULL;
986 DXGI_FORMAT bufferFormat = indexBuffer->getIndexFormat();
987
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000988 if (indexInfo->storage)
989 {
Brandon Jonesd38f9262014-06-18 16:26:45 -0700990 Buffer11 *storage = Buffer11::makeBuffer11(indexInfo->storage);
Geoff Lang7840b172014-03-13 11:20:44 -0400991 buffer = storage->getBuffer(BUFFER_USAGE_INDEX);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000992 }
Geoff Lang7840b172014-03-13 11:20:44 -0400993 else
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000994 {
Geoff Lang7840b172014-03-13 11:20:44 -0400995 buffer = indexBuffer->getBuffer();
996 }
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000997
Geoff Lang7840b172014-03-13 11:20:44 -0400998 if (buffer != mAppliedIB || bufferFormat != mAppliedIBFormat || indexInfo->startOffset != mAppliedIBOffset)
999 {
1000 mDeviceContext->IASetIndexBuffer(buffer, bufferFormat, indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001001
Geoff Lang7840b172014-03-13 11:20:44 -04001002 mAppliedIB = buffer;
1003 mAppliedIBFormat = bufferFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001004 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001005 }
1006 }
1007
1008 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001009}
1010
Geoff Langeeba6e12014-02-03 13:12:30 -05001011void Renderer11::applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[])
1012{
1013 ID3D11Buffer* d3dBuffers[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1014 UINT d3dOffsets[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1015 bool requiresUpdate = false;
1016 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1017 {
1018 if (transformFeedbackBuffers[i])
1019 {
Brandon Jonesd38f9262014-06-18 16:26:45 -07001020 Buffer11 *storage = Buffer11::makeBuffer11(transformFeedbackBuffers[i]->getImplementation());
Geoff Langeeba6e12014-02-03 13:12:30 -05001021 ID3D11Buffer *buffer = storage->getBuffer(BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK);
1022
1023 d3dBuffers[i] = buffer;
1024 d3dOffsets[i] = (mAppliedTFBuffers[i] != buffer) ? static_cast<UINT>(offsets[i]) : -1;
1025 }
1026 else
1027 {
1028 d3dBuffers[i] = NULL;
1029 d3dOffsets[i] = 0;
1030 }
1031
1032 if (d3dBuffers[i] != mAppliedTFBuffers[i] || offsets[i] != mAppliedTFOffsets[i])
1033 {
1034 requiresUpdate = true;
1035 }
1036 }
1037
1038 if (requiresUpdate)
1039 {
1040 mDeviceContext->SOSetTargets(ArraySize(d3dBuffers), d3dBuffers, d3dOffsets);
1041 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1042 {
1043 mAppliedTFBuffers[i] = d3dBuffers[i];
1044 mAppliedTFOffsets[i] = offsets[i];
1045 }
1046 }
1047}
1048
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001049void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001050{
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001051 if (mode == GL_POINTS && transformFeedbackActive)
1052 {
1053 // Since point sprites are generated with a geometry shader, too many vertices will
1054 // be written if transform feedback is active. To work around this, draw only the points
1055 // with the stream out shader and no pixel shader to feed the stream out buffers and then
1056 // draw again with the point sprite geometry shader to rasterize the point sprites.
1057
1058 mDeviceContext->PSSetShader(NULL, NULL, 0);
1059
1060 if (instances > 0)
1061 {
1062 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1063 }
1064 else
1065 {
1066 mDeviceContext->Draw(count, 0);
1067 }
1068
1069 mDeviceContext->GSSetShader(mCurPointGeometryShader, NULL, 0);
1070 mDeviceContext->PSSetShader(mAppliedPixelShader, NULL, 0);
1071
1072 if (instances > 0)
1073 {
1074 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1075 }
1076 else
1077 {
1078 mDeviceContext->Draw(count, 0);
1079 }
1080
1081 mDeviceContext->GSSetShader(mAppliedGeometryShader, NULL, 0);
1082 }
1083 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001084 {
1085 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1086 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001087 else if (mode == GL_TRIANGLE_FAN)
1088 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001089 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001090 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001091 else if (instances > 0)
1092 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001093 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001094 }
1095 else
1096 {
1097 mDeviceContext->Draw(count, 0);
1098 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001099}
1100
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001101void Renderer11::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices,
1102 gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001103{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001104 if (mode == GL_LINE_LOOP)
1105 {
1106 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1107 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001108 else if (mode == GL_TRIANGLE_FAN)
1109 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001110 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1111 }
1112 else if (instances > 0)
1113 {
1114 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001115 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001116 else
1117 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001118 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001119 }
1120}
1121
1122void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1123{
1124 // Get the raw indices for an indexed draw
1125 if (type != GL_NONE && elementArrayBuffer)
1126 {
1127 gl::Buffer *indexBuffer = elementArrayBuffer;
Brandon Jonesd38f9262014-06-18 16:26:45 -07001128 BufferImpl *storage = indexBuffer->getImplementation();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001129 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001130 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001131 }
1132
1133 if (!mLineLoopIB)
1134 {
1135 mLineLoopIB = new StreamingIndexBufferInterface(this);
1136 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1137 {
1138 delete mLineLoopIB;
1139 mLineLoopIB = NULL;
1140
1141 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001142 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001143 }
1144 }
1145
Geoff Lang57e713e2013-07-31 17:01:58 -04001146 // Checked by Renderer11::applyPrimitiveType
1147 ASSERT(count >= 0);
1148
1149 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001150 {
1151 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1152 return gl::error(GL_OUT_OF_MEMORY);
1153 }
1154
Geoff Lang57e713e2013-07-31 17:01:58 -04001155 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001156 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1157 {
1158 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001159 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001160 }
1161
1162 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001163 unsigned int offset;
1164 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001165 {
1166 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001167 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001168 }
1169
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001170 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001171 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001172
1173 switch (type)
1174 {
1175 case GL_NONE: // Non-indexed draw
1176 for (int i = 0; i < count; i++)
1177 {
1178 data[i] = i;
1179 }
1180 data[count] = 0;
1181 break;
1182 case GL_UNSIGNED_BYTE:
1183 for (int i = 0; i < count; i++)
1184 {
1185 data[i] = static_cast<const GLubyte*>(indices)[i];
1186 }
1187 data[count] = static_cast<const GLubyte*>(indices)[0];
1188 break;
1189 case GL_UNSIGNED_SHORT:
1190 for (int i = 0; i < count; i++)
1191 {
1192 data[i] = static_cast<const GLushort*>(indices)[i];
1193 }
1194 data[count] = static_cast<const GLushort*>(indices)[0];
1195 break;
1196 case GL_UNSIGNED_INT:
1197 for (int i = 0; i < count; i++)
1198 {
1199 data[i] = static_cast<const GLuint*>(indices)[i];
1200 }
1201 data[count] = static_cast<const GLuint*>(indices)[0];
1202 break;
1203 default: UNREACHABLE();
1204 }
1205
1206 if (!mLineLoopIB->unmapBuffer())
1207 {
1208 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001209 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001210 }
1211
Geoff Lang7840b172014-03-13 11:20:44 -04001212 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1213 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1214 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001215
Geoff Lang7840b172014-03-13 11:20:44 -04001216 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1217 {
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001218 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001219 mAppliedIB = d3dIndexBuffer;
1220 mAppliedIBFormat = indexFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001221 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001222 }
1223
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001224 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001225}
1226
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001227void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001228{
1229 // Get the raw indices for an indexed draw
1230 if (type != GL_NONE && elementArrayBuffer)
1231 {
1232 gl::Buffer *indexBuffer = elementArrayBuffer;
Brandon Jonesd38f9262014-06-18 16:26:45 -07001233 BufferImpl *storage = indexBuffer->getImplementation();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001234 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001235 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001236 }
1237
1238 if (!mTriangleFanIB)
1239 {
1240 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1241 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1242 {
1243 delete mTriangleFanIB;
1244 mTriangleFanIB = NULL;
1245
1246 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001247 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001248 }
1249 }
1250
Geoff Lang57e713e2013-07-31 17:01:58 -04001251 // Checked by Renderer11::applyPrimitiveType
1252 ASSERT(count >= 3);
1253
Geoff Langeadfd572013-07-09 15:55:07 -04001254 const unsigned int numTris = count - 2;
1255
Geoff Lang57e713e2013-07-31 17:01:58 -04001256 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001257 {
1258 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1259 return gl::error(GL_OUT_OF_MEMORY);
1260 }
1261
1262 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001263 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1264 {
1265 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001266 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001267 }
1268
1269 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001270 unsigned int offset;
1271 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001272 {
1273 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001274 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001275 }
1276
1277 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001278 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001279
1280 switch (type)
1281 {
1282 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001283 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001284 {
1285 data[i*3 + 0] = 0;
1286 data[i*3 + 1] = i + 1;
1287 data[i*3 + 2] = i + 2;
1288 }
1289 break;
1290 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001291 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001292 {
1293 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1294 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1295 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1296 }
1297 break;
1298 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001299 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001300 {
1301 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1302 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1303 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1304 }
1305 break;
1306 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001307 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001308 {
1309 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1310 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1311 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1312 }
1313 break;
1314 default: UNREACHABLE();
1315 }
1316
1317 if (!mTriangleFanIB->unmapBuffer())
1318 {
1319 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001320 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001321 }
1322
Geoff Lang7840b172014-03-13 11:20:44 -04001323 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1324 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1325 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001326
Geoff Lang7840b172014-03-13 11:20:44 -04001327 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1328 {
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001329 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001330 mAppliedIB = d3dIndexBuffer;
1331 mAppliedIBFormat = indexFormat;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001332 mAppliedIBOffset = indexBufferOffset;
1333 }
1334
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001335 if (instances > 0)
1336 {
1337 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1338 }
1339 else
1340 {
1341 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1342 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001343}
1344
Geoff Lang04fb89a2014-06-09 15:05:36 -04001345void Renderer11::applyShaders(gl::ProgramBinary *programBinary, const gl::VertexFormat inputLayout[], const gl::Framebuffer *framebuffer,
1346 bool rasterizerDiscard, bool transformFeedbackActive)
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001347{
Jamie Madillc5a83002014-02-14 16:41:25 -05001348 ShaderExecutable *vertexExe = programBinary->getVertexExecutableForInputLayout(inputLayout);
Geoff Lang04fb89a2014-06-09 15:05:36 -04001349 ShaderExecutable *pixelExe = programBinary->getPixelExecutableForFramebuffer(framebuffer);
Jamie Madill6246dc82014-01-29 09:26:47 -05001350 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001351
Jamie Madill6246dc82014-01-29 09:26:47 -05001352 ID3D11VertexShader *vertexShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader() : NULL);
Jamie Madill6246dc82014-01-29 09:26:47 -05001353
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001354 ID3D11PixelShader *pixelShader = NULL;
1355 // Skip pixel shader if we're doing rasterizer discard.
1356 if (!rasterizerDiscard)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001357 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001358 pixelShader = (pixelExe ? ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader() : NULL);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001359 }
1360
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001361 ID3D11GeometryShader *geometryShader = NULL;
1362 if (transformFeedbackActive)
Geoff Lang0550d032014-01-30 11:29:07 -05001363 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001364 geometryShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getStreamOutShader() : NULL);
1365 }
1366 else if (mCurRasterState.pointDrawMode)
1367 {
1368 geometryShader = (geometryExe ? ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader() : NULL);
Geoff Lang0550d032014-01-30 11:29:07 -05001369 }
1370
Jamie Madill6246dc82014-01-29 09:26:47 -05001371 bool dirtyUniforms = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001372
Jamie Madill6246dc82014-01-29 09:26:47 -05001373 if (vertexShader != mAppliedVertexShader)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001374 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001375 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1376 mAppliedVertexShader = vertexShader;
1377 dirtyUniforms = true;
1378 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001379
Jamie Madill6246dc82014-01-29 09:26:47 -05001380 if (geometryShader != mAppliedGeometryShader)
1381 {
1382 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1383 mAppliedGeometryShader = geometryShader;
1384 dirtyUniforms = true;
1385 }
1386
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001387 if (geometryExe && mCurRasterState.pointDrawMode)
1388 {
1389 mCurPointGeometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
1390 }
1391 else
1392 {
1393 mCurPointGeometryShader = NULL;
1394 }
1395
Jamie Madill6246dc82014-01-29 09:26:47 -05001396 if (pixelShader != mAppliedPixelShader)
1397 {
1398 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1399 mAppliedPixelShader = pixelShader;
1400 dirtyUniforms = true;
1401 }
1402
1403 if (dirtyUniforms)
1404 {
1405 programBinary->dirtyAllUniforms();
daniel@transgaming.come4991412012-12-20 20:55:34 +00001406 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001407}
1408
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001409void Renderer11::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001410{
Jamie Madill834e8b72014-04-11 13:33:58 -04001411 const std::vector<gl::LinkedUniform*> &uniformArray = programBinary.getUniforms();
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001412
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001413 unsigned int totalRegisterCountVS = 0;
1414 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001415
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001416 bool vertexUniformsDirty = false;
1417 bool pixelUniformsDirty = false;
1418
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001419 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001420 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001421 const gl::LinkedUniform &uniform = *uniformArray[uniformIndex];
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001422
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001423 if (uniform.isReferencedByVertexShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001424 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001425 totalRegisterCountVS += uniform.registerCount;
1426 vertexUniformsDirty = (vertexUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001427 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001428
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001429 if (uniform.isReferencedByFragmentShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001430 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001431 totalRegisterCountPS += uniform.registerCount;
1432 pixelUniformsDirty = (pixelUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001433 }
1434 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001435
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001436 const UniformStorage11 *vertexUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getVertexUniformStorage());
1437 const UniformStorage11 *fragmentUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getFragmentUniformStorage());
1438 ASSERT(vertexUniformStorage);
1439 ASSERT(fragmentUniformStorage);
1440
1441 ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
1442 ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001443
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001444 float (*mapVS)[4] = NULL;
1445 float (*mapPS)[4] = NULL;
1446
Shannon Woods5ab33c82013-06-26 15:31:09 -04001447 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1448 {
1449 D3D11_MAPPED_SUBRESOURCE map = {0};
1450 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001451 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001452 ASSERT(SUCCEEDED(result));
1453 mapVS = (float(*)[4])map.pData;
1454 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001455
Shannon Woods5ab33c82013-06-26 15:31:09 -04001456 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1457 {
1458 D3D11_MAPPED_SUBRESOURCE map = {0};
1459 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001460 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001461 ASSERT(SUCCEEDED(result));
1462 mapPS = (float(*)[4])map.pData;
1463 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001464
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001465 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001466 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001467 gl::LinkedUniform *uniform = uniformArray[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001468
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001469 if (!uniform->isSampler())
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001470 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001471 unsigned int componentCount = (4 - uniform->registerElement);
1472
Jamie Madill71cc91f2013-09-18 12:51:22 -04001473 // 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 -04001474 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001475
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001476 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001477 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001478 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001479 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001480
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001481 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001482 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001483 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001484 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001485 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001486 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001487
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001488 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001489 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001490 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001491 }
1492
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001493 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001494 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001495 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001496 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001497
1498 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1499 {
1500 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1501 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1502 }
1503
1504 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1505 {
1506 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1507 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1508 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001509
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001510 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001511 if (!mDriverConstantBufferVS)
1512 {
1513 D3D11_BUFFER_DESC constantBufferDescription = {0};
1514 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1515 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1516 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1517 constantBufferDescription.CPUAccessFlags = 0;
1518 constantBufferDescription.MiscFlags = 0;
1519 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001520
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001521 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001522 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001523 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001524
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001525 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1526 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001527
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001528 if (!mDriverConstantBufferPS)
1529 {
1530 D3D11_BUFFER_DESC constantBufferDescription = {0};
1531 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1532 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1533 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1534 constantBufferDescription.CPUAccessFlags = 0;
1535 constantBufferDescription.MiscFlags = 0;
1536 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001537
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001538 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001539 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001540 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001541
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001542 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1543 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001544
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001545 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1546 {
1547 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1548 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1549 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001550
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001551 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1552 {
1553 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1554 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1555 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001556
1557 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001558 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1559 {
1560 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1561 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1562 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001563}
1564
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001565void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001566{
Geoff Langda507fe2013-08-20 12:01:42 -04001567 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001568 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001569}
1570
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001571void Renderer11::markAllStateDirty()
1572{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001573 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1574 {
1575 mAppliedRenderTargetSerials[rtIndex] = 0;
1576 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001577 mAppliedDepthbufferSerial = 0;
1578 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001579 mDepthStencilInitialized = false;
1580 mRenderTargetDescInitialized = false;
1581
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001582 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001583 {
1584 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001585 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001586 }
1587 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1588 {
1589 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001590 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001591 }
1592
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001593 mForceSetBlendState = true;
1594 mForceSetRasterState = true;
1595 mForceSetDepthStencilState = true;
1596 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001597 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001598
Geoff Lang7840b172014-03-13 11:20:44 -04001599 mAppliedIB = NULL;
1600 mAppliedIBFormat = DXGI_FORMAT_UNKNOWN;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001601 mAppliedIBOffset = 0;
1602
Jamie Madill6246dc82014-01-29 09:26:47 -05001603 mAppliedVertexShader = NULL;
1604 mAppliedGeometryShader = NULL;
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001605 mCurPointGeometryShader = NULL;
Jamie Madill6246dc82014-01-29 09:26:47 -05001606 mAppliedPixelShader = NULL;
Geoff Langeeba6e12014-02-03 13:12:30 -05001607
1608 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1609 {
1610 mAppliedTFBuffers[i] = NULL;
1611 mAppliedTFOffsets[i] = 0;
1612 }
1613
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001614 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1615 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001616
1617 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001618
1619 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1620 {
1621 mCurrentConstantBufferVS[i] = -1;
1622 mCurrentConstantBufferPS[i] = -1;
1623 }
1624
1625 mCurrentVertexConstantBuffer = NULL;
1626 mCurrentPixelConstantBuffer = NULL;
1627 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001628
1629 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001630}
1631
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001632void Renderer11::releaseDeviceResources()
1633{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001634 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001635 mInputLayoutCache.clear();
1636
Geoff Langea228632013-07-30 15:17:12 -04001637 SafeDelete(mVertexDataManager);
1638 SafeDelete(mIndexDataManager);
1639 SafeDelete(mLineLoopIB);
1640 SafeDelete(mTriangleFanIB);
1641 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001642 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001643 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001644
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001645 SafeRelease(mDriverConstantBufferVS);
1646 SafeRelease(mDriverConstantBufferPS);
1647 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001648}
1649
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001650void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001651{
1652 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001653 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001654}
1655
1656bool Renderer11::isDeviceLost()
1657{
1658 return mDeviceLost;
1659}
1660
1661// set notify to true to broadcast a message to all contexts of the device loss
1662bool Renderer11::testDeviceLost(bool notify)
1663{
1664 bool isLost = false;
1665
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001666 // GetRemovedReason is used to test if the device is removed
1667 HRESULT result = mDevice->GetDeviceRemovedReason();
1668 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001669
1670 if (isLost)
1671 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001672 // Log error if this is a new device lost event
1673 if (mDeviceLost == false)
1674 {
1675 ERR("The D3D11 device was removed: 0x%08X", result);
1676 }
1677
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001678 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001679 // we'll probably get this done again by notifyDeviceLost
1680 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001681 // Note that we don't want to clear the device loss status here
1682 // -- this needs to be done by resetDevice
1683 mDeviceLost = true;
1684 if (notify)
1685 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001686 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001687 }
1688 }
1689
1690 return isLost;
1691}
1692
1693bool Renderer11::testDeviceResettable()
1694{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001695 // determine if the device is resettable by creating a dummy device
1696 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001697
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001698 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001699 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001700 return false;
1701 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001702
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001703 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001704 {
1705 D3D_FEATURE_LEVEL_11_0,
1706 D3D_FEATURE_LEVEL_10_1,
1707 D3D_FEATURE_LEVEL_10_0,
1708 };
1709
1710 ID3D11Device* dummyDevice;
1711 D3D_FEATURE_LEVEL dummyFeatureLevel;
1712 ID3D11DeviceContext* dummyContext;
1713
1714 HRESULT result = D3D11CreateDevice(NULL,
1715 D3D_DRIVER_TYPE_HARDWARE,
1716 NULL,
1717 #if defined(_DEBUG)
1718 D3D11_CREATE_DEVICE_DEBUG,
1719 #else
1720 0,
1721 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001722 featureLevels,
1723 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001724 D3D11_SDK_VERSION,
1725 &dummyDevice,
1726 &dummyFeatureLevel,
1727 &dummyContext);
1728
1729 if (!mDevice || FAILED(result))
1730 {
1731 return false;
1732 }
1733
Geoff Langea228632013-07-30 15:17:12 -04001734 SafeRelease(dummyContext);
1735 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001736
1737 return true;
1738}
1739
1740void Renderer11::release()
1741{
1742 releaseDeviceResources();
1743
Geoff Langea228632013-07-30 15:17:12 -04001744 SafeRelease(mDxgiFactory);
1745 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001746
1747 if (mDeviceContext)
1748 {
1749 mDeviceContext->ClearState();
1750 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001751 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001752 }
1753
Geoff Langea228632013-07-30 15:17:12 -04001754 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001755
1756 if (mD3d11Module)
1757 {
1758 FreeLibrary(mD3d11Module);
1759 mD3d11Module = NULL;
1760 }
1761
1762 if (mDxgiModule)
1763 {
1764 FreeLibrary(mDxgiModule);
1765 mDxgiModule = NULL;
1766 }
Geoff Langdad5ed32014-02-10 12:59:17 -05001767
1768 mCompiler.release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001769}
1770
1771bool Renderer11::resetDevice()
1772{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001773 // recreate everything
1774 release();
1775 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001776
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001777 if (result != EGL_SUCCESS)
1778 {
1779 ERR("Could not reinitialize D3D11 device: %08X", result);
1780 return false;
1781 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001782
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001783 mDeviceLost = false;
1784
1785 return true;
1786}
1787
1788DWORD Renderer11::getAdapterVendor() const
1789{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001790 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001791}
1792
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001793std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001794{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001795 std::ostringstream rendererString;
1796
1797 rendererString << mDescription;
1798 rendererString << " Direct3D11";
1799
1800 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1801 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1802
1803 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001804}
1805
1806GUID Renderer11::getAdapterIdentifier() const
1807{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001808 // Use the adapter LUID as our adapter ID
1809 // This number is local to a machine is only guaranteed to be unique between restarts
1810 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1811 GUID adapterId = {0};
1812 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1813 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001814}
1815
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001816unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001817{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001818 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1819 switch (mFeatureLevel)
1820 {
1821 case D3D_FEATURE_LEVEL_11_0:
1822 case D3D_FEATURE_LEVEL_10_1:
1823 case D3D_FEATURE_LEVEL_10_0:
1824 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1825 default: UNREACHABLE();
1826 return 0;
1827 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001828}
1829
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001830unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1831{
1832 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1833}
1834
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001835unsigned int Renderer11::getReservedVertexUniformVectors() const
1836{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001837 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001838}
1839
1840unsigned int Renderer11::getReservedFragmentUniformVectors() const
1841{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001842 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001843}
1844
1845unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001846{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001847 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1848 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1849 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001850}
1851
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001852unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001853{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001854 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1855 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1856 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001857}
1858
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001859unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001860{
1861 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001862 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1863 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001864 switch (mFeatureLevel)
1865 {
1866 case D3D_FEATURE_LEVEL_11_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001867 return D3D11_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001868 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001869 return D3D10_1_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001870 case D3D_FEATURE_LEVEL_10_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001871 return D3D10_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001872 default: UNREACHABLE();
1873 return 0;
1874 }
1875}
1876
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001877unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
1878{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001879 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1880 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1881
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001882 switch (mFeatureLevel)
1883 {
1884 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001885 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001886 case D3D_FEATURE_LEVEL_10_1:
1887 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001888 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001889 default: UNREACHABLE();
1890 return 0;
1891 }
1892}
1893
1894unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
1895{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001896 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1897 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1898
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001899 switch (mFeatureLevel)
1900 {
1901 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001902 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001903 case D3D_FEATURE_LEVEL_10_1:
1904 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001905 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001906 default: UNREACHABLE();
1907 return 0;
1908 }
1909}
1910
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001911unsigned int Renderer11::getReservedVertexUniformBuffers() const
1912{
1913 // we reserve one buffer for the application uniforms, and one for driver uniforms
1914 return 2;
1915}
1916
1917unsigned int Renderer11::getReservedFragmentUniformBuffers() const
1918{
1919 // we reserve one buffer for the application uniforms, and one for driver uniforms
1920 return 2;
1921}
1922
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001923unsigned int Renderer11::getReservedVaryings() const
1924{
Jamie Madill2bf8b372014-06-16 17:18:51 -04001925 // We potentially reserve varyings for gl_Position, dx_Position, gl_FragCoord and gl_PointSize
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001926 return 4;
1927}
1928
1929
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001930unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
1931{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001932 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
1933 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
1934
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001935 switch (mFeatureLevel)
1936 {
1937 case D3D_FEATURE_LEVEL_11_0:
1938 return D3D11_SO_BUFFER_SLOT_COUNT;
1939 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001940 return D3D10_1_SO_BUFFER_SLOT_COUNT;
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001941 case D3D_FEATURE_LEVEL_10_0:
1942 return D3D10_SO_BUFFER_SLOT_COUNT;
1943 default: UNREACHABLE();
1944 return 0;
1945 }
1946}
1947
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001948unsigned int Renderer11::getMaxTransformFeedbackSeparateComponents() const
1949{
1950 switch (mFeatureLevel)
1951 {
1952 case D3D_FEATURE_LEVEL_11_0:
1953 return getMaxTransformFeedbackInterleavedComponents() / getMaxTransformFeedbackBuffers();
1954 case D3D_FEATURE_LEVEL_10_1:
1955 case D3D_FEATURE_LEVEL_10_0:
1956 // D3D 10 and 10.1 only allow one output per output slot if an output slot other than zero
1957 // is used.
1958 return 4;
1959 default: UNREACHABLE();
1960 return 0;
1961 }
1962}
1963
1964unsigned int Renderer11::getMaxTransformFeedbackInterleavedComponents() const
1965{
1966 return (getMaxVaryingVectors() * 4);
1967}
1968
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00001969unsigned int Renderer11::getMaxUniformBufferSize() const
1970{
1971 // Each component is a 4-element vector of 4-byte units (floats)
1972 const unsigned int bytesPerComponent = 4 * sizeof(float);
1973
1974 switch (mFeatureLevel)
1975 {
1976 case D3D_FEATURE_LEVEL_11_0:
1977 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1978 case D3D_FEATURE_LEVEL_10_1:
1979 case D3D_FEATURE_LEVEL_10_0:
1980 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1981 default: UNREACHABLE();
1982 return 0;
1983 }
1984}
1985
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001986bool Renderer11::getShareHandleSupport() const
1987{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00001988 // We only currently support share handles with BGRA surfaces, because
1989 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001990 // PIX doesn't seem to support using share handles, so disable them.
Geoff Langcec35902014-04-16 10:52:36 -04001991 return getCaps().extensions.textureFormatBGRA8888 && !gl::perfActive();
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00001992}
1993
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00001994bool Renderer11::getPostSubBufferSupport() const
1995{
1996 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
1997 return false;
1998}
1999
Jamie Madill13a2f852013-12-11 16:35:08 -05002000int Renderer11::getMaxRecommendedElementsIndices() const
2001{
2002 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2003 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2004
2005 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2006 return std::numeric_limits<GLint>::max();
2007}
2008
2009int Renderer11::getMaxRecommendedElementsVertices() const
2010{
2011 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2012 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2013
2014 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2015 return std::numeric_limits<GLint>::max();
2016}
2017
Geoff Lang05b05022014-06-11 15:31:45 -04002018bool Renderer11::getSRGBTextureSupport() const
2019{
2020 return true;
2021}
2022
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002023int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002024{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002025 switch (mFeatureLevel)
2026 {
2027 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002028 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002029 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2030 default: UNREACHABLE(); return 0;
2031 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002032}
2033
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002034int Renderer11::getMinorShaderModel() const
2035{
2036 switch (mFeatureLevel)
2037 {
2038 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2039 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2040 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2041 default: UNREACHABLE(); return 0;
2042 }
2043}
2044
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002045int Renderer11::getMinSwapInterval() const
2046{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002047 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002048}
2049
2050int Renderer11::getMaxSwapInterval() const
2051{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002052 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002053}
2054
2055int Renderer11::getMaxSupportedSamples() const
2056{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002057 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002058}
2059
Geoff Lang005df412013-10-16 14:12:50 -04002060GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002061{
Geoff Lange4a492b2014-06-19 14:14:41 -04002062 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat);
Geoff Lang0e120e32013-05-29 10:23:55 -04002063 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2064 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2065}
2066
Geoff Lang005df412013-10-16 14:12:50 -04002067GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002068{
2069 unsigned int numCounts = 0;
2070
2071 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Lange4a492b2014-06-19 14:14:41 -04002072 GLenum componentType = gl::GetComponentType(internalFormat);
Geoff Langb2f3d052013-08-13 12:49:27 -04002073 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002074 {
Geoff Lange4a492b2014-06-19 14:14:41 -04002075 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat);
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002076 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2077
2078 if (iter != mMultisampleSupportMap.end())
2079 {
2080 const MultisampleSupportInfo& info = iter->second;
2081 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2082 {
2083 if (info.qualityLevels[i] > 0)
2084 {
2085 numCounts++;
2086 }
2087 }
2088 }
2089 }
2090
2091 return numCounts;
2092}
2093
Geoff Lang005df412013-10-16 14:12:50 -04002094void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002095{
2096 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Lange4a492b2014-06-19 14:14:41 -04002097 GLenum componentType = gl::GetComponentType(internalFormat);
Geoff Langb2f3d052013-08-13 12:49:27 -04002098 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2099 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002100 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002101 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002102
Geoff Lange4a492b2014-06-19 14:14:41 -04002103 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat);
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002104 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2105
2106 if (iter != mMultisampleSupportMap.end())
2107 {
2108 const MultisampleSupportInfo& info = iter->second;
2109 int bufPos = 0;
2110 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2111 {
2112 if (info.qualityLevels[i] > 0)
2113 {
2114 params[bufPos++] = i + 1;
2115 }
2116 }
2117 }
2118}
2119
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002120int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2121{
2122 if (requested == 0)
2123 {
2124 return 0;
2125 }
2126
2127 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2128 if (iter != mMultisampleSupportMap.end())
2129 {
2130 const MultisampleSupportInfo& info = iter->second;
2131 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2132 {
2133 if (info.qualityLevels[i] > 0)
2134 {
2135 return i + 1;
2136 }
2137 }
2138 }
2139
2140 return -1;
2141}
2142
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002143bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002144{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002145 if (source && dest)
2146 {
2147 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2148 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2149
Nicolas Capens76b258f2014-04-03 10:59:42 -04002150 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002151
2152 dest11->invalidateSwizzleCache();
2153
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002154 return true;
2155 }
2156
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002157 return false;
2158}
2159
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002160bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002161{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002162 if (source && dest)
2163 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002164 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2165 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002166
Nicolas Capens76b258f2014-04-03 10:59:42 -04002167 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002168
2169 dest11->invalidateSwizzleCache();
2170
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002171 return true;
2172 }
2173
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002174 return false;
2175}
2176
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002177bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2178{
2179 if (source && dest)
2180 {
2181 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2182 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2183
Nicolas Capens76b258f2014-04-03 10:59:42 -04002184 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002185
2186 dest11->invalidateSwizzleCache();
2187
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002188 return true;
2189 }
2190
2191 return false;
2192}
2193
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002194bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2195{
2196 if (source && dest)
2197 {
2198 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2199 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2200
Nicolas Capens76b258f2014-04-03 10:59:42 -04002201 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002202
2203 dest11->invalidateSwizzleCache();
2204
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002205 return true;
2206 }
2207
2208 return false;
2209}
2210
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002211bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002212 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002213{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002214 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002215 if (!colorbuffer)
2216 {
2217 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002218 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002219 }
2220
2221 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2222 if (!sourceRenderTarget)
2223 {
2224 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002225 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002226 }
2227
2228 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2229 if (!source)
2230 {
2231 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002232 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002233 }
2234
2235 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2236 if (!storage11)
2237 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002238 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002239 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002240 }
2241
2242 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2243 if (!destRenderTarget)
2244 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002245 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002246 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002247 }
2248
2249 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2250 if (!dest)
2251 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002252 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002253 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002254 }
2255
Geoff Langb86b9792013-06-04 16:32:05 -04002256 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2257 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002258
Geoff Langb86b9792013-06-04 16:32:05 -04002259 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2260 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002261
Geoff Langb86b9792013-06-04 16:32:05 -04002262 // Use nearest filtering because source and destination are the same size for the direct
2263 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002264 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002265 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002266
Geoff Lang42477a42013-09-17 17:07:02 -04002267 storage11->invalidateSwizzleCacheLevel(level);
2268
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002269 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002270}
2271
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002272bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002273 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002274{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002275 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002276 if (!colorbuffer)
2277 {
2278 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002279 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002280 }
2281
2282 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2283 if (!sourceRenderTarget)
2284 {
2285 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002286 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002287 }
2288
2289 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2290 if (!source)
2291 {
2292 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002293 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002294 }
2295
2296 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2297 if (!storage11)
2298 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002299 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002300 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002301 }
2302
Nicolas Capensb13f8662013-06-04 13:30:19 -04002303 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002304 if (!destRenderTarget)
2305 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002306 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002307 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002308 }
2309
2310 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2311 if (!dest)
2312 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002313 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002314 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002315 }
2316
Geoff Langb86b9792013-06-04 16:32:05 -04002317 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2318 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002319
Geoff Langb86b9792013-06-04 16:32:05 -04002320 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2321 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002322
Geoff Langb86b9792013-06-04 16:32:05 -04002323 // Use nearest filtering because source and destination are the same size for the direct
2324 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002325 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002326 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002327
Geoff Lang42477a42013-09-17 17:07:02 -04002328 storage11->invalidateSwizzleCacheLevel(level);
2329
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002330 return ret;
2331}
2332
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002333bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2334 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2335{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002336 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002337 if (!colorbuffer)
2338 {
2339 ERR("Failed to retrieve the color buffer from the frame buffer.");
2340 return gl::error(GL_OUT_OF_MEMORY, false);
2341 }
2342
2343 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2344 if (!sourceRenderTarget)
2345 {
2346 ERR("Failed to retrieve the render target from the frame buffer.");
2347 return gl::error(GL_OUT_OF_MEMORY, false);
2348 }
2349
2350 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2351 if (!source)
2352 {
2353 ERR("Failed to retrieve the render target view from the render target.");
2354 return gl::error(GL_OUT_OF_MEMORY, false);
2355 }
2356
2357 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2358 if (!storage11)
2359 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002360 ERR("Failed to retrieve the texture storage from the destination.");
2361 return gl::error(GL_OUT_OF_MEMORY, false);
2362 }
2363
2364 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2365 if (!destRenderTarget)
2366 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002367 ERR("Failed to retrieve the render target from the destination storage.");
2368 return gl::error(GL_OUT_OF_MEMORY, false);
2369 }
2370
2371 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2372 if (!dest)
2373 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002374 ERR("Failed to retrieve the render target view from the destination render target.");
2375 return gl::error(GL_OUT_OF_MEMORY, false);
2376 }
2377
Geoff Langb86b9792013-06-04 16:32:05 -04002378 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2379 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002380
Geoff Langb86b9792013-06-04 16:32:05 -04002381 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2382 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002383
Geoff Langb86b9792013-06-04 16:32:05 -04002384 // Use nearest filtering because source and destination are the same size for the direct
2385 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002386 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002387 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002388
Geoff Lang42477a42013-09-17 17:07:02 -04002389 storage11->invalidateSwizzleCacheLevel(level);
2390
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002391 return ret;
2392}
2393
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002394bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2395 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2396{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002397 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002398 if (!colorbuffer)
2399 {
2400 ERR("Failed to retrieve the color buffer from the frame buffer.");
2401 return gl::error(GL_OUT_OF_MEMORY, false);
2402 }
2403
2404 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2405 if (!sourceRenderTarget)
2406 {
2407 ERR("Failed to retrieve the render target from the frame buffer.");
2408 return gl::error(GL_OUT_OF_MEMORY, false);
2409 }
2410
2411 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2412 if (!source)
2413 {
2414 ERR("Failed to retrieve the render target view from the render target.");
2415 return gl::error(GL_OUT_OF_MEMORY, false);
2416 }
2417
2418 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2419 if (!storage11)
2420 {
Geoff Langea228632013-07-30 15:17:12 -04002421 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002422 ERR("Failed to retrieve the texture storage from the destination.");
2423 return gl::error(GL_OUT_OF_MEMORY, false);
2424 }
2425
2426 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2427 if (!destRenderTarget)
2428 {
Geoff Langea228632013-07-30 15:17:12 -04002429 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002430 ERR("Failed to retrieve the render target from the destination storage.");
2431 return gl::error(GL_OUT_OF_MEMORY, false);
2432 }
2433
2434 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2435 if (!dest)
2436 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002437 ERR("Failed to retrieve the render target view from the destination render target.");
2438 return gl::error(GL_OUT_OF_MEMORY, false);
2439 }
2440
Geoff Langb86b9792013-06-04 16:32:05 -04002441 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2442 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002443
Geoff Langb86b9792013-06-04 16:32:05 -04002444 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2445 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002446
Geoff Langb86b9792013-06-04 16:32:05 -04002447 // Use nearest filtering because source and destination are the same size for the direct
2448 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002449 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002450 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002451
Geoff Lang42477a42013-09-17 17:07:02 -04002452 storage11->invalidateSwizzleCacheLevel(level);
2453
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002454 return ret;
2455}
2456
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002457void Renderer11::unapplyRenderTargets()
2458{
2459 setOneTimeRenderTarget(NULL);
2460}
2461
2462void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2463{
2464 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2465
2466 rtvArray[0] = renderTargetView;
2467
Geoff Langaae65a42014-05-26 12:43:44 -04002468 mDeviceContext->OMSetRenderTargets(getCaps().maxDrawBuffers, rtvArray, NULL);
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002469
2470 // Do not preserve the serial for this one-time-use render target
2471 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2472 {
2473 mAppliedRenderTargetSerials[rtIndex] = 0;
2474 }
2475}
2476
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002477RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2478{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002479 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002480 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002481
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002482 if (depth)
2483 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002484 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002485 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002486 swapChain11->getDepthStencilTexture(),
2487 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002488 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002489 }
2490 else
2491 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002492 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002493 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002494 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002495 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002496 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002497 }
2498 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002499}
2500
Geoff Langa2d97f12013-06-11 11:44:02 -04002501RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002502{
Geoff Langa2d97f12013-06-11 11:44:02 -04002503 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002504 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002505}
2506
Geoff Lang48dcae72014-02-05 16:28:24 -05002507ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type,
2508 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2509 bool separatedOutputBuffers)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002510{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002511 ShaderExecutable11 *executable = NULL;
Geoff Lang48dcae72014-02-05 16:28:24 -05002512 HRESULT result;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002513
2514 switch (type)
2515 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002516 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002517 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002518 ID3D11VertexShader *vertexShader = NULL;
2519 ID3D11GeometryShader *streamOutShader = NULL;
2520
2521 result = mDevice->CreateVertexShader(function, length, NULL, &vertexShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002522 ASSERT(SUCCEEDED(result));
2523
Geoff Lang48dcae72014-02-05 16:28:24 -05002524 if (transformFeedbackVaryings.size() > 0)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002525 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002526 std::vector<D3D11_SO_DECLARATION_ENTRY> soDeclaration;
2527 for (size_t i = 0; i < transformFeedbackVaryings.size(); i++)
2528 {
2529 const gl::LinkedVarying &varying = transformFeedbackVaryings[i];
Jamie Madill6195ef82014-06-18 10:09:43 -04002530 GLenum transposedType = gl::TransposeMatrixType(varying.type);
2531
Geoff Langcebb5aa2014-04-07 14:13:40 -04002532 for (size_t j = 0; j < varying.semanticIndexCount; j++)
Geoff Lang48dcae72014-02-05 16:28:24 -05002533 {
2534 D3D11_SO_DECLARATION_ENTRY entry = { 0 };
2535 entry.Stream = 0;
Geoff Langcebb5aa2014-04-07 14:13:40 -04002536 entry.SemanticName = varying.semanticName.c_str();
2537 entry.SemanticIndex = varying.semanticIndex + j;
Geoff Lang48dcae72014-02-05 16:28:24 -05002538 entry.StartComponent = 0;
Jamie Madill6195ef82014-06-18 10:09:43 -04002539 entry.ComponentCount = gl::VariableColumnCount(transposedType);
Geoff Lang48dcae72014-02-05 16:28:24 -05002540 entry.OutputSlot = (separatedOutputBuffers ? i : 0);
2541 soDeclaration.push_back(entry);
2542 }
2543 }
2544
2545 result = mDevice->CreateGeometryShaderWithStreamOutput(function, length, soDeclaration.data(), soDeclaration.size(),
2546 NULL, 0, 0, NULL, &streamOutShader);
2547 ASSERT(SUCCEEDED(result));
2548 }
2549
2550 if (vertexShader)
2551 {
2552 executable = new ShaderExecutable11(function, length, vertexShader, streamOutShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002553 }
2554 }
2555 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002556 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002557 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002558 ID3D11PixelShader *pixelShader = NULL;
2559
2560 result = mDevice->CreatePixelShader(function, length, NULL, &pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002561 ASSERT(SUCCEEDED(result));
2562
Geoff Lang48dcae72014-02-05 16:28:24 -05002563 if (pixelShader)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002564 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002565 executable = new ShaderExecutable11(function, length, pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002566 }
2567 }
2568 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002569 case rx::SHADER_GEOMETRY:
2570 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002571 ID3D11GeometryShader *geometryShader = NULL;
2572
2573 result = mDevice->CreateGeometryShader(function, length, NULL, &geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002574 ASSERT(SUCCEEDED(result));
2575
Geoff Lang48dcae72014-02-05 16:28:24 -05002576 if (geometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002577 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002578 executable = new ShaderExecutable11(function, length, geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002579 }
2580 }
2581 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002582 default:
2583 UNREACHABLE();
2584 break;
2585 }
2586
2587 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002588}
2589
Geoff Lang48dcae72014-02-05 16:28:24 -05002590ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
2591 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2592 bool separatedOutputBuffers, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002593{
Geoff Lang6e05c272014-03-17 10:46:54 -07002594 const char *profileType = NULL;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002595 switch (type)
2596 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002597 case rx::SHADER_VERTEX:
Geoff Lang6e05c272014-03-17 10:46:54 -07002598 profileType = "vs";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002599 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002600 case rx::SHADER_PIXEL:
Geoff Lang6e05c272014-03-17 10:46:54 -07002601 profileType = "ps";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002602 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002603 case rx::SHADER_GEOMETRY:
Geoff Lang6e05c272014-03-17 10:46:54 -07002604 profileType = "gs";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002605 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002606 default:
2607 UNREACHABLE();
2608 return NULL;
2609 }
2610
Geoff Lang6e05c272014-03-17 10:46:54 -07002611 const char *profileVersion = NULL;
2612 switch (mFeatureLevel)
2613 {
2614 case D3D_FEATURE_LEVEL_11_0:
2615 profileVersion = "5_0";
2616 break;
2617 case D3D_FEATURE_LEVEL_10_1:
2618 profileVersion = "4_1";
2619 break;
2620 case D3D_FEATURE_LEVEL_10_0:
2621 profileVersion = "4_0";
2622 break;
2623 default:
2624 UNREACHABLE();
2625 return NULL;
2626 }
2627
2628 char profile[32];
2629 snprintf(profile, ArraySize(profile), "%s_%s", profileType, profileVersion);
2630
Nicolas Capens93faad92014-05-10 12:14:13 -04002631 UINT flags = D3DCOMPILE_OPTIMIZATION_LEVEL0;
2632
2633 if (gl::perfActive())
2634 {
2635#ifndef NDEBUG
2636 flags = D3DCOMPILE_SKIP_OPTIMIZATION;
2637#endif
2638
2639 flags |= D3DCOMPILE_DEBUG;
2640
2641 std::string sourcePath = getTempPath();
2642 std::string sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(shaderHLSL);
2643 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
2644 }
2645
2646 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
2647 // Try the default flags first and if compilation fails, try some alternatives.
2648 const UINT extraFlags[] =
2649 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002650 flags,
Nicolas Capens2c27db62014-05-10 12:21:11 -04002651 flags | D3DCOMPILE_SKIP_VALIDATION,
2652 flags | D3DCOMPILE_SKIP_OPTIMIZATION
Nicolas Capens93faad92014-05-10 12:14:13 -04002653 };
2654
2655 const static char *extraFlagNames[] =
2656 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002657 "default",
Nicolas Capens2c27db62014-05-10 12:21:11 -04002658 "skip validation",
2659 "skip optimization"
Nicolas Capens93faad92014-05-10 12:14:13 -04002660 };
2661
2662 int attempts = ArraySize(extraFlags);
2663
2664 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, extraFlags, extraFlagNames, attempts);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002665 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002666 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002667 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002668 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002669
Geoff Lang48dcae72014-02-05 16:28:24 -05002670 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type,
2671 transformFeedbackVaryings, separatedOutputBuffers);
Geoff Langea228632013-07-30 15:17:12 -04002672 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002673
2674 return executable;
2675}
2676
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002677rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2678{
2679 return new UniformStorage11(this, storageSize);
2680}
2681
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002682VertexBuffer *Renderer11::createVertexBuffer()
2683{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002684 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002685}
2686
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002687IndexBuffer *Renderer11::createIndexBuffer()
2688{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002689 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002690}
2691
Brandon Jonesd38f9262014-06-18 16:26:45 -07002692BufferImpl *Renderer11::createBuffer()
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002693{
Brandon Jonesd38f9262014-06-18 16:26:45 -07002694 return new Buffer11(this);
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002695}
2696
Brandon Jones5bf98292014-06-06 17:19:38 -07002697VertexArrayImpl *Renderer11::createVertexArray()
2698{
2699 return new VertexArray11(this);
2700}
2701
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002702QueryImpl *Renderer11::createQuery(GLenum type)
2703{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002704 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002705}
2706
2707FenceImpl *Renderer11::createFence()
2708{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002709 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002710}
2711
Geoff Lang005df412013-10-16 14:12:50 -04002712bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002713{
Geoff Lang2b31af22014-05-23 14:45:40 -04002714 ASSERT(getCaps().extensions.pixelBufferObject);
Jamie Madill4461f092013-10-10 15:10:39 -04002715
Jamie Madill4461f092013-10-10 15:10:39 -04002716 // sRGB formats do not work with D3D11 buffer SRVs
Geoff Lange4a492b2014-06-19 14:14:41 -04002717 if (gl::GetColorEncoding(internalFormat) == GL_SRGB)
Jamie Madill4461f092013-10-10 15:10:39 -04002718 {
2719 return false;
2720 }
2721
2722 // We cannot support direct copies to non-color-renderable formats
Geoff Langcec35902014-04-16 10:52:36 -04002723 if (!getCaps().textureCaps.get(internalFormat).colorRendering)
Jamie Madill4461f092013-10-10 15:10:39 -04002724 {
2725 return false;
2726 }
2727
2728 // We skip all 3-channel formats since sometimes format support is missing
Geoff Lange4a492b2014-06-19 14:14:41 -04002729 if (gl::GetComponentCount(internalFormat) == 3)
Jamie Madill4461f092013-10-10 15:10:39 -04002730 {
2731 return false;
2732 }
2733
2734 // We don't support formats which we can't represent without conversion
2735 if (getNativeTextureFormat(internalFormat) != internalFormat)
2736 {
2737 return false;
2738 }
2739
2740 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002741}
2742
Jamie Madilla21eea32013-09-18 14:36:25 -04002743bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2744 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2745{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002746 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002747 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2748}
2749
Jamie Madill3c7fa222014-06-05 13:08:51 -04002750bool Renderer11::getRenderTargetResource(gl::FramebufferAttachment *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002751{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002752 ASSERT(colorbuffer != NULL);
2753
2754 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2755 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002756 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002757 *subresourceIndex = renderTarget->getSubresourceIndex();
2758
2759 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2760 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002761 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002762 ID3D11Resource *textureResource = NULL;
2763 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002764
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002765 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002766 {
Geoff Lang8e328842014-02-10 13:11:20 -05002767 HRESULT result = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002768 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002769
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002770 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002771 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002772 return true;
2773 }
2774 else
2775 {
2776 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2777 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002778 }
2779 }
2780 }
2781 }
2782
2783 return false;
2784}
2785
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002786bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002787 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002788{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002789 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002790 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002791 gl::FramebufferAttachment *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002792
2793 if (!readBuffer)
2794 {
2795 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2796 return gl::error(GL_OUT_OF_MEMORY, false);
2797 }
2798
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002799 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002800
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002801 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002802 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002803 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2804 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002805 gl::FramebufferAttachment *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002806
2807 if (!drawBuffer)
2808 {
2809 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2810 return gl::error(GL_OUT_OF_MEMORY, false);
2811 }
2812
2813 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2814
Geoff Lang125deab2013-08-09 13:34:16 -04002815 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002816 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002817 {
2818 return false;
2819 }
2820 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002821 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002822 }
2823
Geoff Lang685806d2013-06-12 11:16:36 -04002824 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002825 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002826 gl::FramebufferAttachment *readBuffer = readTarget->getDepthOrStencilbuffer();
2827 gl::FramebufferAttachment *drawBuffer = drawTarget->getDepthOrStencilbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002828
2829 if (!readBuffer)
2830 {
2831 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2832 return gl::error(GL_OUT_OF_MEMORY, false);
2833 }
2834
2835 if (!drawBuffer)
2836 {
2837 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2838 return gl::error(GL_OUT_OF_MEMORY, false);
2839 }
2840
2841 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2842 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2843
Geoff Lang125deab2013-08-09 13:34:16 -04002844 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002845 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002846 {
2847 return false;
2848 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002849 }
2850
Geoff Lang42477a42013-09-17 17:07:02 -04002851 invalidateFramebufferSwizzles(drawTarget);
2852
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002853 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002854}
2855
Jamie Madilleb9baab2014-03-24 13:19:43 -04002856void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
2857 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002858{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002859 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002860 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002861
Jamie Madill3c7fa222014-06-05 13:08:51 -04002862 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002863
2864 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002865 {
2866 gl::Rectangle area;
2867 area.x = x;
2868 area.y = y;
2869 area.width = width;
2870 area.height = height;
2871
Jamie Madill1ef6fe72014-05-01 14:51:05 -04002872 if (pack.pixelBuffer.get() != NULL)
2873 {
Brandon Jonesd38f9262014-06-18 16:26:45 -07002874 rx::Buffer11 *packBufferStorage = Buffer11::makeBuffer11(pack.pixelBuffer.get()->getImplementation());
Jamie Madill1ef6fe72014-05-01 14:51:05 -04002875 PackPixelsParams packParams(area, format, type, outputPitch, pack, reinterpret_cast<ptrdiff_t>(pixels));
2876 packBufferStorage->packPixels(colorBufferTexture, subresourceIndex, packParams);
2877 }
2878 else
2879 {
2880 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch, pack, pixels);
2881 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002882
Geoff Langea228632013-07-30 15:17:12 -04002883 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002884 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002885}
2886
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002887Image *Renderer11::createImage()
2888{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002889 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002890}
2891
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002892void Renderer11::generateMipmap(Image *dest, Image *src)
2893{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002894 Image11 *dest11 = Image11::makeImage11(dest);
2895 Image11 *src11 = Image11::makeImage11(src);
Geoff Lange4a492b2014-06-19 14:14:41 -04002896 Image11::generateMipmap(dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002897}
2898
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002899TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
2900{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002901 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
2902 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002903}
2904
Nicolas Capensbf712d02014-03-31 14:23:35 -04002905TextureStorage *Renderer11::createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002906{
Nicolas Capensbf712d02014-03-31 14:23:35 -04002907 return new TextureStorage11_2D(this, internalformat, renderTarget, width, height, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002908}
2909
Nicolas Capensbf712d02014-03-31 14:23:35 -04002910TextureStorage *Renderer11::createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002911{
Nicolas Capensbf712d02014-03-31 14:23:35 -04002912 return new TextureStorage11_Cube(this, internalformat, renderTarget, size, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002913}
2914
Nicolas Capensbf712d02014-03-31 14:23:35 -04002915TextureStorage *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 +00002916{
Nicolas Capensbf712d02014-03-31 14:23:35 -04002917 return new TextureStorage11_3D(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00002918}
2919
Nicolas Capensbf712d02014-03-31 14:23:35 -04002920TextureStorage *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 +00002921{
Nicolas Capensbf712d02014-03-31 14:23:35 -04002922 return new TextureStorage11_2DArray(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002923}
2924
Jamie Madilleb9baab2014-03-24 13:19:43 -04002925void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format,
2926 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void *pixels)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002927{
Jamie Madill1eb5bd72014-03-28 10:43:39 -04002928 ASSERT(area.width >= 0);
2929 ASSERT(area.height >= 0);
2930
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002931 D3D11_TEXTURE2D_DESC textureDesc;
2932 texture->GetDesc(&textureDesc);
2933
Jamie Madill1eb5bd72014-03-28 10:43:39 -04002934 // Clamp read region to the defined texture boundaries, preventing out of bounds reads
2935 // and reads of uninitialized data.
2936 gl::Rectangle safeArea;
2937 safeArea.x = gl::clamp(area.x, 0, static_cast<int>(textureDesc.Width));
2938 safeArea.y = gl::clamp(area.y, 0, static_cast<int>(textureDesc.Height));
2939 safeArea.width = gl::clamp(area.width + std::min(area.x, 0), 0,
2940 static_cast<int>(textureDesc.Width) - safeArea.x);
2941 safeArea.height = gl::clamp(area.height + std::min(area.y, 0), 0,
2942 static_cast<int>(textureDesc.Height) - safeArea.y);
2943
2944 ASSERT(safeArea.x >= 0 && safeArea.y >= 0);
2945 ASSERT(safeArea.x + safeArea.width <= static_cast<int>(textureDesc.Width));
2946 ASSERT(safeArea.y + safeArea.height <= static_cast<int>(textureDesc.Height));
2947
2948 if (safeArea.width == 0 || safeArea.height == 0)
2949 {
2950 // no work to do
2951 return;
2952 }
2953
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002954 D3D11_TEXTURE2D_DESC stagingDesc;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04002955 stagingDesc.Width = safeArea.width;
2956 stagingDesc.Height = safeArea.height;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002957 stagingDesc.MipLevels = 1;
2958 stagingDesc.ArraySize = 1;
2959 stagingDesc.Format = textureDesc.Format;
2960 stagingDesc.SampleDesc.Count = 1;
2961 stagingDesc.SampleDesc.Quality = 0;
2962 stagingDesc.Usage = D3D11_USAGE_STAGING;
2963 stagingDesc.BindFlags = 0;
2964 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
2965 stagingDesc.MiscFlags = 0;
2966
2967 ID3D11Texture2D* stagingTex = NULL;
2968 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
2969 if (FAILED(result))
2970 {
2971 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
2972 return;
2973 }
2974
2975 ID3D11Texture2D* srcTex = NULL;
2976 if (textureDesc.SampleDesc.Count > 1)
2977 {
2978 D3D11_TEXTURE2D_DESC resolveDesc;
2979 resolveDesc.Width = textureDesc.Width;
2980 resolveDesc.Height = textureDesc.Height;
2981 resolveDesc.MipLevels = 1;
2982 resolveDesc.ArraySize = 1;
2983 resolveDesc.Format = textureDesc.Format;
2984 resolveDesc.SampleDesc.Count = 1;
2985 resolveDesc.SampleDesc.Quality = 0;
2986 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
2987 resolveDesc.BindFlags = 0;
2988 resolveDesc.CPUAccessFlags = 0;
2989 resolveDesc.MiscFlags = 0;
2990
2991 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
2992 if (FAILED(result))
2993 {
2994 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04002995 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002996 return;
2997 }
2998
2999 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3000 subResource = 0;
3001 }
3002 else
3003 {
3004 srcTex = texture;
3005 srcTex->AddRef();
3006 }
3007
3008 D3D11_BOX srcBox;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003009 srcBox.left = static_cast<UINT>(safeArea.x);
3010 srcBox.right = static_cast<UINT>(safeArea.x + safeArea.width);
3011 srcBox.top = static_cast<UINT>(safeArea.y);
3012 srcBox.bottom = static_cast<UINT>(safeArea.y + safeArea.height);
3013 srcBox.front = 0;
3014 srcBox.back = 1;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003015
3016 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3017
Geoff Langea228632013-07-30 15:17:12 -04003018 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003019
Jamie Madill7538f7f2014-04-17 11:53:39 -04003020 PackPixelsParams packParams(safeArea, format, type, outputPitch, pack, 0);
3021 packPixels(stagingTex, packParams, pixels);
3022
3023 SafeRelease(stagingTex);
3024}
3025
3026void Renderer11::packPixels(ID3D11Texture2D *readTexture, const PackPixelsParams &params, void *pixelsOut)
3027{
3028 D3D11_TEXTURE2D_DESC textureDesc;
3029 readTexture->GetDesc(&textureDesc);
3030
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003031 D3D11_MAPPED_SUBRESOURCE mapping;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003032 HRESULT hr = mDeviceContext->Map(readTexture, 0, D3D11_MAP_READ, 0, &mapping);
Geoff Lang9cd19152014-05-28 15:54:34 -04003033 UNUSED_ASSERTION_VARIABLE(hr);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003034 ASSERT(SUCCEEDED(hr));
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003035
3036 unsigned char *source;
3037 int inputPitch;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003038 if (params.pack.reverseRowOrder)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003039 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003040 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (params.area.height - 1);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003041 inputPitch = -static_cast<int>(mapping.RowPitch);
3042 }
3043 else
3044 {
3045 source = static_cast<unsigned char*>(mapping.pData);
3046 inputPitch = static_cast<int>(mapping.RowPitch);
3047 }
3048
Geoff Lange4a492b2014-06-19 14:14:41 -04003049 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format);
3050 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat);
3051 GLenum sourceType = gl::GetType(sourceInternalFormat);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003052
Geoff Lange4a492b2014-06-19 14:14:41 -04003053 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003054
Jamie Madill7538f7f2014-04-17 11:53:39 -04003055 if (sourceFormat == params.format && sourceType == params.type)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003056 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003057 unsigned char *dest = static_cast<unsigned char*>(pixelsOut) + params.offset;
3058 for (int y = 0; y < params.area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003059 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003060 memcpy(dest + y * params.outputPitch, source + y * inputPitch, params.area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003061 }
3062 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003063 else
3064 {
Geoff Lange4a492b2014-06-19 14:14:41 -04003065 GLenum destInternalFormat = gl::GetSizedInternalFormat(params.format, params.type);
3066 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003067
Jamie Madill7538f7f2014-04-17 11:53:39 -04003068 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, params.format, params.type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003069 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003070 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003071 // Fast copy is possible through some special function
Jamie Madill7538f7f2014-04-17 11:53:39 -04003072 for (int y = 0; y < params.area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003073 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003074 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003075 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003076 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003077 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3078
3079 fastCopyFunc(src, dest);
3080 }
3081 }
3082 }
3083 else
3084 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003085 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Geoff Lange4a492b2014-06-19 14:14:41 -04003086 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(params.format, params.type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003087
3088 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3089 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3090 sizeof(temp) >= sizeof(gl::ColorUI) &&
3091 sizeof(temp) >= sizeof(gl::ColorI));
3092
Jamie Madill7538f7f2014-04-17 11:53:39 -04003093 for (int y = 0; y < params.area.height; y++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003094 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003095 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003096 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003097 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003098 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3099
3100 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3101 // will not allow the copy otherwise.
3102 readFunc(src, temp);
3103 writeFunc(temp, dest);
3104 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003105 }
3106 }
3107 }
3108
Jamie Madill7538f7f2014-04-17 11:53:39 -04003109 mDeviceContext->Unmap(readTexture, 0);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003110}
3111
Geoff Lang758d5b22013-06-11 11:42:50 -04003112bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003113 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3114 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003115{
Geoff Lang975af372013-06-12 11:19:22 -04003116 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3117 // it should never be the case that both color and depth/stencil need to be blitted at
3118 // at the same time.
3119 ASSERT(colorBlit != (depthBlit || stencilBlit));
3120
Geoff Langc1f51be2013-06-11 11:49:14 -04003121 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003122
Geoff Lang4d782732013-07-22 10:44:18 -04003123 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3124 if (!drawRenderTarget)
3125 {
3126 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3127 return gl::error(GL_OUT_OF_MEMORY, false);
3128 }
3129
3130 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3131 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3132 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3133 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3134
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003135 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3136 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003137 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003138 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003139 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003140 }
3141
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003142 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003143 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003144 unsigned int readSubresource = 0;
3145 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003146 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003147 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3148 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003149
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003150 if (unresolvedTexture)
3151 {
3152 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3153 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003154
Geoff Langea228632013-07-30 15:17:12 -04003155 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003156
Geoff Lang5c9a29a2014-02-11 10:26:24 -05003157 HRESULT hresult = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3158 if (FAILED(hresult))
Geoff Langc1f51be2013-06-11 11:49:14 -04003159 {
Geoff Langea228632013-07-30 15:17:12 -04003160 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003161 return gl::error(GL_OUT_OF_MEMORY, false);
3162 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003163 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003164 }
3165 else
3166 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003167 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003168 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003169 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003170 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003171 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003172 }
3173
Geoff Lang4d782732013-07-22 10:44:18 -04003174 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003175 {
Geoff Lang4d782732013-07-22 10:44:18 -04003176 SafeRelease(readTexture);
3177 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003178 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003179 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003180 }
3181
Geoff Lang125deab2013-08-09 13:34:16 -04003182 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3183 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3184
3185 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3186
3187 bool wholeBufferCopy = !scissorNeeded &&
3188 readRect.x == 0 && readRect.width == readSize.width &&
3189 readRect.y == 0 && readRect.height == readSize.height &&
3190 drawRect.x == 0 && drawRect.width == drawSize.width &&
3191 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003192
Geoff Langc1f51be2013-06-11 11:49:14 -04003193 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003194
Geoff Lang1cd1b212014-02-11 09:42:27 -05003195 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003196
3197 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3198 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3199 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3200 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3201
Geoff Lange4a492b2014-06-19 14:14:41 -04003202 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat()) > 0;
3203 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat()) > 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003204 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3205
Geoff Langc1f51be2013-06-11 11:49:14 -04003206 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003207 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3208 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003209 {
Geoff Lang125deab2013-08-09 13:34:16 -04003210 UINT dstX = drawRect.x;
3211 UINT dstY = drawRect.y;
3212
Geoff Langc1f51be2013-06-11 11:49:14 -04003213 D3D11_BOX readBox;
3214 readBox.left = readRect.x;
3215 readBox.right = readRect.x + readRect.width;
3216 readBox.top = readRect.y;
3217 readBox.bottom = readRect.y + readRect.height;
3218 readBox.front = 0;
3219 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003220
Geoff Lang125deab2013-08-09 13:34:16 -04003221 if (scissorNeeded)
3222 {
3223 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3224 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3225
3226 if (drawRect.x < scissor->x)
3227 {
3228 dstX = scissor->x;
3229 readBox.left += (scissor->x - drawRect.x);
3230 }
3231 if (drawRect.y < scissor->y)
3232 {
3233 dstY = scissor->y;
3234 readBox.top += (scissor->y - drawRect.y);
3235 }
3236 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3237 {
3238 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3239 }
3240 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3241 {
3242 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3243 }
3244 }
3245
Geoff Langc1f51be2013-06-11 11:49:14 -04003246 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3247 // We also require complete framebuffer copies for depth-stencil blit.
3248 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003249
Geoff Lang125deab2013-08-09 13:34:16 -04003250 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003251 readTexture, readSubresource, pSrcBox);
3252 result = true;
3253 }
3254 else
3255 {
3256 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003257 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003258
Geoff Lang975af372013-06-12 11:19:22 -04003259 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003260 {
Geoff Lang975af372013-06-12 11:19:22 -04003261 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003262 drawTexture, drawSubresource, drawArea, drawSize,
3263 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003264 }
3265 else if (depthBlit)
3266 {
Geoff Lang125deab2013-08-09 13:34:16 -04003267 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3268 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003269 }
3270 else if (stencilBlit)
3271 {
3272 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003273 drawTexture, drawSubresource, drawArea, drawSize,
3274 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003275 }
3276 else
3277 {
Geoff Lange4a492b2014-06-19 14:14:41 -04003278 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat());
Geoff Lang125deab2013-08-09 13:34:16 -04003279 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3280 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003281 }
3282 }
3283
3284 SafeRelease(readTexture);
3285 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003286
3287 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003288}
3289
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003290ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3291{
3292 D3D11_TEXTURE2D_DESC textureDesc;
3293 source->GetDesc(&textureDesc);
3294
3295 if (textureDesc.SampleDesc.Count > 1)
3296 {
3297 D3D11_TEXTURE2D_DESC resolveDesc;
3298 resolveDesc.Width = textureDesc.Width;
3299 resolveDesc.Height = textureDesc.Height;
3300 resolveDesc.MipLevels = 1;
3301 resolveDesc.ArraySize = 1;
3302 resolveDesc.Format = textureDesc.Format;
3303 resolveDesc.SampleDesc.Count = 1;
3304 resolveDesc.SampleDesc.Quality = 0;
3305 resolveDesc.Usage = textureDesc.Usage;
3306 resolveDesc.BindFlags = textureDesc.BindFlags;
3307 resolveDesc.CPUAccessFlags = 0;
3308 resolveDesc.MiscFlags = 0;
3309
3310 ID3D11Texture2D *resolveTexture = NULL;
3311 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3312 if (FAILED(result))
3313 {
3314 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3315 return NULL;
3316 }
3317
3318 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3319 return resolveTexture;
3320 }
3321 else
3322 {
3323 source->AddRef();
3324 return source;
3325 }
3326}
3327
Jamie Madill3c7fa222014-06-05 13:08:51 -04003328void Renderer11::invalidateFBOAttachmentSwizzles(gl::FramebufferAttachment *attachment, int mipLevel)
Geoff Lang42477a42013-09-17 17:07:02 -04003329{
Jamie Madill3c7fa222014-06-05 13:08:51 -04003330 ASSERT(attachment->isTexture());
3331 TextureStorage *texStorage = attachment->getTextureStorage();
Geoff Lang42477a42013-09-17 17:07:02 -04003332 if (texStorage)
3333 {
3334 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3335 if (!texStorage11)
3336 {
3337 ERR("texture storage pointer unexpectedly null.");
3338 return;
3339 }
3340
3341 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3342 }
3343}
3344
3345void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3346{
3347 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3348 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003349 gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(colorAttachment);
3350 if (attachment && attachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003351 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003352 invalidateFBOAttachmentSwizzles(attachment, framebuffer->getColorbufferMipLevel(colorAttachment));
Geoff Lang42477a42013-09-17 17:07:02 -04003353 }
3354 }
3355
Jamie Madill3c7fa222014-06-05 13:08:51 -04003356 gl::FramebufferAttachment *depthAttachment = framebuffer->getDepthbuffer();
3357 if (depthAttachment && depthAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003358 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003359 invalidateFBOAttachmentSwizzles(depthAttachment, framebuffer->getDepthbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003360 }
3361
Jamie Madill3c7fa222014-06-05 13:08:51 -04003362 gl::FramebufferAttachment *stencilAttachment = framebuffer->getStencilbuffer();
3363 if (stencilAttachment && stencilAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003364 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003365 invalidateFBOAttachmentSwizzles(stencilAttachment, framebuffer->getStencilbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003366 }
3367}
3368
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003369bool Renderer11::getLUID(LUID *adapterLuid) const
3370{
3371 adapterLuid->HighPart = 0;
3372 adapterLuid->LowPart = 0;
3373
3374 if (!mDxgiAdapter)
3375 {
3376 return false;
3377 }
3378
3379 DXGI_ADAPTER_DESC adapterDesc;
3380 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3381 {
3382 return false;
3383 }
3384
3385 *adapterLuid = adapterDesc.AdapterLuid;
3386 return true;
3387}
3388
Geoff Lang005df412013-10-16 14:12:50 -04003389GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003390{
Geoff Lange4a492b2014-06-19 14:14:41 -04003391 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat));
Jamie Madillc8c102b2013-10-10 15:10:24 -04003392}
3393
Jamie Madill95ffb862014-01-29 09:26:59 -05003394rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3395{
3396 return gl_d3d11::GetVertexConversionType(vertexFormat);
3397}
3398
3399GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3400{
3401 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3402}
3403
Geoff Lang61e49a52013-05-29 10:22:58 -04003404Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3405{
3406 MultisampleSupportInfo supportInfo = { 0 };
3407
3408 UINT formatSupport;
3409 HRESULT result;
3410
3411 result = mDevice->CheckFormatSupport(format, &formatSupport);
3412 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3413 {
3414 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3415 {
3416 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3417 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3418 {
3419 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3420 }
3421 else
3422 {
3423 supportInfo.qualityLevels[i - 1] = 0;
3424 }
3425 }
3426 }
3427
3428 return supportInfo;
3429}
3430
Geoff Langcec35902014-04-16 10:52:36 -04003431gl::Caps Renderer11::generateCaps() const
3432{
3433 return d3d11_gl::GenerateCaps(mDevice);
3434}
3435
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003436}