blob: 092a0284ba3f865ed7a552599466afdb26727ff0 [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"
25#include "libGLESv2/renderer/d3d/d3d11/BufferStorage11.h"
26#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"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000035#include "libEGL/Display.h"
36
Geoff Lang94a90892014-02-18 17:14:19 -050037// Enable ANGLE_SKIP_DXGI_1_2_CHECK if there is not a possibility of using cross-process
38// HWNDs or the Windows 7 Platform Update (KB2670838) is expected to be installed.
39#ifndef ANGLE_SKIP_DXGI_1_2_CHECK
40#define ANGLE_SKIP_DXGI_1_2_CHECK 0
41#endif
42
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000043#ifdef _DEBUG
44// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
45// and conformance tests. to enable all warnings, remove this define.
46#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
47#endif
48
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000049namespace rx
50{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000051static const DXGI_FORMAT RenderTargetFormats[] =
52 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000053 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000054 DXGI_FORMAT_R8G8B8A8_UNORM
55 };
56
57static const DXGI_FORMAT DepthStencilFormats[] =
58 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000059 DXGI_FORMAT_UNKNOWN,
60 DXGI_FORMAT_D24_UNORM_S8_UINT,
61 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000062 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000063
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000064enum
65{
66 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
67};
68
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000069Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
70{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000071 mVertexDataManager = NULL;
72 mIndexDataManager = NULL;
73
daniel@transgaming.comc5114302012-12-20 21:11:36 +000074 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000075 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000076
Geoff Langb86b9792013-06-04 16:32:05 -040077 mBlit = NULL;
Jamie Madilla21eea32013-09-18 14:36:25 -040078 mPixelTransfer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000079
Geoff Langda507fe2013-08-20 12:01:42 -040080 mClear = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000081
daniel@transgaming.combdf787f2013-02-01 03:20:36 +000082 mSyncQuery = NULL;
83
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000084 mD3d11Module = NULL;
85 mDxgiModule = NULL;
86
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000087 mDeviceLost = false;
88
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +000089 mMaxSupportedSamples = 0;
90
daniel@transgaming.com25072f62012-11-28 19:31:32 +000091 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000092 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000093 mDxgiAdapter = NULL;
94 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +000095
96 mDriverConstantBufferVS = NULL;
97 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +000098
Jamie Madill6246dc82014-01-29 09:26:47 -050099 mAppliedVertexShader = NULL;
100 mAppliedGeometryShader = NULL;
Geoff Lang4c5c6bb2014-02-05 16:32:46 -0500101 mCurPointGeometryShader = NULL;
Jamie Madill6246dc82014-01-29 09:26:47 -0500102 mAppliedPixelShader = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000103}
104
105Renderer11::~Renderer11()
106{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000107 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000108}
109
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000110Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
111{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000112 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000113 return static_cast<rx::Renderer11*>(renderer);
114}
115
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000116#ifndef __d3d11_1_h__
117#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
118#endif
119
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000120EGLint Renderer11::initialize()
121{
Geoff Langdad5ed32014-02-10 12:59:17 -0500122 if (!mCompiler.initialize())
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000123 {
124 return EGL_NOT_INITIALIZED;
125 }
126
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000127 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
128 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000129
130 if (mD3d11Module == NULL || mDxgiModule == NULL)
131 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000132 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000133 return EGL_NOT_INITIALIZED;
134 }
135
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000136 // create the D3D11 device
137 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000138 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000139
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000140 if (D3D11CreateDevice == NULL)
141 {
142 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
143 return EGL_NOT_INITIALIZED;
144 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000145
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000146 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000147 {
148 D3D_FEATURE_LEVEL_11_0,
149 D3D_FEATURE_LEVEL_10_1,
150 D3D_FEATURE_LEVEL_10_0,
151 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000152
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000153 HRESULT result = S_OK;
154
155#ifdef _DEBUG
156 result = D3D11CreateDevice(NULL,
157 D3D_DRIVER_TYPE_HARDWARE,
158 NULL,
159 D3D11_CREATE_DEVICE_DEBUG,
160 featureLevels,
161 ArraySize(featureLevels),
162 D3D11_SDK_VERSION,
163 &mDevice,
164 &mFeatureLevel,
165 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000166
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000167 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000168 {
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000169 ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n");
170 }
171
172 if (!mDevice || FAILED(result))
173#endif
174 {
175 result = D3D11CreateDevice(NULL,
176 D3D_DRIVER_TYPE_HARDWARE,
177 NULL,
178 0,
179 featureLevels,
180 ArraySize(featureLevels),
181 D3D11_SDK_VERSION,
182 &mDevice,
183 &mFeatureLevel,
184 &mDeviceContext);
185
186 if (!mDevice || FAILED(result))
187 {
188 ERR("Could not create D3D11 device - aborting!\n");
189 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
190 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000191 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000192
Geoff Lang94a90892014-02-18 17:14:19 -0500193#if !ANGLE_SKIP_DXGI_1_2_CHECK
194 // In order to create a swap chain for an HWND owned by another process, DXGI 1.2 is required.
195 // The easiest way to check is to query for a IDXGIDevice2.
196 bool requireDXGI1_2 = false;
197 HWND hwnd = WindowFromDC(mDc);
198 if (hwnd)
199 {
200 DWORD currentProcessId = GetCurrentProcessId();
201 DWORD wndProcessId;
202 GetWindowThreadProcessId(hwnd, &wndProcessId);
203 requireDXGI1_2 = (currentProcessId != wndProcessId);
204 }
205 else
206 {
207 requireDXGI1_2 = true;
208 }
209
210 if (requireDXGI1_2)
211 {
212 IDXGIDevice2 *dxgiDevice2 = NULL;
213 result = mDevice->QueryInterface(__uuidof(IDXGIDevice2), (void**)&dxgiDevice2);
214 if (FAILED(result))
215 {
216 ERR("DXGI 1.2 required to present to HWNDs owned by another process.\n");
217 return EGL_NOT_INITIALIZED;
218 }
219 SafeRelease(dxgiDevice2);
220 }
221#endif
222
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000223 IDXGIDevice *dxgiDevice = NULL;
224 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
225
226 if (FAILED(result))
227 {
228 ERR("Could not query DXGI device - aborting!\n");
229 return EGL_NOT_INITIALIZED;
230 }
231
232 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
233
234 if (FAILED(result))
235 {
236 ERR("Could not retrieve DXGI adapter - aborting!\n");
237 return EGL_NOT_INITIALIZED;
238 }
239
Geoff Langea228632013-07-30 15:17:12 -0400240 SafeRelease(dxgiDevice);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000241
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000242 mDxgiAdapter->GetDesc(&mAdapterDescription);
243 memset(mDescription, 0, sizeof(mDescription));
244 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
245
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000246 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
247
248 if (!mDxgiFactory || FAILED(result))
249 {
250 ERR("Could not create DXGI factory - aborting!\n");
251 return EGL_NOT_INITIALIZED;
252 }
253
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000254 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
255#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
256 ID3D11InfoQueue *infoQueue;
257 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
258
259 if (SUCCEEDED(result))
260 {
261 D3D11_MESSAGE_ID hideMessages[] =
262 {
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000263 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000264 };
265
266 D3D11_INFO_QUEUE_FILTER filter = {0};
267 filter.DenyList.NumIDs = ArraySize(hideMessages);
268 filter.DenyList.pIDList = hideMessages;
269
270 infoQueue->AddStorageFilterEntries(&filter);
Geoff Langea228632013-07-30 15:17:12 -0400271 SafeRelease(infoQueue);
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000272 }
273#endif
274
Geoff Lang61e49a52013-05-29 10:22:58 -0400275 mMaxSupportedSamples = 0;
276
277 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
278 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000279 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400280 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
281 mMultisampleSupportMap.insert(std::make_pair(*i, support));
282 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000283 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000284
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000285 initializeDevice();
286
287 return EGL_SUCCESS;
288}
289
290// do any one-time device initialization
291// NOTE: this is also needed after a device lost/reset
292// to reset the scene status and ensure the default states are reset.
293void Renderer11::initializeDevice()
294{
Geoff Lange4a492b2014-06-19 14:14:41 -0400295 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000296 mInputLayoutCache.initialize(mDevice, mDeviceContext);
297
298 ASSERT(!mVertexDataManager && !mIndexDataManager);
299 mVertexDataManager = new VertexDataManager(this);
300 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000301
Geoff Langb86b9792013-06-04 16:32:05 -0400302 ASSERT(!mBlit);
303 mBlit = new Blit11(this);
304
Geoff Langda507fe2013-08-20 12:01:42 -0400305 ASSERT(!mClear);
306 mClear = new Clear11(this);
307
Jamie Madilla21eea32013-09-18 14:36:25 -0400308 ASSERT(!mPixelTransfer);
309 mPixelTransfer = new PixelTransfer11(this);
310
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000311 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000312}
313
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000314int Renderer11::generateConfigs(ConfigDesc **configDescList)
315{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000316 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
317 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000318 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
319 int numConfigs = 0;
320
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000321 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000322 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000323 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000324 {
325 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
326
327 UINT formatSupport = 0;
328 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000329
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000330 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
331 {
332 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
333
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000334 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000335
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000336 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
337 {
Geoff Lang5c9a29a2014-02-11 10:26:24 -0500338 UINT depthStencilSupport = 0;
339 result = mDevice->CheckFormatSupport(depthStencilFormat, &depthStencilSupport);
340 depthStencilFormatOK = SUCCEEDED(result) && (depthStencilSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000341 }
342
343 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000344 {
345 ConfigDesc newConfig;
Geoff Lange4a492b2014-06-19 14:14:41 -0400346 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat);
347 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000348 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
349 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000350 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000351
352 (*configDescList)[numConfigs++] = newConfig;
353 }
354 }
355 }
356 }
357
358 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000359}
360
361void Renderer11::deleteConfigs(ConfigDesc *configDescList)
362{
363 delete [] (configDescList);
364}
365
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000366void Renderer11::sync(bool block)
367{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000368 if (block)
369 {
370 HRESULT result;
371
372 if (!mSyncQuery)
373 {
374 D3D11_QUERY_DESC queryDesc;
375 queryDesc.Query = D3D11_QUERY_EVENT;
376 queryDesc.MiscFlags = 0;
377
378 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
379 ASSERT(SUCCEEDED(result));
380 }
381
382 mDeviceContext->End(mSyncQuery);
383 mDeviceContext->Flush();
384
385 do
386 {
387 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
388
389 // Keep polling, but allow other threads to do something useful first
390 Sleep(0);
391
392 if (testDeviceLost(true))
393 {
394 return;
395 }
396 }
397 while (result == S_FALSE);
398 }
399 else
400 {
401 mDeviceContext->Flush();
402 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000403}
404
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000405SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
406{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000407 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000408}
409
Geoff Lange2e0ce02013-09-17 17:05:08 -0400410void Renderer11::generateSwizzle(gl::Texture *texture)
411{
Geoff Lang42477a42013-09-17 17:07:02 -0400412 if (texture)
413 {
414 TextureStorageInterface *texStorage = texture->getNativeTexture();
415 if (texStorage)
416 {
417 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
418
419 storage11->generateSwizzles(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
420 texture->getSwizzleAlpha());
421 }
422 }
Geoff Lange2e0ce02013-09-17 17:05:08 -0400423}
424
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000425void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
426{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000427 if (type == gl::SAMPLER_PIXEL)
428 {
429 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
430 {
431 ERR("Pixel shader sampler index %i is not valid.", index);
432 return;
433 }
434
435 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
436 {
437 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
438
439 if (!dxSamplerState)
440 {
441 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
442 "sampler state for pixel shaders at slot %i.", index);
443 }
444
445 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
446
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000447 mCurPixelSamplerStates[index] = samplerState;
448 }
449
450 mForceSetPixelSamplerStates[index] = false;
451 }
452 else if (type == gl::SAMPLER_VERTEX)
453 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000454 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000455 {
456 ERR("Vertex shader sampler index %i is not valid.", index);
457 return;
458 }
459
460 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
461 {
462 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
463
464 if (!dxSamplerState)
465 {
466 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
467 "sampler state for vertex shaders at slot %i.", index);
468 }
469
470 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
471
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000472 mCurVertexSamplerStates[index] = samplerState;
473 }
474
475 mForceSetVertexSamplerStates[index] = false;
476 }
477 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000478}
479
480void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
481{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000482 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000483 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000484
485 if (texture)
486 {
487 TextureStorageInterface *texStorage = texture->getNativeTexture();
488 if (texStorage)
489 {
490 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
Nicolas Capensa10c1c72014-03-27 11:20:52 -0400491 gl::SamplerState samplerState;
492 texture->getSamplerState(&samplerState);
493 textureSRV = storage11->getSRV(samplerState);
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000494 }
495
496 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
497 // missing the shader resource view
498 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000499
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000500 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000501 }
502
503 if (type == gl::SAMPLER_PIXEL)
504 {
505 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
506 {
507 ERR("Pixel shader sampler index %i is not valid.", index);
508 return;
509 }
510
Geoff Lang91382e52014-01-07 16:16:30 -0500511 if (forceSetTexture || mCurPixelSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000512 {
513 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
514 }
515
Geoff Lang91382e52014-01-07 16:16:30 -0500516 mCurPixelSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000517 }
518 else if (type == gl::SAMPLER_VERTEX)
519 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000520 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000521 {
522 ERR("Vertex shader sampler index %i is not valid.", index);
523 return;
524 }
525
Geoff Lang91382e52014-01-07 16:16:30 -0500526 if (forceSetTexture || mCurVertexSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000527 {
528 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
529 }
530
Geoff Lang91382e52014-01-07 16:16:30 -0500531 mCurVertexSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000532 }
533 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000534}
535
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000536bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
537{
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000538 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
539 {
540 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
541 if (uniformBuffer)
542 {
543 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500544 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000545
546 if (!constantBuffer)
547 {
548 return false;
549 }
550
Geoff Langc6354ee2013-07-22 10:40:07 -0400551 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
552 {
553 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
554 1, &constantBuffer);
555 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
556 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000557 }
558 }
559
560 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
561 {
562 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
563 if (uniformBuffer)
564 {
565 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500566 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000567
568 if (!constantBuffer)
569 {
570 return false;
571 }
572
Geoff Langc6354ee2013-07-22 10:40:07 -0400573 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
574 {
575 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
576 1, &constantBuffer);
577 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
578 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000579 }
580 }
581
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000582 return true;
583}
584
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000585void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000586{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000587 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000588 {
Nicolas Capensaea8e942014-05-09 19:14:08 -0400589 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000590 if (!dxRasterState)
591 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000592 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000593 "rasterizer state.");
594 }
595
596 mDeviceContext->RSSetState(dxRasterState);
597
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000598 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000599 }
600
601 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000602}
603
Geoff Langc142e9d2013-09-30 15:19:47 -0400604void Renderer11::setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000605 unsigned int sampleMask)
606{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000607 if (mForceSetBlendState ||
608 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400609 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000610 sampleMask != mCurSampleMask)
611 {
Geoff Langc142e9d2013-09-30 15:19:47 -0400612 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(framebuffer, blendState);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000613 if (!dxBlendState)
614 {
615 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
616 "blend state.");
617 }
618
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000619 float blendColors[4] = {0.0f};
620 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
621 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
622 {
623 blendColors[0] = blendColor.red;
624 blendColors[1] = blendColor.green;
625 blendColors[2] = blendColor.blue;
626 blendColors[3] = blendColor.alpha;
627 }
628 else
629 {
630 blendColors[0] = blendColor.alpha;
631 blendColors[1] = blendColor.alpha;
632 blendColors[2] = blendColor.alpha;
633 blendColors[3] = blendColor.alpha;
634 }
635
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000636 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
637
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000638 mCurBlendState = blendState;
639 mCurBlendColor = blendColor;
640 mCurSampleMask = sampleMask;
641 }
642
643 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000644}
645
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000646void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000647 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000648{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000649 if (mForceSetDepthStencilState ||
650 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
651 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
652 {
Jamie Madillac528012014-06-20 13:21:23 -0400653 ASSERT(depthStencilState.stencilWritemask == depthStencilState.stencilBackWritemask);
654 ASSERT(stencilRef == stencilBackRef);
655 ASSERT(depthStencilState.stencilMask == depthStencilState.stencilBackMask);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000656
657 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
658 if (!dxDepthStencilState)
659 {
660 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
661 "setting the default depth stencil state.");
662 }
663
Jamie Madillec91cd32014-01-21 16:38:12 -0500664 // Max D3D11 stencil reference value is 0xFF, corresponding to the max 8 bits in a stencil buffer
665 // GL specifies we should clamp the ref value to the nearest bit depth when doing stencil ops
666 META_ASSERT(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF);
667 META_ASSERT(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF);
Geoff Lang0bf3a982014-02-11 09:40:48 -0500668 UINT dxStencilRef = std::min<UINT>(stencilRef, 0xFFu);
Jamie Madillec91cd32014-01-21 16:38:12 -0500669
Geoff Lang0bf3a982014-02-11 09:40:48 -0500670 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, dxStencilRef);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000671
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000672 mCurDepthStencilState = depthStencilState;
673 mCurStencilRef = stencilRef;
674 mCurStencilBackRef = stencilBackRef;
675 }
676
677 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000678}
679
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000680void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000681{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000682 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
683 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000684 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000685 if (enabled)
686 {
687 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000688 rect.left = std::max(0, scissor.x);
689 rect.top = std::max(0, scissor.y);
690 rect.right = scissor.x + std::max(0, scissor.width);
691 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000692
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000693 mDeviceContext->RSSetScissorRects(1, &rect);
694 }
695
696 if (enabled != mScissorEnabled)
697 {
698 mForceSetRasterState = true;
699 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000700
701 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000702 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000703 }
704
705 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000706}
707
daniel@transgaming.com12985182012-12-20 20:56:31 +0000708bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000709 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000710{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000711 gl::Rectangle actualViewport = viewport;
712 float actualZNear = gl::clamp01(zNear);
713 float actualZFar = gl::clamp01(zFar);
714 if (ignoreViewport)
715 {
716 actualViewport.x = 0;
717 actualViewport.y = 0;
718 actualViewport.width = mRenderTargetDesc.width;
719 actualViewport.height = mRenderTargetDesc.height;
720 actualZNear = 0.0f;
721 actualZFar = 1.0f;
722 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000723
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000724 // Get D3D viewport bounds, which depends on the feature level
725 const Range& viewportBounds = getViewportBounds();
726
727 // 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 +0000728 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000729 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
730 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
731 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
732 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
733 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
734 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(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 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000935 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), 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 {
Geoff Lang7840b172014-03-13 11:20:44 -0400990 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
991 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 {
1020 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(transformFeedbackBuffers[i]->getStorage());
1021 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;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001128 BufferStorage *storage = indexBuffer->getStorage();
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;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001233 BufferStorage *storage = indexBuffer->getStorage();
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.com8ce2f8f2013-02-28 23:07:10 +00001816Range Renderer11::getViewportBounds() const
1817{
1818 switch (mFeatureLevel)
1819 {
1820 case D3D_FEATURE_LEVEL_11_0:
1821 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1822 case D3D_FEATURE_LEVEL_10_1:
1823 case D3D_FEATURE_LEVEL_10_0:
1824 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1825 default: UNREACHABLE();
1826 return Range(0, 0);
1827 }
1828}
1829
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001830unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001831{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001832 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1833 switch (mFeatureLevel)
1834 {
1835 case D3D_FEATURE_LEVEL_11_0:
1836 case D3D_FEATURE_LEVEL_10_1:
1837 case D3D_FEATURE_LEVEL_10_0:
1838 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1839 default: UNREACHABLE();
1840 return 0;
1841 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001842}
1843
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001844unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1845{
1846 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1847}
1848
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001849unsigned int Renderer11::getReservedVertexUniformVectors() const
1850{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001851 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001852}
1853
1854unsigned int Renderer11::getReservedFragmentUniformVectors() const
1855{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001856 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001857}
1858
1859unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001860{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001861 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1862 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1863 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001864}
1865
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001866unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001867{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001868 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1869 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1870 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001871}
1872
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001873unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001874{
1875 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001876 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1877 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001878 switch (mFeatureLevel)
1879 {
1880 case D3D_FEATURE_LEVEL_11_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001881 return D3D11_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001882 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001883 return D3D10_1_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001884 case D3D_FEATURE_LEVEL_10_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001885 return D3D10_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001886 default: UNREACHABLE();
1887 return 0;
1888 }
1889}
1890
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001891unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
1892{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001893 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1894 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1895
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001896 switch (mFeatureLevel)
1897 {
1898 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001899 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001900 case D3D_FEATURE_LEVEL_10_1:
1901 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001902 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001903 default: UNREACHABLE();
1904 return 0;
1905 }
1906}
1907
1908unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
1909{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001910 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1911 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1912
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001913 switch (mFeatureLevel)
1914 {
1915 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001916 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001917 case D3D_FEATURE_LEVEL_10_1:
1918 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001919 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001920 default: UNREACHABLE();
1921 return 0;
1922 }
1923}
1924
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001925unsigned int Renderer11::getReservedVertexUniformBuffers() const
1926{
1927 // we reserve one buffer for the application uniforms, and one for driver uniforms
1928 return 2;
1929}
1930
1931unsigned int Renderer11::getReservedFragmentUniformBuffers() const
1932{
1933 // we reserve one buffer for the application uniforms, and one for driver uniforms
1934 return 2;
1935}
1936
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001937unsigned int Renderer11::getReservedVaryings() const
1938{
Jamie Madill2bf8b372014-06-16 17:18:51 -04001939 // We potentially reserve varyings for gl_Position, dx_Position, gl_FragCoord and gl_PointSize
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001940 return 4;
1941}
1942
1943
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001944unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
1945{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001946 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
1947 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
1948
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001949 switch (mFeatureLevel)
1950 {
1951 case D3D_FEATURE_LEVEL_11_0:
1952 return D3D11_SO_BUFFER_SLOT_COUNT;
1953 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001954 return D3D10_1_SO_BUFFER_SLOT_COUNT;
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001955 case D3D_FEATURE_LEVEL_10_0:
1956 return D3D10_SO_BUFFER_SLOT_COUNT;
1957 default: UNREACHABLE();
1958 return 0;
1959 }
1960}
1961
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001962unsigned int Renderer11::getMaxTransformFeedbackSeparateComponents() const
1963{
1964 switch (mFeatureLevel)
1965 {
1966 case D3D_FEATURE_LEVEL_11_0:
1967 return getMaxTransformFeedbackInterleavedComponents() / getMaxTransformFeedbackBuffers();
1968 case D3D_FEATURE_LEVEL_10_1:
1969 case D3D_FEATURE_LEVEL_10_0:
1970 // D3D 10 and 10.1 only allow one output per output slot if an output slot other than zero
1971 // is used.
1972 return 4;
1973 default: UNREACHABLE();
1974 return 0;
1975 }
1976}
1977
1978unsigned int Renderer11::getMaxTransformFeedbackInterleavedComponents() const
1979{
1980 return (getMaxVaryingVectors() * 4);
1981}
1982
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00001983unsigned int Renderer11::getMaxUniformBufferSize() const
1984{
1985 // Each component is a 4-element vector of 4-byte units (floats)
1986 const unsigned int bytesPerComponent = 4 * sizeof(float);
1987
1988 switch (mFeatureLevel)
1989 {
1990 case D3D_FEATURE_LEVEL_11_0:
1991 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1992 case D3D_FEATURE_LEVEL_10_1:
1993 case D3D_FEATURE_LEVEL_10_0:
1994 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1995 default: UNREACHABLE();
1996 return 0;
1997 }
1998}
1999
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002000bool Renderer11::getShareHandleSupport() const
2001{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002002 // We only currently support share handles with BGRA surfaces, because
2003 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002004 // PIX doesn't seem to support using share handles, so disable them.
Geoff Langcec35902014-04-16 10:52:36 -04002005 return getCaps().extensions.textureFormatBGRA8888 && !gl::perfActive();
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002006}
2007
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002008bool Renderer11::getPostSubBufferSupport() const
2009{
2010 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2011 return false;
2012}
2013
Jamie Madill13a2f852013-12-11 16:35:08 -05002014int Renderer11::getMaxRecommendedElementsIndices() const
2015{
2016 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2017 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2018
2019 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2020 return std::numeric_limits<GLint>::max();
2021}
2022
2023int Renderer11::getMaxRecommendedElementsVertices() const
2024{
2025 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2026 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2027
2028 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2029 return std::numeric_limits<GLint>::max();
2030}
2031
Geoff Lang05b05022014-06-11 15:31:45 -04002032bool Renderer11::getSRGBTextureSupport() const
2033{
2034 return true;
2035}
2036
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002037int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002038{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002039 switch (mFeatureLevel)
2040 {
2041 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002042 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002043 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2044 default: UNREACHABLE(); return 0;
2045 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002046}
2047
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002048int Renderer11::getMinorShaderModel() const
2049{
2050 switch (mFeatureLevel)
2051 {
2052 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2053 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2054 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2055 default: UNREACHABLE(); return 0;
2056 }
2057}
2058
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002059float Renderer11::getMaxPointSize() const
2060{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002061 // choose a reasonable maximum. we enforce this in the shader.
2062 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2063 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002064}
2065
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002066int Renderer11::getMaxViewportDimension() const
2067{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002068 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2069 // In our case return the maximum texture size, which is the maximum render buffer size.
2070 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2071 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2072
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002073 switch (mFeatureLevel)
2074 {
2075 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002076 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002077 case D3D_FEATURE_LEVEL_10_1:
2078 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002079 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002080 default: UNREACHABLE();
2081 return 0;
2082 }
2083}
2084
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002085int Renderer11::getMaxTextureWidth() const
2086{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002087 switch (mFeatureLevel)
2088 {
2089 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2090 case D3D_FEATURE_LEVEL_10_1:
2091 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2092 default: UNREACHABLE(); return 0;
2093 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002094}
2095
2096int Renderer11::getMaxTextureHeight() const
2097{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002098 switch (mFeatureLevel)
2099 {
2100 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2101 case D3D_FEATURE_LEVEL_10_1:
2102 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2103 default: UNREACHABLE(); return 0;
2104 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002105}
2106
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002107int Renderer11::getMaxTextureDepth() const
2108{
2109 switch (mFeatureLevel)
2110 {
2111 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2112 case D3D_FEATURE_LEVEL_10_1:
2113 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2114 default: UNREACHABLE(); return 0;
2115 }
2116}
2117
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002118int Renderer11::getMaxTextureArrayLayers() const
2119{
2120 switch (mFeatureLevel)
2121 {
2122 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2123 case D3D_FEATURE_LEVEL_10_1:
2124 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2125 default: UNREACHABLE(); return 0;
2126 }
2127}
2128
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002129int Renderer11::getMinSwapInterval() const
2130{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002131 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002132}
2133
2134int Renderer11::getMaxSwapInterval() const
2135{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002136 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002137}
2138
2139int Renderer11::getMaxSupportedSamples() const
2140{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002141 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002142}
2143
Geoff Lang005df412013-10-16 14:12:50 -04002144GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002145{
Geoff Lange4a492b2014-06-19 14:14:41 -04002146 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat);
Geoff Lang0e120e32013-05-29 10:23:55 -04002147 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2148 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2149}
2150
Geoff Lang005df412013-10-16 14:12:50 -04002151GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002152{
2153 unsigned int numCounts = 0;
2154
2155 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Lange4a492b2014-06-19 14:14:41 -04002156 GLenum componentType = gl::GetComponentType(internalFormat);
Geoff Langb2f3d052013-08-13 12:49:27 -04002157 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002158 {
Geoff Lange4a492b2014-06-19 14:14:41 -04002159 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat);
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002160 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2161
2162 if (iter != mMultisampleSupportMap.end())
2163 {
2164 const MultisampleSupportInfo& info = iter->second;
2165 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2166 {
2167 if (info.qualityLevels[i] > 0)
2168 {
2169 numCounts++;
2170 }
2171 }
2172 }
2173 }
2174
2175 return numCounts;
2176}
2177
Geoff Lang005df412013-10-16 14:12:50 -04002178void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002179{
2180 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Lange4a492b2014-06-19 14:14:41 -04002181 GLenum componentType = gl::GetComponentType(internalFormat);
Geoff Langb2f3d052013-08-13 12:49:27 -04002182 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2183 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002184 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002185 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002186
Geoff Lange4a492b2014-06-19 14:14:41 -04002187 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat);
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002188 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2189
2190 if (iter != mMultisampleSupportMap.end())
2191 {
2192 const MultisampleSupportInfo& info = iter->second;
2193 int bufPos = 0;
2194 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2195 {
2196 if (info.qualityLevels[i] > 0)
2197 {
2198 params[bufPos++] = i + 1;
2199 }
2200 }
2201 }
2202}
2203
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002204int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2205{
2206 if (requested == 0)
2207 {
2208 return 0;
2209 }
2210
2211 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2212 if (iter != mMultisampleSupportMap.end())
2213 {
2214 const MultisampleSupportInfo& info = iter->second;
2215 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2216 {
2217 if (info.qualityLevels[i] > 0)
2218 {
2219 return i + 1;
2220 }
2221 }
2222 }
2223
2224 return -1;
2225}
2226
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002227unsigned int Renderer11::getMaxRenderTargets() const
2228{
Geoff Lang4ace4232014-06-18 19:12:48 -04002229 return d3d11::GetMaximumSimultaneousRenderTargets(mFeatureLevel);
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002230}
2231
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002232bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002233{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002234 if (source && dest)
2235 {
2236 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2237 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2238
Nicolas Capens76b258f2014-04-03 10:59:42 -04002239 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002240
2241 dest11->invalidateSwizzleCache();
2242
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002243 return true;
2244 }
2245
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002246 return false;
2247}
2248
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002249bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002250{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002251 if (source && dest)
2252 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002253 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2254 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002255
Nicolas Capens76b258f2014-04-03 10:59:42 -04002256 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002257
2258 dest11->invalidateSwizzleCache();
2259
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002260 return true;
2261 }
2262
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002263 return false;
2264}
2265
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002266bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2267{
2268 if (source && dest)
2269 {
2270 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2271 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2272
Nicolas Capens76b258f2014-04-03 10:59:42 -04002273 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002274
2275 dest11->invalidateSwizzleCache();
2276
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002277 return true;
2278 }
2279
2280 return false;
2281}
2282
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002283bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2284{
2285 if (source && dest)
2286 {
2287 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2288 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2289
Nicolas Capens76b258f2014-04-03 10:59:42 -04002290 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002291
2292 dest11->invalidateSwizzleCache();
2293
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002294 return true;
2295 }
2296
2297 return false;
2298}
2299
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002300bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002301 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002302{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002303 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002304 if (!colorbuffer)
2305 {
2306 ERR("Failed to retrieve the color buffer from the frame buffer.");
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 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2311 if (!sourceRenderTarget)
2312 {
2313 ERR("Failed to retrieve the render target from the frame buffer.");
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
2317 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2318 if (!source)
2319 {
2320 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002321 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002322 }
2323
2324 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2325 if (!storage11)
2326 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002327 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002328 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002329 }
2330
2331 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2332 if (!destRenderTarget)
2333 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002334 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002335 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002336 }
2337
2338 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2339 if (!dest)
2340 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002341 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002342 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002343 }
2344
Geoff Langb86b9792013-06-04 16:32:05 -04002345 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2346 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002347
Geoff Langb86b9792013-06-04 16:32:05 -04002348 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2349 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002350
Geoff Langb86b9792013-06-04 16:32:05 -04002351 // Use nearest filtering because source and destination are the same size for the direct
2352 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002353 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002354 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002355
Geoff Lang42477a42013-09-17 17:07:02 -04002356 storage11->invalidateSwizzleCacheLevel(level);
2357
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002358 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002359}
2360
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002361bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002362 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002363{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002364 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002365 if (!colorbuffer)
2366 {
2367 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002368 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002369 }
2370
2371 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2372 if (!sourceRenderTarget)
2373 {
2374 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002375 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002376 }
2377
2378 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2379 if (!source)
2380 {
2381 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002382 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002383 }
2384
2385 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2386 if (!storage11)
2387 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002388 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002389 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002390 }
2391
Nicolas Capensb13f8662013-06-04 13:30:19 -04002392 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002393 if (!destRenderTarget)
2394 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002395 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002396 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002397 }
2398
2399 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2400 if (!dest)
2401 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002402 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002403 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002404 }
2405
Geoff Langb86b9792013-06-04 16:32:05 -04002406 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2407 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002408
Geoff Langb86b9792013-06-04 16:32:05 -04002409 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2410 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002411
Geoff Langb86b9792013-06-04 16:32:05 -04002412 // Use nearest filtering because source and destination are the same size for the direct
2413 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002414 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002415 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002416
Geoff Lang42477a42013-09-17 17:07:02 -04002417 storage11->invalidateSwizzleCacheLevel(level);
2418
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002419 return ret;
2420}
2421
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002422bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2423 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2424{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002425 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002426 if (!colorbuffer)
2427 {
2428 ERR("Failed to retrieve the color buffer from the frame buffer.");
2429 return gl::error(GL_OUT_OF_MEMORY, false);
2430 }
2431
2432 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2433 if (!sourceRenderTarget)
2434 {
2435 ERR("Failed to retrieve the render target from the frame buffer.");
2436 return gl::error(GL_OUT_OF_MEMORY, false);
2437 }
2438
2439 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2440 if (!source)
2441 {
2442 ERR("Failed to retrieve the render target view from the render target.");
2443 return gl::error(GL_OUT_OF_MEMORY, false);
2444 }
2445
2446 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2447 if (!storage11)
2448 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002449 ERR("Failed to retrieve the texture storage from the destination.");
2450 return gl::error(GL_OUT_OF_MEMORY, false);
2451 }
2452
2453 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2454 if (!destRenderTarget)
2455 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002456 ERR("Failed to retrieve the render target from the destination storage.");
2457 return gl::error(GL_OUT_OF_MEMORY, false);
2458 }
2459
2460 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2461 if (!dest)
2462 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002463 ERR("Failed to retrieve the render target view from the destination render target.");
2464 return gl::error(GL_OUT_OF_MEMORY, false);
2465 }
2466
Geoff Langb86b9792013-06-04 16:32:05 -04002467 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2468 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002469
Geoff Langb86b9792013-06-04 16:32:05 -04002470 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2471 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002472
Geoff Langb86b9792013-06-04 16:32:05 -04002473 // Use nearest filtering because source and destination are the same size for the direct
2474 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002475 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002476 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002477
Geoff Lang42477a42013-09-17 17:07:02 -04002478 storage11->invalidateSwizzleCacheLevel(level);
2479
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002480 return ret;
2481}
2482
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002483bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2484 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2485{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002486 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002487 if (!colorbuffer)
2488 {
2489 ERR("Failed to retrieve the color buffer from the frame buffer.");
2490 return gl::error(GL_OUT_OF_MEMORY, false);
2491 }
2492
2493 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2494 if (!sourceRenderTarget)
2495 {
2496 ERR("Failed to retrieve the render target from the frame buffer.");
2497 return gl::error(GL_OUT_OF_MEMORY, false);
2498 }
2499
2500 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2501 if (!source)
2502 {
2503 ERR("Failed to retrieve the render target view from the render target.");
2504 return gl::error(GL_OUT_OF_MEMORY, false);
2505 }
2506
2507 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2508 if (!storage11)
2509 {
Geoff Langea228632013-07-30 15:17:12 -04002510 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002511 ERR("Failed to retrieve the texture storage from the destination.");
2512 return gl::error(GL_OUT_OF_MEMORY, false);
2513 }
2514
2515 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2516 if (!destRenderTarget)
2517 {
Geoff Langea228632013-07-30 15:17:12 -04002518 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002519 ERR("Failed to retrieve the render target from the destination storage.");
2520 return gl::error(GL_OUT_OF_MEMORY, false);
2521 }
2522
2523 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2524 if (!dest)
2525 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002526 ERR("Failed to retrieve the render target view from the destination render target.");
2527 return gl::error(GL_OUT_OF_MEMORY, false);
2528 }
2529
Geoff Langb86b9792013-06-04 16:32:05 -04002530 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2531 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002532
Geoff Langb86b9792013-06-04 16:32:05 -04002533 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2534 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002535
Geoff Langb86b9792013-06-04 16:32:05 -04002536 // Use nearest filtering because source and destination are the same size for the direct
2537 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002538 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002539 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002540
Geoff Lang42477a42013-09-17 17:07:02 -04002541 storage11->invalidateSwizzleCacheLevel(level);
2542
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002543 return ret;
2544}
2545
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002546void Renderer11::unapplyRenderTargets()
2547{
2548 setOneTimeRenderTarget(NULL);
2549}
2550
2551void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2552{
2553 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2554
2555 rtvArray[0] = renderTargetView;
2556
2557 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2558
2559 // Do not preserve the serial for this one-time-use render target
2560 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2561 {
2562 mAppliedRenderTargetSerials[rtIndex] = 0;
2563 }
2564}
2565
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002566RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2567{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002568 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002569 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002570
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002571 if (depth)
2572 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002573 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002574 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002575 swapChain11->getDepthStencilTexture(),
2576 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002577 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002578 }
2579 else
2580 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002581 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002582 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002583 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002584 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002585 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002586 }
2587 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002588}
2589
Geoff Langa2d97f12013-06-11 11:44:02 -04002590RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002591{
Geoff Langa2d97f12013-06-11 11:44:02 -04002592 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002593 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002594}
2595
Geoff Lang48dcae72014-02-05 16:28:24 -05002596ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type,
2597 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2598 bool separatedOutputBuffers)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002599{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002600 ShaderExecutable11 *executable = NULL;
Geoff Lang48dcae72014-02-05 16:28:24 -05002601 HRESULT result;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002602
2603 switch (type)
2604 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002605 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002606 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002607 ID3D11VertexShader *vertexShader = NULL;
2608 ID3D11GeometryShader *streamOutShader = NULL;
2609
2610 result = mDevice->CreateVertexShader(function, length, NULL, &vertexShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002611 ASSERT(SUCCEEDED(result));
2612
Geoff Lang48dcae72014-02-05 16:28:24 -05002613 if (transformFeedbackVaryings.size() > 0)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002614 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002615 std::vector<D3D11_SO_DECLARATION_ENTRY> soDeclaration;
2616 for (size_t i = 0; i < transformFeedbackVaryings.size(); i++)
2617 {
2618 const gl::LinkedVarying &varying = transformFeedbackVaryings[i];
Jamie Madill6195ef82014-06-18 10:09:43 -04002619 GLenum transposedType = gl::TransposeMatrixType(varying.type);
2620
Geoff Langcebb5aa2014-04-07 14:13:40 -04002621 for (size_t j = 0; j < varying.semanticIndexCount; j++)
Geoff Lang48dcae72014-02-05 16:28:24 -05002622 {
2623 D3D11_SO_DECLARATION_ENTRY entry = { 0 };
2624 entry.Stream = 0;
Geoff Langcebb5aa2014-04-07 14:13:40 -04002625 entry.SemanticName = varying.semanticName.c_str();
2626 entry.SemanticIndex = varying.semanticIndex + j;
Geoff Lang48dcae72014-02-05 16:28:24 -05002627 entry.StartComponent = 0;
Jamie Madill6195ef82014-06-18 10:09:43 -04002628 entry.ComponentCount = gl::VariableColumnCount(transposedType);
Geoff Lang48dcae72014-02-05 16:28:24 -05002629 entry.OutputSlot = (separatedOutputBuffers ? i : 0);
2630 soDeclaration.push_back(entry);
2631 }
2632 }
2633
2634 result = mDevice->CreateGeometryShaderWithStreamOutput(function, length, soDeclaration.data(), soDeclaration.size(),
2635 NULL, 0, 0, NULL, &streamOutShader);
2636 ASSERT(SUCCEEDED(result));
2637 }
2638
2639 if (vertexShader)
2640 {
2641 executable = new ShaderExecutable11(function, length, vertexShader, streamOutShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002642 }
2643 }
2644 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002645 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002646 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002647 ID3D11PixelShader *pixelShader = NULL;
2648
2649 result = mDevice->CreatePixelShader(function, length, NULL, &pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002650 ASSERT(SUCCEEDED(result));
2651
Geoff Lang48dcae72014-02-05 16:28:24 -05002652 if (pixelShader)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002653 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002654 executable = new ShaderExecutable11(function, length, pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002655 }
2656 }
2657 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002658 case rx::SHADER_GEOMETRY:
2659 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002660 ID3D11GeometryShader *geometryShader = NULL;
2661
2662 result = mDevice->CreateGeometryShader(function, length, NULL, &geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002663 ASSERT(SUCCEEDED(result));
2664
Geoff Lang48dcae72014-02-05 16:28:24 -05002665 if (geometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002666 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002667 executable = new ShaderExecutable11(function, length, geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002668 }
2669 }
2670 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002671 default:
2672 UNREACHABLE();
2673 break;
2674 }
2675
2676 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002677}
2678
Geoff Lang48dcae72014-02-05 16:28:24 -05002679ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
2680 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2681 bool separatedOutputBuffers, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002682{
Geoff Lang6e05c272014-03-17 10:46:54 -07002683 const char *profileType = NULL;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002684 switch (type)
2685 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002686 case rx::SHADER_VERTEX:
Geoff Lang6e05c272014-03-17 10:46:54 -07002687 profileType = "vs";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002688 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002689 case rx::SHADER_PIXEL:
Geoff Lang6e05c272014-03-17 10:46:54 -07002690 profileType = "ps";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002691 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002692 case rx::SHADER_GEOMETRY:
Geoff Lang6e05c272014-03-17 10:46:54 -07002693 profileType = "gs";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002694 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002695 default:
2696 UNREACHABLE();
2697 return NULL;
2698 }
2699
Geoff Lang6e05c272014-03-17 10:46:54 -07002700 const char *profileVersion = NULL;
2701 switch (mFeatureLevel)
2702 {
2703 case D3D_FEATURE_LEVEL_11_0:
2704 profileVersion = "5_0";
2705 break;
2706 case D3D_FEATURE_LEVEL_10_1:
2707 profileVersion = "4_1";
2708 break;
2709 case D3D_FEATURE_LEVEL_10_0:
2710 profileVersion = "4_0";
2711 break;
2712 default:
2713 UNREACHABLE();
2714 return NULL;
2715 }
2716
2717 char profile[32];
2718 snprintf(profile, ArraySize(profile), "%s_%s", profileType, profileVersion);
2719
Nicolas Capens93faad92014-05-10 12:14:13 -04002720 UINT flags = D3DCOMPILE_OPTIMIZATION_LEVEL0;
2721
2722 if (gl::perfActive())
2723 {
2724#ifndef NDEBUG
2725 flags = D3DCOMPILE_SKIP_OPTIMIZATION;
2726#endif
2727
2728 flags |= D3DCOMPILE_DEBUG;
2729
2730 std::string sourcePath = getTempPath();
2731 std::string sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(shaderHLSL);
2732 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
2733 }
2734
2735 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
2736 // Try the default flags first and if compilation fails, try some alternatives.
2737 const UINT extraFlags[] =
2738 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002739 flags,
Nicolas Capens2c27db62014-05-10 12:21:11 -04002740 flags | D3DCOMPILE_SKIP_VALIDATION,
2741 flags | D3DCOMPILE_SKIP_OPTIMIZATION
Nicolas Capens93faad92014-05-10 12:14:13 -04002742 };
2743
2744 const static char *extraFlagNames[] =
2745 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002746 "default",
Nicolas Capens2c27db62014-05-10 12:21:11 -04002747 "skip validation",
2748 "skip optimization"
Nicolas Capens93faad92014-05-10 12:14:13 -04002749 };
2750
2751 int attempts = ArraySize(extraFlags);
2752
2753 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, extraFlags, extraFlagNames, attempts);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002754 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002755 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002756 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002757 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002758
Geoff Lang48dcae72014-02-05 16:28:24 -05002759 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type,
2760 transformFeedbackVaryings, separatedOutputBuffers);
Geoff Langea228632013-07-30 15:17:12 -04002761 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002762
2763 return executable;
2764}
2765
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002766rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2767{
2768 return new UniformStorage11(this, storageSize);
2769}
2770
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002771VertexBuffer *Renderer11::createVertexBuffer()
2772{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002773 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002774}
2775
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002776IndexBuffer *Renderer11::createIndexBuffer()
2777{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002778 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002779}
2780
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002781BufferStorage *Renderer11::createBufferStorage()
2782{
2783 return new BufferStorage11(this);
2784}
2785
Brandon Jones5bf98292014-06-06 17:19:38 -07002786VertexArrayImpl *Renderer11::createVertexArray()
2787{
2788 return new VertexArray11(this);
2789}
2790
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002791QueryImpl *Renderer11::createQuery(GLenum type)
2792{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002793 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002794}
2795
2796FenceImpl *Renderer11::createFence()
2797{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002798 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002799}
2800
Geoff Lang005df412013-10-16 14:12:50 -04002801bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002802{
Geoff Lang2b31af22014-05-23 14:45:40 -04002803 ASSERT(getCaps().extensions.pixelBufferObject);
Jamie Madill4461f092013-10-10 15:10:39 -04002804
Jamie Madill4461f092013-10-10 15:10:39 -04002805 // sRGB formats do not work with D3D11 buffer SRVs
Geoff Lange4a492b2014-06-19 14:14:41 -04002806 if (gl::GetColorEncoding(internalFormat) == GL_SRGB)
Jamie Madill4461f092013-10-10 15:10:39 -04002807 {
2808 return false;
2809 }
2810
2811 // We cannot support direct copies to non-color-renderable formats
Geoff Langcec35902014-04-16 10:52:36 -04002812 if (!getCaps().textureCaps.get(internalFormat).colorRendering)
Jamie Madill4461f092013-10-10 15:10:39 -04002813 {
2814 return false;
2815 }
2816
2817 // We skip all 3-channel formats since sometimes format support is missing
Geoff Lange4a492b2014-06-19 14:14:41 -04002818 if (gl::GetComponentCount(internalFormat) == 3)
Jamie Madill4461f092013-10-10 15:10:39 -04002819 {
2820 return false;
2821 }
2822
2823 // We don't support formats which we can't represent without conversion
2824 if (getNativeTextureFormat(internalFormat) != internalFormat)
2825 {
2826 return false;
2827 }
2828
2829 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002830}
2831
Jamie Madilla21eea32013-09-18 14:36:25 -04002832bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2833 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2834{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002835 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002836 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2837}
2838
Jamie Madill3c7fa222014-06-05 13:08:51 -04002839bool Renderer11::getRenderTargetResource(gl::FramebufferAttachment *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002840{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002841 ASSERT(colorbuffer != NULL);
2842
2843 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2844 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002845 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002846 *subresourceIndex = renderTarget->getSubresourceIndex();
2847
2848 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2849 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002850 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002851 ID3D11Resource *textureResource = NULL;
2852 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002853
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002854 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002855 {
Geoff Lang8e328842014-02-10 13:11:20 -05002856 HRESULT result = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002857 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002858
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002859 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002860 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002861 return true;
2862 }
2863 else
2864 {
2865 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2866 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002867 }
2868 }
2869 }
2870 }
2871
2872 return false;
2873}
2874
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002875bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002876 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002877{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002878 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002879 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002880 gl::FramebufferAttachment *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002881
2882 if (!readBuffer)
2883 {
2884 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2885 return gl::error(GL_OUT_OF_MEMORY, false);
2886 }
2887
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002888 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002889
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002890 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002891 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002892 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2893 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002894 gl::FramebufferAttachment *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002895
2896 if (!drawBuffer)
2897 {
2898 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2899 return gl::error(GL_OUT_OF_MEMORY, false);
2900 }
2901
2902 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2903
Geoff Lang125deab2013-08-09 13:34:16 -04002904 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002905 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002906 {
2907 return false;
2908 }
2909 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002910 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002911 }
2912
Geoff Lang685806d2013-06-12 11:16:36 -04002913 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002914 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002915 gl::FramebufferAttachment *readBuffer = readTarget->getDepthOrStencilbuffer();
2916 gl::FramebufferAttachment *drawBuffer = drawTarget->getDepthOrStencilbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002917
2918 if (!readBuffer)
2919 {
2920 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2921 return gl::error(GL_OUT_OF_MEMORY, false);
2922 }
2923
2924 if (!drawBuffer)
2925 {
2926 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2927 return gl::error(GL_OUT_OF_MEMORY, false);
2928 }
2929
2930 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2931 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2932
Geoff Lang125deab2013-08-09 13:34:16 -04002933 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002934 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002935 {
2936 return false;
2937 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002938 }
2939
Geoff Lang42477a42013-09-17 17:07:02 -04002940 invalidateFramebufferSwizzles(drawTarget);
2941
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002942 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002943}
2944
Jamie Madilleb9baab2014-03-24 13:19:43 -04002945void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
2946 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002947{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002948 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002949 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002950
Jamie Madill3c7fa222014-06-05 13:08:51 -04002951 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002952
2953 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002954 {
2955 gl::Rectangle area;
2956 area.x = x;
2957 area.y = y;
2958 area.width = width;
2959 area.height = height;
2960
Jamie Madill1ef6fe72014-05-01 14:51:05 -04002961 if (pack.pixelBuffer.get() != NULL)
2962 {
2963 rx::BufferStorage11 *packBufferStorage = BufferStorage11::makeBufferStorage11(pack.pixelBuffer.get()->getStorage());
2964 PackPixelsParams packParams(area, format, type, outputPitch, pack, reinterpret_cast<ptrdiff_t>(pixels));
2965 packBufferStorage->packPixels(colorBufferTexture, subresourceIndex, packParams);
2966 }
2967 else
2968 {
2969 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch, pack, pixels);
2970 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002971
Geoff Langea228632013-07-30 15:17:12 -04002972 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002973 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002974}
2975
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002976Image *Renderer11::createImage()
2977{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002978 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002979}
2980
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002981void Renderer11::generateMipmap(Image *dest, Image *src)
2982{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002983 Image11 *dest11 = Image11::makeImage11(dest);
2984 Image11 *src11 = Image11::makeImage11(src);
Geoff Lange4a492b2014-06-19 14:14:41 -04002985 Image11::generateMipmap(dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002986}
2987
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002988TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
2989{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002990 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
2991 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002992}
2993
Nicolas Capensbf712d02014-03-31 14:23:35 -04002994TextureStorage *Renderer11::createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002995{
Nicolas Capensbf712d02014-03-31 14:23:35 -04002996 return new TextureStorage11_2D(this, internalformat, renderTarget, width, height, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002997}
2998
Nicolas Capensbf712d02014-03-31 14:23:35 -04002999TextureStorage *Renderer11::createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003000{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003001 return new TextureStorage11_Cube(this, internalformat, renderTarget, size, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003002}
3003
Nicolas Capensbf712d02014-03-31 14:23:35 -04003004TextureStorage *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 +00003005{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003006 return new TextureStorage11_3D(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003007}
3008
Nicolas Capensbf712d02014-03-31 14:23:35 -04003009TextureStorage *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 +00003010{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003011 return new TextureStorage11_2DArray(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003012}
3013
Jamie Madilleb9baab2014-03-24 13:19:43 -04003014void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format,
3015 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void *pixels)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003016{
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003017 ASSERT(area.width >= 0);
3018 ASSERT(area.height >= 0);
3019
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003020 D3D11_TEXTURE2D_DESC textureDesc;
3021 texture->GetDesc(&textureDesc);
3022
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003023 // Clamp read region to the defined texture boundaries, preventing out of bounds reads
3024 // and reads of uninitialized data.
3025 gl::Rectangle safeArea;
3026 safeArea.x = gl::clamp(area.x, 0, static_cast<int>(textureDesc.Width));
3027 safeArea.y = gl::clamp(area.y, 0, static_cast<int>(textureDesc.Height));
3028 safeArea.width = gl::clamp(area.width + std::min(area.x, 0), 0,
3029 static_cast<int>(textureDesc.Width) - safeArea.x);
3030 safeArea.height = gl::clamp(area.height + std::min(area.y, 0), 0,
3031 static_cast<int>(textureDesc.Height) - safeArea.y);
3032
3033 ASSERT(safeArea.x >= 0 && safeArea.y >= 0);
3034 ASSERT(safeArea.x + safeArea.width <= static_cast<int>(textureDesc.Width));
3035 ASSERT(safeArea.y + safeArea.height <= static_cast<int>(textureDesc.Height));
3036
3037 if (safeArea.width == 0 || safeArea.height == 0)
3038 {
3039 // no work to do
3040 return;
3041 }
3042
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003043 D3D11_TEXTURE2D_DESC stagingDesc;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003044 stagingDesc.Width = safeArea.width;
3045 stagingDesc.Height = safeArea.height;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003046 stagingDesc.MipLevels = 1;
3047 stagingDesc.ArraySize = 1;
3048 stagingDesc.Format = textureDesc.Format;
3049 stagingDesc.SampleDesc.Count = 1;
3050 stagingDesc.SampleDesc.Quality = 0;
3051 stagingDesc.Usage = D3D11_USAGE_STAGING;
3052 stagingDesc.BindFlags = 0;
3053 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3054 stagingDesc.MiscFlags = 0;
3055
3056 ID3D11Texture2D* stagingTex = NULL;
3057 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3058 if (FAILED(result))
3059 {
3060 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3061 return;
3062 }
3063
3064 ID3D11Texture2D* srcTex = NULL;
3065 if (textureDesc.SampleDesc.Count > 1)
3066 {
3067 D3D11_TEXTURE2D_DESC resolveDesc;
3068 resolveDesc.Width = textureDesc.Width;
3069 resolveDesc.Height = textureDesc.Height;
3070 resolveDesc.MipLevels = 1;
3071 resolveDesc.ArraySize = 1;
3072 resolveDesc.Format = textureDesc.Format;
3073 resolveDesc.SampleDesc.Count = 1;
3074 resolveDesc.SampleDesc.Quality = 0;
3075 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3076 resolveDesc.BindFlags = 0;
3077 resolveDesc.CPUAccessFlags = 0;
3078 resolveDesc.MiscFlags = 0;
3079
3080 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3081 if (FAILED(result))
3082 {
3083 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003084 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003085 return;
3086 }
3087
3088 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3089 subResource = 0;
3090 }
3091 else
3092 {
3093 srcTex = texture;
3094 srcTex->AddRef();
3095 }
3096
3097 D3D11_BOX srcBox;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003098 srcBox.left = static_cast<UINT>(safeArea.x);
3099 srcBox.right = static_cast<UINT>(safeArea.x + safeArea.width);
3100 srcBox.top = static_cast<UINT>(safeArea.y);
3101 srcBox.bottom = static_cast<UINT>(safeArea.y + safeArea.height);
3102 srcBox.front = 0;
3103 srcBox.back = 1;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003104
3105 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3106
Geoff Langea228632013-07-30 15:17:12 -04003107 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003108
Jamie Madill7538f7f2014-04-17 11:53:39 -04003109 PackPixelsParams packParams(safeArea, format, type, outputPitch, pack, 0);
3110 packPixels(stagingTex, packParams, pixels);
3111
3112 SafeRelease(stagingTex);
3113}
3114
3115void Renderer11::packPixels(ID3D11Texture2D *readTexture, const PackPixelsParams &params, void *pixelsOut)
3116{
3117 D3D11_TEXTURE2D_DESC textureDesc;
3118 readTexture->GetDesc(&textureDesc);
3119
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003120 D3D11_MAPPED_SUBRESOURCE mapping;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003121 HRESULT hr = mDeviceContext->Map(readTexture, 0, D3D11_MAP_READ, 0, &mapping);
Geoff Lang9cd19152014-05-28 15:54:34 -04003122 UNUSED_ASSERTION_VARIABLE(hr);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003123 ASSERT(SUCCEEDED(hr));
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003124
3125 unsigned char *source;
3126 int inputPitch;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003127 if (params.pack.reverseRowOrder)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003128 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003129 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (params.area.height - 1);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003130 inputPitch = -static_cast<int>(mapping.RowPitch);
3131 }
3132 else
3133 {
3134 source = static_cast<unsigned char*>(mapping.pData);
3135 inputPitch = static_cast<int>(mapping.RowPitch);
3136 }
3137
Geoff Lange4a492b2014-06-19 14:14:41 -04003138 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format);
3139 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat);
3140 GLenum sourceType = gl::GetType(sourceInternalFormat);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003141
Geoff Lange4a492b2014-06-19 14:14:41 -04003142 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003143
Jamie Madill7538f7f2014-04-17 11:53:39 -04003144 if (sourceFormat == params.format && sourceType == params.type)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003145 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003146 unsigned char *dest = static_cast<unsigned char*>(pixelsOut) + params.offset;
3147 for (int y = 0; y < params.area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003148 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003149 memcpy(dest + y * params.outputPitch, source + y * inputPitch, params.area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003150 }
3151 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003152 else
3153 {
Geoff Lange4a492b2014-06-19 14:14:41 -04003154 GLenum destInternalFormat = gl::GetSizedInternalFormat(params.format, params.type);
3155 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003156
Jamie Madill7538f7f2014-04-17 11:53:39 -04003157 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, params.format, params.type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003158 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003159 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003160 // Fast copy is possible through some special function
Jamie Madill7538f7f2014-04-17 11:53:39 -04003161 for (int y = 0; y < params.area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003162 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003163 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003164 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003165 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003166 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3167
3168 fastCopyFunc(src, dest);
3169 }
3170 }
3171 }
3172 else
3173 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003174 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Geoff Lange4a492b2014-06-19 14:14:41 -04003175 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(params.format, params.type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003176
3177 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3178 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3179 sizeof(temp) >= sizeof(gl::ColorUI) &&
3180 sizeof(temp) >= sizeof(gl::ColorI));
3181
Jamie Madill7538f7f2014-04-17 11:53:39 -04003182 for (int y = 0; y < params.area.height; y++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003183 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003184 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003185 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003186 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003187 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3188
3189 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3190 // will not allow the copy otherwise.
3191 readFunc(src, temp);
3192 writeFunc(temp, dest);
3193 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003194 }
3195 }
3196 }
3197
Jamie Madill7538f7f2014-04-17 11:53:39 -04003198 mDeviceContext->Unmap(readTexture, 0);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003199}
3200
Geoff Lang758d5b22013-06-11 11:42:50 -04003201bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003202 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3203 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003204{
Geoff Lang975af372013-06-12 11:19:22 -04003205 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3206 // it should never be the case that both color and depth/stencil need to be blitted at
3207 // at the same time.
3208 ASSERT(colorBlit != (depthBlit || stencilBlit));
3209
Geoff Langc1f51be2013-06-11 11:49:14 -04003210 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003211
Geoff Lang4d782732013-07-22 10:44:18 -04003212 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3213 if (!drawRenderTarget)
3214 {
3215 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3216 return gl::error(GL_OUT_OF_MEMORY, false);
3217 }
3218
3219 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3220 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3221 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3222 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3223
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003224 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3225 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003226 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003227 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003228 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003229 }
3230
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003231 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003232 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003233 unsigned int readSubresource = 0;
3234 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003235 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003236 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3237 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003238
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003239 if (unresolvedTexture)
3240 {
3241 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3242 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003243
Geoff Langea228632013-07-30 15:17:12 -04003244 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003245
Geoff Lang5c9a29a2014-02-11 10:26:24 -05003246 HRESULT hresult = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3247 if (FAILED(hresult))
Geoff Langc1f51be2013-06-11 11:49:14 -04003248 {
Geoff Langea228632013-07-30 15:17:12 -04003249 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003250 return gl::error(GL_OUT_OF_MEMORY, false);
3251 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003252 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003253 }
3254 else
3255 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003256 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003257 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003258 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003259 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003260 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003261 }
3262
Geoff Lang4d782732013-07-22 10:44:18 -04003263 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003264 {
Geoff Lang4d782732013-07-22 10:44:18 -04003265 SafeRelease(readTexture);
3266 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003267 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003268 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003269 }
3270
Geoff Lang125deab2013-08-09 13:34:16 -04003271 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3272 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3273
3274 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3275
3276 bool wholeBufferCopy = !scissorNeeded &&
3277 readRect.x == 0 && readRect.width == readSize.width &&
3278 readRect.y == 0 && readRect.height == readSize.height &&
3279 drawRect.x == 0 && drawRect.width == drawSize.width &&
3280 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003281
Geoff Langc1f51be2013-06-11 11:49:14 -04003282 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003283
Geoff Lang1cd1b212014-02-11 09:42:27 -05003284 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003285
3286 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3287 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3288 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3289 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3290
Geoff Lange4a492b2014-06-19 14:14:41 -04003291 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat()) > 0;
3292 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat()) > 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003293 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3294
Geoff Langc1f51be2013-06-11 11:49:14 -04003295 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003296 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3297 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003298 {
Geoff Lang125deab2013-08-09 13:34:16 -04003299 UINT dstX = drawRect.x;
3300 UINT dstY = drawRect.y;
3301
Geoff Langc1f51be2013-06-11 11:49:14 -04003302 D3D11_BOX readBox;
3303 readBox.left = readRect.x;
3304 readBox.right = readRect.x + readRect.width;
3305 readBox.top = readRect.y;
3306 readBox.bottom = readRect.y + readRect.height;
3307 readBox.front = 0;
3308 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003309
Geoff Lang125deab2013-08-09 13:34:16 -04003310 if (scissorNeeded)
3311 {
3312 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3313 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3314
3315 if (drawRect.x < scissor->x)
3316 {
3317 dstX = scissor->x;
3318 readBox.left += (scissor->x - drawRect.x);
3319 }
3320 if (drawRect.y < scissor->y)
3321 {
3322 dstY = scissor->y;
3323 readBox.top += (scissor->y - drawRect.y);
3324 }
3325 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3326 {
3327 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3328 }
3329 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3330 {
3331 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3332 }
3333 }
3334
Geoff Langc1f51be2013-06-11 11:49:14 -04003335 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3336 // We also require complete framebuffer copies for depth-stencil blit.
3337 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003338
Geoff Lang125deab2013-08-09 13:34:16 -04003339 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003340 readTexture, readSubresource, pSrcBox);
3341 result = true;
3342 }
3343 else
3344 {
3345 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003346 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003347
Geoff Lang975af372013-06-12 11:19:22 -04003348 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003349 {
Geoff Lang975af372013-06-12 11:19:22 -04003350 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003351 drawTexture, drawSubresource, drawArea, drawSize,
3352 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003353 }
3354 else if (depthBlit)
3355 {
Geoff Lang125deab2013-08-09 13:34:16 -04003356 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3357 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003358 }
3359 else if (stencilBlit)
3360 {
3361 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003362 drawTexture, drawSubresource, drawArea, drawSize,
3363 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003364 }
3365 else
3366 {
Geoff Lange4a492b2014-06-19 14:14:41 -04003367 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat());
Geoff Lang125deab2013-08-09 13:34:16 -04003368 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3369 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003370 }
3371 }
3372
3373 SafeRelease(readTexture);
3374 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003375
3376 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003377}
3378
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003379ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3380{
3381 D3D11_TEXTURE2D_DESC textureDesc;
3382 source->GetDesc(&textureDesc);
3383
3384 if (textureDesc.SampleDesc.Count > 1)
3385 {
3386 D3D11_TEXTURE2D_DESC resolveDesc;
3387 resolveDesc.Width = textureDesc.Width;
3388 resolveDesc.Height = textureDesc.Height;
3389 resolveDesc.MipLevels = 1;
3390 resolveDesc.ArraySize = 1;
3391 resolveDesc.Format = textureDesc.Format;
3392 resolveDesc.SampleDesc.Count = 1;
3393 resolveDesc.SampleDesc.Quality = 0;
3394 resolveDesc.Usage = textureDesc.Usage;
3395 resolveDesc.BindFlags = textureDesc.BindFlags;
3396 resolveDesc.CPUAccessFlags = 0;
3397 resolveDesc.MiscFlags = 0;
3398
3399 ID3D11Texture2D *resolveTexture = NULL;
3400 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3401 if (FAILED(result))
3402 {
3403 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3404 return NULL;
3405 }
3406
3407 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3408 return resolveTexture;
3409 }
3410 else
3411 {
3412 source->AddRef();
3413 return source;
3414 }
3415}
3416
Jamie Madill3c7fa222014-06-05 13:08:51 -04003417void Renderer11::invalidateFBOAttachmentSwizzles(gl::FramebufferAttachment *attachment, int mipLevel)
Geoff Lang42477a42013-09-17 17:07:02 -04003418{
Jamie Madill3c7fa222014-06-05 13:08:51 -04003419 ASSERT(attachment->isTexture());
3420 TextureStorage *texStorage = attachment->getTextureStorage();
Geoff Lang42477a42013-09-17 17:07:02 -04003421 if (texStorage)
3422 {
3423 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3424 if (!texStorage11)
3425 {
3426 ERR("texture storage pointer unexpectedly null.");
3427 return;
3428 }
3429
3430 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3431 }
3432}
3433
3434void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3435{
3436 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3437 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003438 gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(colorAttachment);
3439 if (attachment && attachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003440 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003441 invalidateFBOAttachmentSwizzles(attachment, framebuffer->getColorbufferMipLevel(colorAttachment));
Geoff Lang42477a42013-09-17 17:07:02 -04003442 }
3443 }
3444
Jamie Madill3c7fa222014-06-05 13:08:51 -04003445 gl::FramebufferAttachment *depthAttachment = framebuffer->getDepthbuffer();
3446 if (depthAttachment && depthAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003447 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003448 invalidateFBOAttachmentSwizzles(depthAttachment, framebuffer->getDepthbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003449 }
3450
Jamie Madill3c7fa222014-06-05 13:08:51 -04003451 gl::FramebufferAttachment *stencilAttachment = framebuffer->getStencilbuffer();
3452 if (stencilAttachment && stencilAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003453 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003454 invalidateFBOAttachmentSwizzles(stencilAttachment, framebuffer->getStencilbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003455 }
3456}
3457
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003458bool Renderer11::getLUID(LUID *adapterLuid) const
3459{
3460 adapterLuid->HighPart = 0;
3461 adapterLuid->LowPart = 0;
3462
3463 if (!mDxgiAdapter)
3464 {
3465 return false;
3466 }
3467
3468 DXGI_ADAPTER_DESC adapterDesc;
3469 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3470 {
3471 return false;
3472 }
3473
3474 *adapterLuid = adapterDesc.AdapterLuid;
3475 return true;
3476}
3477
Geoff Lang005df412013-10-16 14:12:50 -04003478GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003479{
Geoff Lange4a492b2014-06-19 14:14:41 -04003480 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat));
Jamie Madillc8c102b2013-10-10 15:10:24 -04003481}
3482
Jamie Madill95ffb862014-01-29 09:26:59 -05003483rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3484{
3485 return gl_d3d11::GetVertexConversionType(vertexFormat);
3486}
3487
3488GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3489{
3490 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3491}
3492
Geoff Lang61e49a52013-05-29 10:22:58 -04003493Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3494{
3495 MultisampleSupportInfo supportInfo = { 0 };
3496
3497 UINT formatSupport;
3498 HRESULT result;
3499
3500 result = mDevice->CheckFormatSupport(format, &formatSupport);
3501 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3502 {
3503 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3504 {
3505 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3506 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3507 {
3508 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3509 }
3510 else
3511 {
3512 supportInfo.qualityLevels[i - 1] = 0;
3513 }
3514 }
3515 }
3516
3517 return supportInfo;
3518}
3519
Geoff Langcec35902014-04-16 10:52:36 -04003520gl::Caps Renderer11::generateCaps() const
3521{
3522 return d3d11_gl::GenerateCaps(mDevice);
3523}
3524
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003525}