blob: 7d1ac2622f92f9d3f8762a87af4e8d9797283089 [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002//
Geoff Langeeba6e12014-02-03 13:12:30 -05003// Copyright (c) 2012-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// Renderer11.cpp: Implements a back-end specific class for the D3D11 renderer.
9
daniel@transgaming.com5503fd02012-11-28 19:38:57 +000010#include "libGLESv2/main.h"
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +000011#include "common/utilities.h"
daniel@transgaming.com18adad02012-11-28 21:04:03 +000012#include "libGLESv2/Buffer.h"
daniel@transgaming.com53670042012-11-28 20:55:51 +000013#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com80fc3322012-11-28 21:02:13 +000014#include "libGLESv2/Framebuffer.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000015#include "libGLESv2/RenderBuffer.h"
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{
Jamie Madill1e3fa742014-06-16 10:34:00 -0400295 mStateCache.initialize(this);
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 {
Jamie Madilld6cb2442013-07-10 15:13:38 -0400345 // FIXME: parse types from context version
346 ASSERT(d3d11_gl::GetInternalFormat(renderTargetFormat, 2) == d3d11_gl::GetInternalFormat(renderTargetFormat, 3));
347 ASSERT(d3d11_gl::GetInternalFormat(depthStencilFormat, 2) == d3d11_gl::GetInternalFormat(depthStencilFormat, 3));
348
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000349 ConfigDesc newConfig;
Jamie Madilld6cb2442013-07-10 15:13:38 -0400350 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat, getCurrentClientVersion());
351 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat, getCurrentClientVersion());
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000352 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
353 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000354 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000355
356 (*configDescList)[numConfigs++] = newConfig;
357 }
358 }
359 }
360 }
361
362 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000363}
364
365void Renderer11::deleteConfigs(ConfigDesc *configDescList)
366{
367 delete [] (configDescList);
368}
369
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000370void Renderer11::sync(bool block)
371{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000372 if (block)
373 {
374 HRESULT result;
375
376 if (!mSyncQuery)
377 {
378 D3D11_QUERY_DESC queryDesc;
379 queryDesc.Query = D3D11_QUERY_EVENT;
380 queryDesc.MiscFlags = 0;
381
382 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
383 ASSERT(SUCCEEDED(result));
384 }
385
386 mDeviceContext->End(mSyncQuery);
387 mDeviceContext->Flush();
388
389 do
390 {
391 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
392
393 // Keep polling, but allow other threads to do something useful first
394 Sleep(0);
395
396 if (testDeviceLost(true))
397 {
398 return;
399 }
400 }
401 while (result == S_FALSE);
402 }
403 else
404 {
405 mDeviceContext->Flush();
406 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000407}
408
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000409SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
410{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000411 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000412}
413
Geoff Lange2e0ce02013-09-17 17:05:08 -0400414void Renderer11::generateSwizzle(gl::Texture *texture)
415{
Geoff Lang42477a42013-09-17 17:07:02 -0400416 if (texture)
417 {
418 TextureStorageInterface *texStorage = texture->getNativeTexture();
419 if (texStorage)
420 {
421 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
422
423 storage11->generateSwizzles(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
424 texture->getSwizzleAlpha());
425 }
426 }
Geoff Lange2e0ce02013-09-17 17:05:08 -0400427}
428
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000429void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
430{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000431 if (type == gl::SAMPLER_PIXEL)
432 {
433 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
434 {
435 ERR("Pixel shader sampler index %i is not valid.", index);
436 return;
437 }
438
439 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
440 {
441 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
442
443 if (!dxSamplerState)
444 {
445 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
446 "sampler state for pixel shaders at slot %i.", index);
447 }
448
449 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
450
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000451 mCurPixelSamplerStates[index] = samplerState;
452 }
453
454 mForceSetPixelSamplerStates[index] = false;
455 }
456 else if (type == gl::SAMPLER_VERTEX)
457 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000458 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000459 {
460 ERR("Vertex shader sampler index %i is not valid.", index);
461 return;
462 }
463
464 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
465 {
466 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
467
468 if (!dxSamplerState)
469 {
470 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
471 "sampler state for vertex shaders at slot %i.", index);
472 }
473
474 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
475
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000476 mCurVertexSamplerStates[index] = samplerState;
477 }
478
479 mForceSetVertexSamplerStates[index] = false;
480 }
481 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000482}
483
484void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
485{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000486 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000487 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000488
489 if (texture)
490 {
491 TextureStorageInterface *texStorage = texture->getNativeTexture();
492 if (texStorage)
493 {
494 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
Nicolas Capensa10c1c72014-03-27 11:20:52 -0400495 gl::SamplerState samplerState;
496 texture->getSamplerState(&samplerState);
497 textureSRV = storage11->getSRV(samplerState);
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000498 }
499
500 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
501 // missing the shader resource view
502 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000503
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000504 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000505 }
506
507 if (type == gl::SAMPLER_PIXEL)
508 {
509 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
510 {
511 ERR("Pixel shader sampler index %i is not valid.", index);
512 return;
513 }
514
Geoff Lang91382e52014-01-07 16:16:30 -0500515 if (forceSetTexture || mCurPixelSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000516 {
517 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
518 }
519
Geoff Lang91382e52014-01-07 16:16:30 -0500520 mCurPixelSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000521 }
522 else if (type == gl::SAMPLER_VERTEX)
523 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000524 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000525 {
526 ERR("Vertex shader sampler index %i is not valid.", index);
527 return;
528 }
529
Geoff Lang91382e52014-01-07 16:16:30 -0500530 if (forceSetTexture || mCurVertexSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000531 {
532 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
533 }
534
Geoff Lang91382e52014-01-07 16:16:30 -0500535 mCurVertexSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000536 }
537 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000538}
539
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000540bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
541{
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000542 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
543 {
544 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
545 if (uniformBuffer)
546 {
547 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500548 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000549
550 if (!constantBuffer)
551 {
552 return false;
553 }
554
Geoff Langc6354ee2013-07-22 10:40:07 -0400555 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
556 {
557 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
558 1, &constantBuffer);
559 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
560 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000561 }
562 }
563
564 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
565 {
566 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
567 if (uniformBuffer)
568 {
569 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500570 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000571
572 if (!constantBuffer)
573 {
574 return false;
575 }
576
Geoff Langc6354ee2013-07-22 10:40:07 -0400577 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
578 {
579 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
580 1, &constantBuffer);
581 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
582 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000583 }
584 }
585
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000586 return true;
587}
588
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000589void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000590{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000591 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000592 {
Nicolas Capensaea8e942014-05-09 19:14:08 -0400593 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000594 if (!dxRasterState)
595 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000596 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000597 "rasterizer state.");
598 }
599
600 mDeviceContext->RSSetState(dxRasterState);
601
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000602 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000603 }
604
605 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000606}
607
Geoff Langc142e9d2013-09-30 15:19:47 -0400608void Renderer11::setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000609 unsigned int sampleMask)
610{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000611 if (mForceSetBlendState ||
612 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400613 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000614 sampleMask != mCurSampleMask)
615 {
Geoff Langc142e9d2013-09-30 15:19:47 -0400616 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(framebuffer, blendState);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000617 if (!dxBlendState)
618 {
619 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
620 "blend state.");
621 }
622
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000623 float blendColors[4] = {0.0f};
624 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
625 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
626 {
627 blendColors[0] = blendColor.red;
628 blendColors[1] = blendColor.green;
629 blendColors[2] = blendColor.blue;
630 blendColors[3] = blendColor.alpha;
631 }
632 else
633 {
634 blendColors[0] = blendColor.alpha;
635 blendColors[1] = blendColor.alpha;
636 blendColors[2] = blendColor.alpha;
637 blendColors[3] = blendColor.alpha;
638 }
639
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000640 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
641
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000642 mCurBlendState = blendState;
643 mCurBlendColor = blendColor;
644 mCurSampleMask = sampleMask;
645 }
646
647 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000648}
649
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000650void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000651 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000652{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000653 if (mForceSetDepthStencilState ||
654 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
655 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
656 {
Jamie Madillac528012014-06-20 13:21:23 -0400657 ASSERT(depthStencilState.stencilWritemask == depthStencilState.stencilBackWritemask);
658 ASSERT(stencilRef == stencilBackRef);
659 ASSERT(depthStencilState.stencilMask == depthStencilState.stencilBackMask);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000660
661 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
662 if (!dxDepthStencilState)
663 {
664 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
665 "setting the default depth stencil state.");
666 }
667
Jamie Madillec91cd32014-01-21 16:38:12 -0500668 // Max D3D11 stencil reference value is 0xFF, corresponding to the max 8 bits in a stencil buffer
669 // GL specifies we should clamp the ref value to the nearest bit depth when doing stencil ops
670 META_ASSERT(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF);
671 META_ASSERT(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF);
Geoff Lang0bf3a982014-02-11 09:40:48 -0500672 UINT dxStencilRef = std::min<UINT>(stencilRef, 0xFFu);
Jamie Madillec91cd32014-01-21 16:38:12 -0500673
Geoff Lang0bf3a982014-02-11 09:40:48 -0500674 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, dxStencilRef);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000675
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000676 mCurDepthStencilState = depthStencilState;
677 mCurStencilRef = stencilRef;
678 mCurStencilBackRef = stencilBackRef;
679 }
680
681 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000682}
683
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000684void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000685{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000686 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
687 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000688 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000689 if (enabled)
690 {
691 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000692 rect.left = std::max(0, scissor.x);
693 rect.top = std::max(0, scissor.y);
694 rect.right = scissor.x + std::max(0, scissor.width);
695 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000696
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000697 mDeviceContext->RSSetScissorRects(1, &rect);
698 }
699
700 if (enabled != mScissorEnabled)
701 {
702 mForceSetRasterState = true;
703 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000704
705 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000706 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000707 }
708
709 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000710}
711
daniel@transgaming.com12985182012-12-20 20:56:31 +0000712bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000713 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000714{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000715 gl::Rectangle actualViewport = viewport;
716 float actualZNear = gl::clamp01(zNear);
717 float actualZFar = gl::clamp01(zFar);
718 if (ignoreViewport)
719 {
720 actualViewport.x = 0;
721 actualViewport.y = 0;
722 actualViewport.width = mRenderTargetDesc.width;
723 actualViewport.height = mRenderTargetDesc.height;
724 actualZNear = 0.0f;
725 actualZFar = 1.0f;
726 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000727
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000728 // Get D3D viewport bounds, which depends on the feature level
729 const Range& viewportBounds = getViewportBounds();
730
731 // 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 +0000732 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000733 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
734 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
735 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
736 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
737 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
738 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000739 dxViewport.MinDepth = actualZNear;
740 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000741
742 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
743 {
744 return false; // Nothing to render
745 }
746
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000747 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
748 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000749
daniel@transgaming.com53670042012-11-28 20:55:51 +0000750 if (viewportChanged)
751 {
752 mDeviceContext->RSSetViewports(1, &dxViewport);
753
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000754 mCurViewport = actualViewport;
755 mCurNear = actualZNear;
756 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000757
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000758 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
759 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
760 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
761 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000762
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000763 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
764 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000765
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000766 mVertexConstants.depthRange[0] = actualZNear;
767 mVertexConstants.depthRange[1] = actualZFar;
768 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000769
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000770 mPixelConstants.depthRange[0] = actualZNear;
771 mPixelConstants.depthRange[1] = actualZFar;
772 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000773 }
774
775 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000776 return true;
777}
778
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000779bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
780{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000781 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000782
Geoff Lang57e713e2013-07-31 17:01:58 -0400783 GLsizei minCount = 0;
784
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000785 switch (mode)
786 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400787 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
788 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
789 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
790 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
791 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
792 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000793 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400794 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000795 default:
Jamie Madill1aeb1312014-06-20 13:21:25 -0400796 UNREACHABLE();
797 return false;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000798 }
799
Geoff Lang4c095862013-07-22 10:43:36 -0400800 if (primitiveTopology != mCurrentPrimitiveTopology)
801 {
802 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
803 mCurrentPrimitiveTopology = primitiveTopology;
804 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000805
Geoff Lang57e713e2013-07-31 17:01:58 -0400806 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000807}
808
809bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000810{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000811 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000812 // Also extract the render target dimensions and view
813 unsigned int renderTargetWidth = 0;
814 unsigned int renderTargetHeight = 0;
815 GLenum renderTargetFormat = 0;
816 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
817 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
818 bool missingColorRenderTarget = true;
819
820 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000821 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000822 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
823
824 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000825 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000826 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
827 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
828
Jamie Madill3c7fa222014-06-05 13:08:51 -0400829 gl::FramebufferAttachment *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000830
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000831 if (!colorbuffer)
832 {
833 ERR("render target pointer unexpectedly null.");
834 return false;
835 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000836
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000837 // check for zero-sized default framebuffer, which is a special case.
838 // in this case we do not wish to modify any state and just silently return false.
839 // this will not report any gl error but will cause the calling method to return.
840 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
841 {
842 return false;
843 }
844
845 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
846
847 // Extract the render target dimensions and view
848 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
849 if (!renderTarget)
850 {
851 ERR("render target pointer unexpectedly null.");
852 return false;
853 }
854
855 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
856 if (!framebufferRTVs[colorAttachment])
857 {
858 ERR("render target view pointer unexpectedly null.");
859 return false;
860 }
861
862 if (missingColorRenderTarget)
863 {
864 renderTargetWidth = colorbuffer->getWidth();
865 renderTargetHeight = colorbuffer->getHeight();
866 renderTargetFormat = colorbuffer->getActualFormat();
867 missingColorRenderTarget = false;
868 }
Jamie Madillba597af2013-10-22 13:12:15 -0400869
Geoff Lang91382e52014-01-07 16:16:30 -0500870 // TODO: Detect if this color buffer is already bound as a texture and unbind it first to prevent
871 // D3D11 warnings.
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000872 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000873 }
874
875 // Get the depth stencil render buffer and serials
Jamie Madill3c7fa222014-06-05 13:08:51 -0400876 gl::FramebufferAttachment *depthStencil = NULL;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000877 unsigned int depthbufferSerial = 0;
878 unsigned int stencilbufferSerial = 0;
879 if (framebuffer->getDepthbufferType() != GL_NONE)
880 {
881 depthStencil = framebuffer->getDepthbuffer();
882 if (!depthStencil)
883 {
884 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000885 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000886 return false;
887 }
888
889 depthbufferSerial = depthStencil->getSerial();
890 }
891 else if (framebuffer->getStencilbufferType() != GL_NONE)
892 {
893 depthStencil = framebuffer->getStencilbuffer();
894 if (!depthStencil)
895 {
896 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000897 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000898 return false;
899 }
900
901 stencilbufferSerial = depthStencil->getSerial();
902 }
903
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000904 ID3D11DepthStencilView* framebufferDSV = NULL;
905 if (depthStencil)
906 {
907 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
908 if (!depthStencilRenderTarget)
909 {
910 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000911 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000912 return false;
913 }
914
915 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
916 if (!framebufferDSV)
917 {
918 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000919 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000920 return false;
921 }
922
923 // If there is no render buffer, the width, height and format values come from
924 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000925 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000926 {
927 renderTargetWidth = depthStencil->getWidth();
928 renderTargetHeight = depthStencil->getHeight();
929 renderTargetFormat = depthStencil->getActualFormat();
930 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000931 }
932
933 // Apply the render target and depth stencil
934 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000935 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000936 depthbufferSerial != mAppliedDepthbufferSerial ||
937 stencilbufferSerial != mAppliedStencilbufferSerial)
938 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000939 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000940
941 mRenderTargetDesc.width = renderTargetWidth;
942 mRenderTargetDesc.height = renderTargetHeight;
943 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000944 mForceSetViewport = true;
945 mForceSetScissor = true;
Geoff Langc142e9d2013-09-30 15:19:47 -0400946 mForceSetBlendState = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000947
Nicolas Capensaea8e942014-05-09 19:14:08 -0400948 if (!mDepthStencilInitialized)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000949 {
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000950 mForceSetRasterState = true;
951 }
952
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000953 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
954 {
955 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
956 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000957 mAppliedDepthbufferSerial = depthbufferSerial;
958 mAppliedStencilbufferSerial = stencilbufferSerial;
959 mRenderTargetDescInitialized = true;
960 mDepthStencilInitialized = true;
961 }
962
Geoff Lang42477a42013-09-17 17:07:02 -0400963 invalidateFramebufferSwizzles(framebuffer);
964
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000965 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000966}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000967
Jamie Madill57a89722013-07-02 11:57:03 -0400968GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -0400969 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000970{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000971 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -0400972 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000973 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000974 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000975 return err;
976 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000977
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000978 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000979}
980
daniel@transgaming.com31240482012-11-28 21:06:41 +0000981GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000982{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000983 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000984
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000985 if (err == GL_NO_ERROR)
986 {
Geoff Lang7840b172014-03-13 11:20:44 -0400987 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
988
989 ID3D11Buffer *buffer = NULL;
990 DXGI_FORMAT bufferFormat = indexBuffer->getIndexFormat();
991
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000992 if (indexInfo->storage)
993 {
Geoff Lang7840b172014-03-13 11:20:44 -0400994 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
995 buffer = storage->getBuffer(BUFFER_USAGE_INDEX);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000996 }
Geoff Lang7840b172014-03-13 11:20:44 -0400997 else
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000998 {
Geoff Lang7840b172014-03-13 11:20:44 -0400999 buffer = indexBuffer->getBuffer();
1000 }
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001001
Geoff Lang7840b172014-03-13 11:20:44 -04001002 if (buffer != mAppliedIB || bufferFormat != mAppliedIBFormat || indexInfo->startOffset != mAppliedIBOffset)
1003 {
1004 mDeviceContext->IASetIndexBuffer(buffer, bufferFormat, indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001005
Geoff Lang7840b172014-03-13 11:20:44 -04001006 mAppliedIB = buffer;
1007 mAppliedIBFormat = bufferFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001008 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001009 }
1010 }
1011
1012 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001013}
1014
Geoff Langeeba6e12014-02-03 13:12:30 -05001015void Renderer11::applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[])
1016{
1017 ID3D11Buffer* d3dBuffers[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1018 UINT d3dOffsets[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1019 bool requiresUpdate = false;
1020 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1021 {
1022 if (transformFeedbackBuffers[i])
1023 {
1024 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(transformFeedbackBuffers[i]->getStorage());
1025 ID3D11Buffer *buffer = storage->getBuffer(BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK);
1026
1027 d3dBuffers[i] = buffer;
1028 d3dOffsets[i] = (mAppliedTFBuffers[i] != buffer) ? static_cast<UINT>(offsets[i]) : -1;
1029 }
1030 else
1031 {
1032 d3dBuffers[i] = NULL;
1033 d3dOffsets[i] = 0;
1034 }
1035
1036 if (d3dBuffers[i] != mAppliedTFBuffers[i] || offsets[i] != mAppliedTFOffsets[i])
1037 {
1038 requiresUpdate = true;
1039 }
1040 }
1041
1042 if (requiresUpdate)
1043 {
1044 mDeviceContext->SOSetTargets(ArraySize(d3dBuffers), d3dBuffers, d3dOffsets);
1045 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1046 {
1047 mAppliedTFBuffers[i] = d3dBuffers[i];
1048 mAppliedTFOffsets[i] = offsets[i];
1049 }
1050 }
1051}
1052
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001053void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001054{
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001055 if (mode == GL_POINTS && transformFeedbackActive)
1056 {
1057 // Since point sprites are generated with a geometry shader, too many vertices will
1058 // be written if transform feedback is active. To work around this, draw only the points
1059 // with the stream out shader and no pixel shader to feed the stream out buffers and then
1060 // draw again with the point sprite geometry shader to rasterize the point sprites.
1061
1062 mDeviceContext->PSSetShader(NULL, NULL, 0);
1063
1064 if (instances > 0)
1065 {
1066 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1067 }
1068 else
1069 {
1070 mDeviceContext->Draw(count, 0);
1071 }
1072
1073 mDeviceContext->GSSetShader(mCurPointGeometryShader, NULL, 0);
1074 mDeviceContext->PSSetShader(mAppliedPixelShader, NULL, 0);
1075
1076 if (instances > 0)
1077 {
1078 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1079 }
1080 else
1081 {
1082 mDeviceContext->Draw(count, 0);
1083 }
1084
1085 mDeviceContext->GSSetShader(mAppliedGeometryShader, NULL, 0);
1086 }
1087 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001088 {
1089 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1090 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001091 else if (mode == GL_TRIANGLE_FAN)
1092 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001093 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001094 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001095 else if (instances > 0)
1096 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001097 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001098 }
1099 else
1100 {
1101 mDeviceContext->Draw(count, 0);
1102 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001103}
1104
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001105void Renderer11::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices,
1106 gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001107{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001108 if (mode == GL_LINE_LOOP)
1109 {
1110 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1111 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001112 else if (mode == GL_TRIANGLE_FAN)
1113 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001114 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1115 }
1116 else if (instances > 0)
1117 {
1118 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001119 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001120 else
1121 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001122 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001123 }
1124}
1125
1126void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1127{
1128 // Get the raw indices for an indexed draw
1129 if (type != GL_NONE && elementArrayBuffer)
1130 {
1131 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001132 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001133 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001134 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001135 }
1136
1137 if (!mLineLoopIB)
1138 {
1139 mLineLoopIB = new StreamingIndexBufferInterface(this);
1140 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1141 {
1142 delete mLineLoopIB;
1143 mLineLoopIB = NULL;
1144
1145 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001146 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001147 }
1148 }
1149
Geoff Lang57e713e2013-07-31 17:01:58 -04001150 // Checked by Renderer11::applyPrimitiveType
1151 ASSERT(count >= 0);
1152
1153 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001154 {
1155 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1156 return gl::error(GL_OUT_OF_MEMORY);
1157 }
1158
Geoff Lang57e713e2013-07-31 17:01:58 -04001159 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001160 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1161 {
1162 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001163 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001164 }
1165
1166 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001167 unsigned int offset;
1168 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001169 {
1170 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001171 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001172 }
1173
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001174 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001175 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001176
1177 switch (type)
1178 {
1179 case GL_NONE: // Non-indexed draw
1180 for (int i = 0; i < count; i++)
1181 {
1182 data[i] = i;
1183 }
1184 data[count] = 0;
1185 break;
1186 case GL_UNSIGNED_BYTE:
1187 for (int i = 0; i < count; i++)
1188 {
1189 data[i] = static_cast<const GLubyte*>(indices)[i];
1190 }
1191 data[count] = static_cast<const GLubyte*>(indices)[0];
1192 break;
1193 case GL_UNSIGNED_SHORT:
1194 for (int i = 0; i < count; i++)
1195 {
1196 data[i] = static_cast<const GLushort*>(indices)[i];
1197 }
1198 data[count] = static_cast<const GLushort*>(indices)[0];
1199 break;
1200 case GL_UNSIGNED_INT:
1201 for (int i = 0; i < count; i++)
1202 {
1203 data[i] = static_cast<const GLuint*>(indices)[i];
1204 }
1205 data[count] = static_cast<const GLuint*>(indices)[0];
1206 break;
1207 default: UNREACHABLE();
1208 }
1209
1210 if (!mLineLoopIB->unmapBuffer())
1211 {
1212 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001213 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001214 }
1215
Geoff Lang7840b172014-03-13 11:20:44 -04001216 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1217 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1218 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001219
Geoff Lang7840b172014-03-13 11:20:44 -04001220 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1221 {
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001222 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001223 mAppliedIB = d3dIndexBuffer;
1224 mAppliedIBFormat = indexFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001225 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001226 }
1227
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001228 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001229}
1230
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001231void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001232{
1233 // Get the raw indices for an indexed draw
1234 if (type != GL_NONE && elementArrayBuffer)
1235 {
1236 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001237 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001238 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001239 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001240 }
1241
1242 if (!mTriangleFanIB)
1243 {
1244 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1245 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1246 {
1247 delete mTriangleFanIB;
1248 mTriangleFanIB = NULL;
1249
1250 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001251 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001252 }
1253 }
1254
Geoff Lang57e713e2013-07-31 17:01:58 -04001255 // Checked by Renderer11::applyPrimitiveType
1256 ASSERT(count >= 3);
1257
Geoff Langeadfd572013-07-09 15:55:07 -04001258 const unsigned int numTris = count - 2;
1259
Geoff Lang57e713e2013-07-31 17:01:58 -04001260 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001261 {
1262 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1263 return gl::error(GL_OUT_OF_MEMORY);
1264 }
1265
1266 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001267 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1268 {
1269 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001270 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001271 }
1272
1273 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001274 unsigned int offset;
1275 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001276 {
1277 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001278 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001279 }
1280
1281 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001282 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001283
1284 switch (type)
1285 {
1286 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001287 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001288 {
1289 data[i*3 + 0] = 0;
1290 data[i*3 + 1] = i + 1;
1291 data[i*3 + 2] = i + 2;
1292 }
1293 break;
1294 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001295 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001296 {
1297 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1298 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1299 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1300 }
1301 break;
1302 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001303 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001304 {
1305 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1306 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1307 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1308 }
1309 break;
1310 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001311 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001312 {
1313 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1314 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1315 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1316 }
1317 break;
1318 default: UNREACHABLE();
1319 }
1320
1321 if (!mTriangleFanIB->unmapBuffer())
1322 {
1323 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001324 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001325 }
1326
Geoff Lang7840b172014-03-13 11:20:44 -04001327 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1328 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1329 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001330
Geoff Lang7840b172014-03-13 11:20:44 -04001331 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1332 {
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001333 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001334 mAppliedIB = d3dIndexBuffer;
1335 mAppliedIBFormat = indexFormat;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001336 mAppliedIBOffset = indexBufferOffset;
1337 }
1338
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001339 if (instances > 0)
1340 {
1341 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1342 }
1343 else
1344 {
1345 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1346 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001347}
1348
Geoff Lang04fb89a2014-06-09 15:05:36 -04001349void Renderer11::applyShaders(gl::ProgramBinary *programBinary, const gl::VertexFormat inputLayout[], const gl::Framebuffer *framebuffer,
1350 bool rasterizerDiscard, bool transformFeedbackActive)
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001351{
Jamie Madillc5a83002014-02-14 16:41:25 -05001352 ShaderExecutable *vertexExe = programBinary->getVertexExecutableForInputLayout(inputLayout);
Geoff Lang04fb89a2014-06-09 15:05:36 -04001353 ShaderExecutable *pixelExe = programBinary->getPixelExecutableForFramebuffer(framebuffer);
Jamie Madill6246dc82014-01-29 09:26:47 -05001354 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001355
Jamie Madill6246dc82014-01-29 09:26:47 -05001356 ID3D11VertexShader *vertexShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader() : NULL);
Jamie Madill6246dc82014-01-29 09:26:47 -05001357
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001358 ID3D11PixelShader *pixelShader = NULL;
1359 // Skip pixel shader if we're doing rasterizer discard.
1360 if (!rasterizerDiscard)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001361 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001362 pixelShader = (pixelExe ? ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader() : NULL);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001363 }
1364
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001365 ID3D11GeometryShader *geometryShader = NULL;
1366 if (transformFeedbackActive)
Geoff Lang0550d032014-01-30 11:29:07 -05001367 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001368 geometryShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getStreamOutShader() : NULL);
1369 }
1370 else if (mCurRasterState.pointDrawMode)
1371 {
1372 geometryShader = (geometryExe ? ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader() : NULL);
Geoff Lang0550d032014-01-30 11:29:07 -05001373 }
1374
Jamie Madill6246dc82014-01-29 09:26:47 -05001375 bool dirtyUniforms = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001376
Jamie Madill6246dc82014-01-29 09:26:47 -05001377 if (vertexShader != mAppliedVertexShader)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001378 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001379 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1380 mAppliedVertexShader = vertexShader;
1381 dirtyUniforms = true;
1382 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001383
Jamie Madill6246dc82014-01-29 09:26:47 -05001384 if (geometryShader != mAppliedGeometryShader)
1385 {
1386 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1387 mAppliedGeometryShader = geometryShader;
1388 dirtyUniforms = true;
1389 }
1390
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001391 if (geometryExe && mCurRasterState.pointDrawMode)
1392 {
1393 mCurPointGeometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
1394 }
1395 else
1396 {
1397 mCurPointGeometryShader = NULL;
1398 }
1399
Jamie Madill6246dc82014-01-29 09:26:47 -05001400 if (pixelShader != mAppliedPixelShader)
1401 {
1402 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1403 mAppliedPixelShader = pixelShader;
1404 dirtyUniforms = true;
1405 }
1406
1407 if (dirtyUniforms)
1408 {
1409 programBinary->dirtyAllUniforms();
daniel@transgaming.come4991412012-12-20 20:55:34 +00001410 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001411}
1412
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001413void Renderer11::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001414{
Jamie Madill834e8b72014-04-11 13:33:58 -04001415 const std::vector<gl::LinkedUniform*> &uniformArray = programBinary.getUniforms();
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001416
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001417 unsigned int totalRegisterCountVS = 0;
1418 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001419
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001420 bool vertexUniformsDirty = false;
1421 bool pixelUniformsDirty = false;
1422
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001423 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001424 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001425 const gl::LinkedUniform &uniform = *uniformArray[uniformIndex];
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001426
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001427 if (uniform.isReferencedByVertexShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001428 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001429 totalRegisterCountVS += uniform.registerCount;
1430 vertexUniformsDirty = (vertexUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001431 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001432
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001433 if (uniform.isReferencedByFragmentShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001434 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001435 totalRegisterCountPS += uniform.registerCount;
1436 pixelUniformsDirty = (pixelUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001437 }
1438 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001439
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001440 const UniformStorage11 *vertexUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getVertexUniformStorage());
1441 const UniformStorage11 *fragmentUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getFragmentUniformStorage());
1442 ASSERT(vertexUniformStorage);
1443 ASSERT(fragmentUniformStorage);
1444
1445 ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
1446 ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001447
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001448 float (*mapVS)[4] = NULL;
1449 float (*mapPS)[4] = NULL;
1450
Shannon Woods5ab33c82013-06-26 15:31:09 -04001451 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1452 {
1453 D3D11_MAPPED_SUBRESOURCE map = {0};
1454 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001455 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001456 ASSERT(SUCCEEDED(result));
1457 mapVS = (float(*)[4])map.pData;
1458 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001459
Shannon Woods5ab33c82013-06-26 15:31:09 -04001460 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1461 {
1462 D3D11_MAPPED_SUBRESOURCE map = {0};
1463 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001464 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001465 ASSERT(SUCCEEDED(result));
1466 mapPS = (float(*)[4])map.pData;
1467 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001468
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001469 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001470 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001471 gl::LinkedUniform *uniform = uniformArray[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001472
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001473 if (!uniform->isSampler())
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001474 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001475 unsigned int componentCount = (4 - uniform->registerElement);
1476
Jamie Madill71cc91f2013-09-18 12:51:22 -04001477 // 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 -04001478 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001479
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001480 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001481 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001482 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001483 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001484
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001485 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001486 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001487 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001488 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001489 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001490 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001491
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001492 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001493 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001494 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001495 }
1496
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001497 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001498 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001499 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001500 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001501
1502 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1503 {
1504 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1505 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1506 }
1507
1508 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1509 {
1510 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1511 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1512 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001513
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001514 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001515 if (!mDriverConstantBufferVS)
1516 {
1517 D3D11_BUFFER_DESC constantBufferDescription = {0};
1518 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1519 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1520 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1521 constantBufferDescription.CPUAccessFlags = 0;
1522 constantBufferDescription.MiscFlags = 0;
1523 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001524
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001525 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001526 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001527 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001528
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001529 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1530 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001531
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001532 if (!mDriverConstantBufferPS)
1533 {
1534 D3D11_BUFFER_DESC constantBufferDescription = {0};
1535 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1536 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1537 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1538 constantBufferDescription.CPUAccessFlags = 0;
1539 constantBufferDescription.MiscFlags = 0;
1540 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001541
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001542 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001543 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001544 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001545
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001546 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1547 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001548
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001549 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1550 {
1551 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1552 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1553 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001554
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001555 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1556 {
1557 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1558 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1559 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001560
1561 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001562 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1563 {
1564 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1565 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1566 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001567}
1568
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001569void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001570{
Geoff Langda507fe2013-08-20 12:01:42 -04001571 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001572 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001573}
1574
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001575void Renderer11::markAllStateDirty()
1576{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001577 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1578 {
1579 mAppliedRenderTargetSerials[rtIndex] = 0;
1580 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001581 mAppliedDepthbufferSerial = 0;
1582 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001583 mDepthStencilInitialized = false;
1584 mRenderTargetDescInitialized = false;
1585
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001586 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001587 {
1588 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001589 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001590 }
1591 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1592 {
1593 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001594 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001595 }
1596
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001597 mForceSetBlendState = true;
1598 mForceSetRasterState = true;
1599 mForceSetDepthStencilState = true;
1600 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001601 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001602
Geoff Lang7840b172014-03-13 11:20:44 -04001603 mAppliedIB = NULL;
1604 mAppliedIBFormat = DXGI_FORMAT_UNKNOWN;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001605 mAppliedIBOffset = 0;
1606
Jamie Madill6246dc82014-01-29 09:26:47 -05001607 mAppliedVertexShader = NULL;
1608 mAppliedGeometryShader = NULL;
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001609 mCurPointGeometryShader = NULL;
Jamie Madill6246dc82014-01-29 09:26:47 -05001610 mAppliedPixelShader = NULL;
Geoff Langeeba6e12014-02-03 13:12:30 -05001611
1612 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1613 {
1614 mAppliedTFBuffers[i] = NULL;
1615 mAppliedTFOffsets[i] = 0;
1616 }
1617
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001618 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1619 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001620
1621 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001622
1623 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1624 {
1625 mCurrentConstantBufferVS[i] = -1;
1626 mCurrentConstantBufferPS[i] = -1;
1627 }
1628
1629 mCurrentVertexConstantBuffer = NULL;
1630 mCurrentPixelConstantBuffer = NULL;
1631 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001632
1633 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001634}
1635
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001636void Renderer11::releaseDeviceResources()
1637{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001638 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001639 mInputLayoutCache.clear();
1640
Geoff Langea228632013-07-30 15:17:12 -04001641 SafeDelete(mVertexDataManager);
1642 SafeDelete(mIndexDataManager);
1643 SafeDelete(mLineLoopIB);
1644 SafeDelete(mTriangleFanIB);
1645 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001646 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001647 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001648
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001649 SafeRelease(mDriverConstantBufferVS);
1650 SafeRelease(mDriverConstantBufferPS);
1651 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001652}
1653
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001654void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001655{
1656 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001657 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001658}
1659
1660bool Renderer11::isDeviceLost()
1661{
1662 return mDeviceLost;
1663}
1664
1665// set notify to true to broadcast a message to all contexts of the device loss
1666bool Renderer11::testDeviceLost(bool notify)
1667{
1668 bool isLost = false;
1669
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001670 // GetRemovedReason is used to test if the device is removed
1671 HRESULT result = mDevice->GetDeviceRemovedReason();
1672 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001673
1674 if (isLost)
1675 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001676 // Log error if this is a new device lost event
1677 if (mDeviceLost == false)
1678 {
1679 ERR("The D3D11 device was removed: 0x%08X", result);
1680 }
1681
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001682 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001683 // we'll probably get this done again by notifyDeviceLost
1684 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001685 // Note that we don't want to clear the device loss status here
1686 // -- this needs to be done by resetDevice
1687 mDeviceLost = true;
1688 if (notify)
1689 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001690 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001691 }
1692 }
1693
1694 return isLost;
1695}
1696
1697bool Renderer11::testDeviceResettable()
1698{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001699 // determine if the device is resettable by creating a dummy device
1700 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001701
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001702 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001703 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001704 return false;
1705 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001706
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001707 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001708 {
1709 D3D_FEATURE_LEVEL_11_0,
1710 D3D_FEATURE_LEVEL_10_1,
1711 D3D_FEATURE_LEVEL_10_0,
1712 };
1713
1714 ID3D11Device* dummyDevice;
1715 D3D_FEATURE_LEVEL dummyFeatureLevel;
1716 ID3D11DeviceContext* dummyContext;
1717
1718 HRESULT result = D3D11CreateDevice(NULL,
1719 D3D_DRIVER_TYPE_HARDWARE,
1720 NULL,
1721 #if defined(_DEBUG)
1722 D3D11_CREATE_DEVICE_DEBUG,
1723 #else
1724 0,
1725 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001726 featureLevels,
1727 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001728 D3D11_SDK_VERSION,
1729 &dummyDevice,
1730 &dummyFeatureLevel,
1731 &dummyContext);
1732
1733 if (!mDevice || FAILED(result))
1734 {
1735 return false;
1736 }
1737
Geoff Langea228632013-07-30 15:17:12 -04001738 SafeRelease(dummyContext);
1739 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001740
1741 return true;
1742}
1743
1744void Renderer11::release()
1745{
1746 releaseDeviceResources();
1747
Geoff Langea228632013-07-30 15:17:12 -04001748 SafeRelease(mDxgiFactory);
1749 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001750
1751 if (mDeviceContext)
1752 {
1753 mDeviceContext->ClearState();
1754 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001755 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001756 }
1757
Geoff Langea228632013-07-30 15:17:12 -04001758 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001759
1760 if (mD3d11Module)
1761 {
1762 FreeLibrary(mD3d11Module);
1763 mD3d11Module = NULL;
1764 }
1765
1766 if (mDxgiModule)
1767 {
1768 FreeLibrary(mDxgiModule);
1769 mDxgiModule = NULL;
1770 }
Geoff Langdad5ed32014-02-10 12:59:17 -05001771
1772 mCompiler.release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001773}
1774
1775bool Renderer11::resetDevice()
1776{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001777 // recreate everything
1778 release();
1779 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001780
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001781 if (result != EGL_SUCCESS)
1782 {
1783 ERR("Could not reinitialize D3D11 device: %08X", result);
1784 return false;
1785 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001786
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001787 mDeviceLost = false;
1788
1789 return true;
1790}
1791
1792DWORD Renderer11::getAdapterVendor() const
1793{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001794 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001795}
1796
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001797std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001798{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001799 std::ostringstream rendererString;
1800
1801 rendererString << mDescription;
1802 rendererString << " Direct3D11";
1803
1804 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1805 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1806
1807 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001808}
1809
1810GUID Renderer11::getAdapterIdentifier() const
1811{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001812 // Use the adapter LUID as our adapter ID
1813 // This number is local to a machine is only guaranteed to be unique between restarts
1814 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1815 GUID adapterId = {0};
1816 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1817 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001818}
1819
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001820Range Renderer11::getViewportBounds() const
1821{
1822 switch (mFeatureLevel)
1823 {
1824 case D3D_FEATURE_LEVEL_11_0:
1825 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1826 case D3D_FEATURE_LEVEL_10_1:
1827 case D3D_FEATURE_LEVEL_10_0:
1828 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1829 default: UNREACHABLE();
1830 return Range(0, 0);
1831 }
1832}
1833
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001834unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001835{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001836 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1837 switch (mFeatureLevel)
1838 {
1839 case D3D_FEATURE_LEVEL_11_0:
1840 case D3D_FEATURE_LEVEL_10_1:
1841 case D3D_FEATURE_LEVEL_10_0:
1842 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1843 default: UNREACHABLE();
1844 return 0;
1845 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001846}
1847
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001848unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1849{
1850 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1851}
1852
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001853unsigned int Renderer11::getReservedVertexUniformVectors() const
1854{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001855 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001856}
1857
1858unsigned int Renderer11::getReservedFragmentUniformVectors() const
1859{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001860 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001861}
1862
1863unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001864{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001865 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1866 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1867 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001868}
1869
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001870unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001871{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001872 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1873 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1874 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001875}
1876
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001877unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001878{
1879 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001880 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1881 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001882 switch (mFeatureLevel)
1883 {
1884 case D3D_FEATURE_LEVEL_11_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001885 return D3D11_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001886 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001887 return D3D10_1_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001888 case D3D_FEATURE_LEVEL_10_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001889 return D3D10_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001890 default: UNREACHABLE();
1891 return 0;
1892 }
1893}
1894
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001895unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
1896{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001897 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1898 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1899
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001900 switch (mFeatureLevel)
1901 {
1902 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001903 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001904 case D3D_FEATURE_LEVEL_10_1:
1905 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001906 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001907 default: UNREACHABLE();
1908 return 0;
1909 }
1910}
1911
1912unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
1913{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001914 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1915 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1916
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001917 switch (mFeatureLevel)
1918 {
1919 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001920 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001921 case D3D_FEATURE_LEVEL_10_1:
1922 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001923 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001924 default: UNREACHABLE();
1925 return 0;
1926 }
1927}
1928
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001929unsigned int Renderer11::getReservedVertexUniformBuffers() const
1930{
1931 // we reserve one buffer for the application uniforms, and one for driver uniforms
1932 return 2;
1933}
1934
1935unsigned int Renderer11::getReservedFragmentUniformBuffers() const
1936{
1937 // we reserve one buffer for the application uniforms, and one for driver uniforms
1938 return 2;
1939}
1940
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001941unsigned int Renderer11::getReservedVaryings() const
1942{
Jamie Madill2bf8b372014-06-16 17:18:51 -04001943 // We potentially reserve varyings for gl_Position, dx_Position, gl_FragCoord and gl_PointSize
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001944 return 4;
1945}
1946
1947
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001948unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
1949{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001950 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
1951 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
1952
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001953 switch (mFeatureLevel)
1954 {
1955 case D3D_FEATURE_LEVEL_11_0:
1956 return D3D11_SO_BUFFER_SLOT_COUNT;
1957 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001958 return D3D10_1_SO_BUFFER_SLOT_COUNT;
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001959 case D3D_FEATURE_LEVEL_10_0:
1960 return D3D10_SO_BUFFER_SLOT_COUNT;
1961 default: UNREACHABLE();
1962 return 0;
1963 }
1964}
1965
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001966unsigned int Renderer11::getMaxTransformFeedbackSeparateComponents() const
1967{
1968 switch (mFeatureLevel)
1969 {
1970 case D3D_FEATURE_LEVEL_11_0:
1971 return getMaxTransformFeedbackInterleavedComponents() / getMaxTransformFeedbackBuffers();
1972 case D3D_FEATURE_LEVEL_10_1:
1973 case D3D_FEATURE_LEVEL_10_0:
1974 // D3D 10 and 10.1 only allow one output per output slot if an output slot other than zero
1975 // is used.
1976 return 4;
1977 default: UNREACHABLE();
1978 return 0;
1979 }
1980}
1981
1982unsigned int Renderer11::getMaxTransformFeedbackInterleavedComponents() const
1983{
1984 return (getMaxVaryingVectors() * 4);
1985}
1986
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00001987unsigned int Renderer11::getMaxUniformBufferSize() const
1988{
1989 // Each component is a 4-element vector of 4-byte units (floats)
1990 const unsigned int bytesPerComponent = 4 * sizeof(float);
1991
1992 switch (mFeatureLevel)
1993 {
1994 case D3D_FEATURE_LEVEL_11_0:
1995 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1996 case D3D_FEATURE_LEVEL_10_1:
1997 case D3D_FEATURE_LEVEL_10_0:
1998 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
1999 default: UNREACHABLE();
2000 return 0;
2001 }
2002}
2003
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002004bool Renderer11::getShareHandleSupport() const
2005{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002006 // We only currently support share handles with BGRA surfaces, because
2007 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002008 // PIX doesn't seem to support using share handles, so disable them.
Geoff Langcec35902014-04-16 10:52:36 -04002009 return getCaps().extensions.textureFormatBGRA8888 && !gl::perfActive();
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002010}
2011
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002012bool Renderer11::getPostSubBufferSupport() const
2013{
2014 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2015 return false;
2016}
2017
Jamie Madill13a2f852013-12-11 16:35:08 -05002018int Renderer11::getMaxRecommendedElementsIndices() const
2019{
2020 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2021 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2022
2023 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2024 return std::numeric_limits<GLint>::max();
2025}
2026
2027int Renderer11::getMaxRecommendedElementsVertices() const
2028{
2029 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2030 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2031
2032 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2033 return std::numeric_limits<GLint>::max();
2034}
2035
Geoff Lang05b05022014-06-11 15:31:45 -04002036bool Renderer11::getSRGBTextureSupport() const
2037{
2038 return true;
2039}
2040
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002041int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002042{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002043 switch (mFeatureLevel)
2044 {
2045 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002046 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002047 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2048 default: UNREACHABLE(); return 0;
2049 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002050}
2051
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002052int Renderer11::getMinorShaderModel() const
2053{
2054 switch (mFeatureLevel)
2055 {
2056 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2057 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2058 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2059 default: UNREACHABLE(); return 0;
2060 }
2061}
2062
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002063float Renderer11::getMaxPointSize() const
2064{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002065 // choose a reasonable maximum. we enforce this in the shader.
2066 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2067 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002068}
2069
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002070int Renderer11::getMaxViewportDimension() const
2071{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002072 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2073 // In our case return the maximum texture size, which is the maximum render buffer size.
2074 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2075 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2076
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002077 switch (mFeatureLevel)
2078 {
2079 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002080 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002081 case D3D_FEATURE_LEVEL_10_1:
2082 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002083 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002084 default: UNREACHABLE();
2085 return 0;
2086 }
2087}
2088
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002089int Renderer11::getMaxTextureWidth() const
2090{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002091 switch (mFeatureLevel)
2092 {
2093 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2094 case D3D_FEATURE_LEVEL_10_1:
2095 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2096 default: UNREACHABLE(); return 0;
2097 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002098}
2099
2100int Renderer11::getMaxTextureHeight() const
2101{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002102 switch (mFeatureLevel)
2103 {
2104 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2105 case D3D_FEATURE_LEVEL_10_1:
2106 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2107 default: UNREACHABLE(); return 0;
2108 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002109}
2110
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002111int Renderer11::getMaxTextureDepth() const
2112{
2113 switch (mFeatureLevel)
2114 {
2115 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2116 case D3D_FEATURE_LEVEL_10_1:
2117 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2118 default: UNREACHABLE(); return 0;
2119 }
2120}
2121
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002122int Renderer11::getMaxTextureArrayLayers() const
2123{
2124 switch (mFeatureLevel)
2125 {
2126 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2127 case D3D_FEATURE_LEVEL_10_1:
2128 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2129 default: UNREACHABLE(); return 0;
2130 }
2131}
2132
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002133int Renderer11::getMinSwapInterval() const
2134{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002135 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002136}
2137
2138int Renderer11::getMaxSwapInterval() const
2139{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002140 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002141}
2142
2143int Renderer11::getMaxSupportedSamples() const
2144{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002145 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002146}
2147
Geoff Lang005df412013-10-16 14:12:50 -04002148GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002149{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002150 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002151 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2152 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2153}
2154
Geoff Lang005df412013-10-16 14:12:50 -04002155GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002156{
2157 unsigned int numCounts = 0;
2158
2159 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002160 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2161 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002162 {
2163 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2164 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2165
2166 if (iter != mMultisampleSupportMap.end())
2167 {
2168 const MultisampleSupportInfo& info = iter->second;
2169 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2170 {
2171 if (info.qualityLevels[i] > 0)
2172 {
2173 numCounts++;
2174 }
2175 }
2176 }
2177 }
2178
2179 return numCounts;
2180}
2181
Geoff Lang005df412013-10-16 14:12:50 -04002182void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002183{
2184 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002185 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2186 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2187 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002188 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002189 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002190
2191 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2192 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2193
2194 if (iter != mMultisampleSupportMap.end())
2195 {
2196 const MultisampleSupportInfo& info = iter->second;
2197 int bufPos = 0;
2198 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2199 {
2200 if (info.qualityLevels[i] > 0)
2201 {
2202 params[bufPos++] = i + 1;
2203 }
2204 }
2205 }
2206}
2207
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002208int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2209{
2210 if (requested == 0)
2211 {
2212 return 0;
2213 }
2214
2215 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2216 if (iter != mMultisampleSupportMap.end())
2217 {
2218 const MultisampleSupportInfo& info = iter->second;
2219 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2220 {
2221 if (info.qualityLevels[i] > 0)
2222 {
2223 return i + 1;
2224 }
2225 }
2226 }
2227
2228 return -1;
2229}
2230
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002231unsigned int Renderer11::getMaxRenderTargets() const
2232{
Geoff Lang4ace4232014-06-18 19:12:48 -04002233 return d3d11::GetMaximumSimultaneousRenderTargets(mFeatureLevel);
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002234}
2235
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002236bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002237{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002238 if (source && dest)
2239 {
2240 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2241 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2242
Nicolas Capens76b258f2014-04-03 10:59:42 -04002243 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002244
2245 dest11->invalidateSwizzleCache();
2246
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002247 return true;
2248 }
2249
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002250 return false;
2251}
2252
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002253bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002254{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002255 if (source && dest)
2256 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002257 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2258 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002259
Nicolas Capens76b258f2014-04-03 10:59:42 -04002260 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002261
2262 dest11->invalidateSwizzleCache();
2263
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002264 return true;
2265 }
2266
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002267 return false;
2268}
2269
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002270bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2271{
2272 if (source && dest)
2273 {
2274 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2275 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2276
Nicolas Capens76b258f2014-04-03 10:59:42 -04002277 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002278
2279 dest11->invalidateSwizzleCache();
2280
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002281 return true;
2282 }
2283
2284 return false;
2285}
2286
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002287bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2288{
2289 if (source && dest)
2290 {
2291 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2292 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2293
Nicolas Capens76b258f2014-04-03 10:59:42 -04002294 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002295
2296 dest11->invalidateSwizzleCache();
2297
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002298 return true;
2299 }
2300
2301 return false;
2302}
2303
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002304bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002305 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002306{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002307 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002308 if (!colorbuffer)
2309 {
2310 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002311 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002312 }
2313
2314 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2315 if (!sourceRenderTarget)
2316 {
2317 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002318 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002319 }
2320
2321 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2322 if (!source)
2323 {
2324 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002325 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002326 }
2327
2328 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2329 if (!storage11)
2330 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002331 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002332 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002333 }
2334
2335 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2336 if (!destRenderTarget)
2337 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002338 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002339 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002340 }
2341
2342 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2343 if (!dest)
2344 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002345 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002346 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002347 }
2348
Geoff Langb86b9792013-06-04 16:32:05 -04002349 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2350 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002351
Geoff Langb86b9792013-06-04 16:32:05 -04002352 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2353 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002354
Geoff Langb86b9792013-06-04 16:32:05 -04002355 // Use nearest filtering because source and destination are the same size for the direct
2356 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002357 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002358 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002359
Geoff Lang42477a42013-09-17 17:07:02 -04002360 storage11->invalidateSwizzleCacheLevel(level);
2361
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002362 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002363}
2364
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002365bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002366 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002367{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002368 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002369 if (!colorbuffer)
2370 {
2371 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002372 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002373 }
2374
2375 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2376 if (!sourceRenderTarget)
2377 {
2378 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002379 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002380 }
2381
2382 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2383 if (!source)
2384 {
2385 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002386 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002387 }
2388
2389 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2390 if (!storage11)
2391 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002392 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002393 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002394 }
2395
Nicolas Capensb13f8662013-06-04 13:30:19 -04002396 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002397 if (!destRenderTarget)
2398 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002399 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002400 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002401 }
2402
2403 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2404 if (!dest)
2405 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002406 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002407 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002408 }
2409
Geoff Langb86b9792013-06-04 16:32:05 -04002410 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2411 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002412
Geoff Langb86b9792013-06-04 16:32:05 -04002413 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2414 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002415
Geoff Langb86b9792013-06-04 16:32:05 -04002416 // Use nearest filtering because source and destination are the same size for the direct
2417 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002418 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002419 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002420
Geoff Lang42477a42013-09-17 17:07:02 -04002421 storage11->invalidateSwizzleCacheLevel(level);
2422
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002423 return ret;
2424}
2425
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002426bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2427 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2428{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002429 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002430 if (!colorbuffer)
2431 {
2432 ERR("Failed to retrieve the color buffer from the frame buffer.");
2433 return gl::error(GL_OUT_OF_MEMORY, false);
2434 }
2435
2436 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2437 if (!sourceRenderTarget)
2438 {
2439 ERR("Failed to retrieve the render target from the frame buffer.");
2440 return gl::error(GL_OUT_OF_MEMORY, false);
2441 }
2442
2443 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2444 if (!source)
2445 {
2446 ERR("Failed to retrieve the render target view from the render target.");
2447 return gl::error(GL_OUT_OF_MEMORY, false);
2448 }
2449
2450 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2451 if (!storage11)
2452 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002453 ERR("Failed to retrieve the texture storage from the destination.");
2454 return gl::error(GL_OUT_OF_MEMORY, false);
2455 }
2456
2457 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2458 if (!destRenderTarget)
2459 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002460 ERR("Failed to retrieve the render target from the destination storage.");
2461 return gl::error(GL_OUT_OF_MEMORY, false);
2462 }
2463
2464 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2465 if (!dest)
2466 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002467 ERR("Failed to retrieve the render target view from the destination render target.");
2468 return gl::error(GL_OUT_OF_MEMORY, false);
2469 }
2470
Geoff Langb86b9792013-06-04 16:32:05 -04002471 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2472 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002473
Geoff Langb86b9792013-06-04 16:32:05 -04002474 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2475 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002476
Geoff Langb86b9792013-06-04 16:32:05 -04002477 // Use nearest filtering because source and destination are the same size for the direct
2478 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002479 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002480 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002481
Geoff Lang42477a42013-09-17 17:07:02 -04002482 storage11->invalidateSwizzleCacheLevel(level);
2483
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002484 return ret;
2485}
2486
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002487bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2488 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2489{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002490 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002491 if (!colorbuffer)
2492 {
2493 ERR("Failed to retrieve the color buffer from the frame buffer.");
2494 return gl::error(GL_OUT_OF_MEMORY, false);
2495 }
2496
2497 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2498 if (!sourceRenderTarget)
2499 {
2500 ERR("Failed to retrieve the render target from the frame buffer.");
2501 return gl::error(GL_OUT_OF_MEMORY, false);
2502 }
2503
2504 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2505 if (!source)
2506 {
2507 ERR("Failed to retrieve the render target view from the render target.");
2508 return gl::error(GL_OUT_OF_MEMORY, false);
2509 }
2510
2511 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2512 if (!storage11)
2513 {
Geoff Langea228632013-07-30 15:17:12 -04002514 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002515 ERR("Failed to retrieve the texture storage from the destination.");
2516 return gl::error(GL_OUT_OF_MEMORY, false);
2517 }
2518
2519 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2520 if (!destRenderTarget)
2521 {
Geoff Langea228632013-07-30 15:17:12 -04002522 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002523 ERR("Failed to retrieve the render target from the destination storage.");
2524 return gl::error(GL_OUT_OF_MEMORY, false);
2525 }
2526
2527 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2528 if (!dest)
2529 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002530 ERR("Failed to retrieve the render target view from the destination render target.");
2531 return gl::error(GL_OUT_OF_MEMORY, false);
2532 }
2533
Geoff Langb86b9792013-06-04 16:32:05 -04002534 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2535 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002536
Geoff Langb86b9792013-06-04 16:32:05 -04002537 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2538 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002539
Geoff Langb86b9792013-06-04 16:32:05 -04002540 // Use nearest filtering because source and destination are the same size for the direct
2541 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002542 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002543 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002544
Geoff Lang42477a42013-09-17 17:07:02 -04002545 storage11->invalidateSwizzleCacheLevel(level);
2546
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002547 return ret;
2548}
2549
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002550void Renderer11::unapplyRenderTargets()
2551{
2552 setOneTimeRenderTarget(NULL);
2553}
2554
2555void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2556{
2557 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2558
2559 rtvArray[0] = renderTargetView;
2560
2561 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2562
2563 // Do not preserve the serial for this one-time-use render target
2564 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2565 {
2566 mAppliedRenderTargetSerials[rtIndex] = 0;
2567 }
2568}
2569
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002570RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2571{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002572 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002573 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002574
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002575 if (depth)
2576 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002577 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002578 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002579 swapChain11->getDepthStencilTexture(),
2580 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002581 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002582 }
2583 else
2584 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002585 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002586 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002587 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002588 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002589 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002590 }
2591 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002592}
2593
Geoff Langa2d97f12013-06-11 11:44:02 -04002594RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002595{
Geoff Langa2d97f12013-06-11 11:44:02 -04002596 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002597 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002598}
2599
Geoff Lang48dcae72014-02-05 16:28:24 -05002600ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type,
2601 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2602 bool separatedOutputBuffers)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002603{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002604 ShaderExecutable11 *executable = NULL;
Geoff Lang48dcae72014-02-05 16:28:24 -05002605 HRESULT result;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002606
2607 switch (type)
2608 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002609 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002610 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002611 ID3D11VertexShader *vertexShader = NULL;
2612 ID3D11GeometryShader *streamOutShader = NULL;
2613
2614 result = mDevice->CreateVertexShader(function, length, NULL, &vertexShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002615 ASSERT(SUCCEEDED(result));
2616
Geoff Lang48dcae72014-02-05 16:28:24 -05002617 if (transformFeedbackVaryings.size() > 0)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002618 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002619 std::vector<D3D11_SO_DECLARATION_ENTRY> soDeclaration;
2620 for (size_t i = 0; i < transformFeedbackVaryings.size(); i++)
2621 {
2622 const gl::LinkedVarying &varying = transformFeedbackVaryings[i];
Jamie Madill6195ef82014-06-18 10:09:43 -04002623 GLenum transposedType = gl::TransposeMatrixType(varying.type);
2624
Geoff Langcebb5aa2014-04-07 14:13:40 -04002625 for (size_t j = 0; j < varying.semanticIndexCount; j++)
Geoff Lang48dcae72014-02-05 16:28:24 -05002626 {
2627 D3D11_SO_DECLARATION_ENTRY entry = { 0 };
2628 entry.Stream = 0;
Geoff Langcebb5aa2014-04-07 14:13:40 -04002629 entry.SemanticName = varying.semanticName.c_str();
2630 entry.SemanticIndex = varying.semanticIndex + j;
Geoff Lang48dcae72014-02-05 16:28:24 -05002631 entry.StartComponent = 0;
Jamie Madill6195ef82014-06-18 10:09:43 -04002632 entry.ComponentCount = gl::VariableColumnCount(transposedType);
Geoff Lang48dcae72014-02-05 16:28:24 -05002633 entry.OutputSlot = (separatedOutputBuffers ? i : 0);
2634 soDeclaration.push_back(entry);
2635 }
2636 }
2637
2638 result = mDevice->CreateGeometryShaderWithStreamOutput(function, length, soDeclaration.data(), soDeclaration.size(),
2639 NULL, 0, 0, NULL, &streamOutShader);
2640 ASSERT(SUCCEEDED(result));
2641 }
2642
2643 if (vertexShader)
2644 {
2645 executable = new ShaderExecutable11(function, length, vertexShader, streamOutShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002646 }
2647 }
2648 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002649 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002650 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002651 ID3D11PixelShader *pixelShader = NULL;
2652
2653 result = mDevice->CreatePixelShader(function, length, NULL, &pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002654 ASSERT(SUCCEEDED(result));
2655
Geoff Lang48dcae72014-02-05 16:28:24 -05002656 if (pixelShader)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002657 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002658 executable = new ShaderExecutable11(function, length, pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002659 }
2660 }
2661 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002662 case rx::SHADER_GEOMETRY:
2663 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002664 ID3D11GeometryShader *geometryShader = NULL;
2665
2666 result = mDevice->CreateGeometryShader(function, length, NULL, &geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002667 ASSERT(SUCCEEDED(result));
2668
Geoff Lang48dcae72014-02-05 16:28:24 -05002669 if (geometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002670 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002671 executable = new ShaderExecutable11(function, length, geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002672 }
2673 }
2674 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002675 default:
2676 UNREACHABLE();
2677 break;
2678 }
2679
2680 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002681}
2682
Geoff Lang48dcae72014-02-05 16:28:24 -05002683ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
2684 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2685 bool separatedOutputBuffers, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002686{
Geoff Lang6e05c272014-03-17 10:46:54 -07002687 const char *profileType = NULL;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002688 switch (type)
2689 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002690 case rx::SHADER_VERTEX:
Geoff Lang6e05c272014-03-17 10:46:54 -07002691 profileType = "vs";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002692 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002693 case rx::SHADER_PIXEL:
Geoff Lang6e05c272014-03-17 10:46:54 -07002694 profileType = "ps";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002695 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002696 case rx::SHADER_GEOMETRY:
Geoff Lang6e05c272014-03-17 10:46:54 -07002697 profileType = "gs";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002698 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002699 default:
2700 UNREACHABLE();
2701 return NULL;
2702 }
2703
Geoff Lang6e05c272014-03-17 10:46:54 -07002704 const char *profileVersion = NULL;
2705 switch (mFeatureLevel)
2706 {
2707 case D3D_FEATURE_LEVEL_11_0:
2708 profileVersion = "5_0";
2709 break;
2710 case D3D_FEATURE_LEVEL_10_1:
2711 profileVersion = "4_1";
2712 break;
2713 case D3D_FEATURE_LEVEL_10_0:
2714 profileVersion = "4_0";
2715 break;
2716 default:
2717 UNREACHABLE();
2718 return NULL;
2719 }
2720
2721 char profile[32];
2722 snprintf(profile, ArraySize(profile), "%s_%s", profileType, profileVersion);
2723
Nicolas Capens93faad92014-05-10 12:14:13 -04002724 UINT flags = D3DCOMPILE_OPTIMIZATION_LEVEL0;
2725
2726 if (gl::perfActive())
2727 {
2728#ifndef NDEBUG
2729 flags = D3DCOMPILE_SKIP_OPTIMIZATION;
2730#endif
2731
2732 flags |= D3DCOMPILE_DEBUG;
2733
2734 std::string sourcePath = getTempPath();
2735 std::string sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(shaderHLSL);
2736 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
2737 }
2738
2739 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
2740 // Try the default flags first and if compilation fails, try some alternatives.
2741 const UINT extraFlags[] =
2742 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002743 flags,
Nicolas Capens2c27db62014-05-10 12:21:11 -04002744 flags | D3DCOMPILE_SKIP_VALIDATION,
2745 flags | D3DCOMPILE_SKIP_OPTIMIZATION
Nicolas Capens93faad92014-05-10 12:14:13 -04002746 };
2747
2748 const static char *extraFlagNames[] =
2749 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002750 "default",
Nicolas Capens2c27db62014-05-10 12:21:11 -04002751 "skip validation",
2752 "skip optimization"
Nicolas Capens93faad92014-05-10 12:14:13 -04002753 };
2754
2755 int attempts = ArraySize(extraFlags);
2756
2757 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, extraFlags, extraFlagNames, attempts);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002758 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002759 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002760 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002761 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002762
Geoff Lang48dcae72014-02-05 16:28:24 -05002763 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type,
2764 transformFeedbackVaryings, separatedOutputBuffers);
Geoff Langea228632013-07-30 15:17:12 -04002765 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002766
2767 return executable;
2768}
2769
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002770rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2771{
2772 return new UniformStorage11(this, storageSize);
2773}
2774
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002775VertexBuffer *Renderer11::createVertexBuffer()
2776{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002777 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002778}
2779
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002780IndexBuffer *Renderer11::createIndexBuffer()
2781{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002782 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002783}
2784
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002785BufferStorage *Renderer11::createBufferStorage()
2786{
2787 return new BufferStorage11(this);
2788}
2789
Brandon Jones5bf98292014-06-06 17:19:38 -07002790VertexArrayImpl *Renderer11::createVertexArray()
2791{
2792 return new VertexArray11(this);
2793}
2794
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002795QueryImpl *Renderer11::createQuery(GLenum type)
2796{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002797 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002798}
2799
2800FenceImpl *Renderer11::createFence()
2801{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002802 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002803}
2804
Geoff Lang005df412013-10-16 14:12:50 -04002805bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002806{
Geoff Lang2b31af22014-05-23 14:45:40 -04002807 ASSERT(getCaps().extensions.pixelBufferObject);
Jamie Madill4461f092013-10-10 15:10:39 -04002808
Geoff Lang2b31af22014-05-23 14:45:40 -04002809 GLuint clientVersion = getCurrentClientVersion();
Jamie Madill4461f092013-10-10 15:10:39 -04002810
2811 // sRGB formats do not work with D3D11 buffer SRVs
2812 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2813 {
2814 return false;
2815 }
2816
2817 // We cannot support direct copies to non-color-renderable formats
Geoff Langcec35902014-04-16 10:52:36 -04002818 if (!getCaps().textureCaps.get(internalFormat).colorRendering)
Jamie Madill4461f092013-10-10 15:10:39 -04002819 {
2820 return false;
2821 }
2822
2823 // We skip all 3-channel formats since sometimes format support is missing
2824 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2825 {
2826 return false;
2827 }
2828
2829 // We don't support formats which we can't represent without conversion
2830 if (getNativeTextureFormat(internalFormat) != internalFormat)
2831 {
2832 return false;
2833 }
2834
2835 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002836}
2837
Jamie Madilla21eea32013-09-18 14:36:25 -04002838bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2839 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2840{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002841 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002842 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2843}
2844
Jamie Madill3c7fa222014-06-05 13:08:51 -04002845bool Renderer11::getRenderTargetResource(gl::FramebufferAttachment *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002846{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002847 ASSERT(colorbuffer != NULL);
2848
2849 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2850 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002851 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002852 *subresourceIndex = renderTarget->getSubresourceIndex();
2853
2854 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2855 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002856 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002857 ID3D11Resource *textureResource = NULL;
2858 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002859
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002860 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002861 {
Geoff Lang8e328842014-02-10 13:11:20 -05002862 HRESULT result = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002863 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002864
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002865 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002866 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002867 return true;
2868 }
2869 else
2870 {
2871 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2872 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002873 }
2874 }
2875 }
2876 }
2877
2878 return false;
2879}
2880
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002881bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002882 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002883{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002884 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002885 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002886 gl::FramebufferAttachment *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002887
2888 if (!readBuffer)
2889 {
2890 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2891 return gl::error(GL_OUT_OF_MEMORY, false);
2892 }
2893
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002894 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002895
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002896 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002897 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002898 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2899 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002900 gl::FramebufferAttachment *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002901
2902 if (!drawBuffer)
2903 {
2904 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2905 return gl::error(GL_OUT_OF_MEMORY, false);
2906 }
2907
2908 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2909
Geoff Lang125deab2013-08-09 13:34:16 -04002910 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002911 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002912 {
2913 return false;
2914 }
2915 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002916 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002917 }
2918
Geoff Lang685806d2013-06-12 11:16:36 -04002919 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002920 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002921 gl::FramebufferAttachment *readBuffer = readTarget->getDepthOrStencilbuffer();
2922 gl::FramebufferAttachment *drawBuffer = drawTarget->getDepthOrStencilbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002923
2924 if (!readBuffer)
2925 {
2926 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2927 return gl::error(GL_OUT_OF_MEMORY, false);
2928 }
2929
2930 if (!drawBuffer)
2931 {
2932 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2933 return gl::error(GL_OUT_OF_MEMORY, false);
2934 }
2935
2936 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2937 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2938
Geoff Lang125deab2013-08-09 13:34:16 -04002939 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002940 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002941 {
2942 return false;
2943 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002944 }
2945
Geoff Lang42477a42013-09-17 17:07:02 -04002946 invalidateFramebufferSwizzles(drawTarget);
2947
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002948 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002949}
2950
Jamie Madilleb9baab2014-03-24 13:19:43 -04002951void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
2952 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002953{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002954 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002955 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002956
Jamie Madill3c7fa222014-06-05 13:08:51 -04002957 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002958
2959 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002960 {
2961 gl::Rectangle area;
2962 area.x = x;
2963 area.y = y;
2964 area.width = width;
2965 area.height = height;
2966
Jamie Madill1ef6fe72014-05-01 14:51:05 -04002967 if (pack.pixelBuffer.get() != NULL)
2968 {
2969 rx::BufferStorage11 *packBufferStorage = BufferStorage11::makeBufferStorage11(pack.pixelBuffer.get()->getStorage());
2970 PackPixelsParams packParams(area, format, type, outputPitch, pack, reinterpret_cast<ptrdiff_t>(pixels));
2971 packBufferStorage->packPixels(colorBufferTexture, subresourceIndex, packParams);
2972 }
2973 else
2974 {
2975 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch, pack, pixels);
2976 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002977
Geoff Langea228632013-07-30 15:17:12 -04002978 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002979 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002980}
2981
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002982Image *Renderer11::createImage()
2983{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002984 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002985}
2986
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002987void Renderer11::generateMipmap(Image *dest, Image *src)
2988{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002989 Image11 *dest11 = Image11::makeImage11(dest);
2990 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04002991 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002992}
2993
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002994TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
2995{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002996 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
2997 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002998}
2999
Nicolas Capensbf712d02014-03-31 14:23:35 -04003000TextureStorage *Renderer11::createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003001{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003002 return new TextureStorage11_2D(this, internalformat, renderTarget, width, height, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003003}
3004
Nicolas Capensbf712d02014-03-31 14:23:35 -04003005TextureStorage *Renderer11::createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003006{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003007 return new TextureStorage11_Cube(this, internalformat, renderTarget, size, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003008}
3009
Nicolas Capensbf712d02014-03-31 14:23:35 -04003010TextureStorage *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 +00003011{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003012 return new TextureStorage11_3D(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003013}
3014
Nicolas Capensbf712d02014-03-31 14:23:35 -04003015TextureStorage *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 +00003016{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003017 return new TextureStorage11_2DArray(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003018}
3019
Jamie Madilleb9baab2014-03-24 13:19:43 -04003020void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format,
3021 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void *pixels)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003022{
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003023 ASSERT(area.width >= 0);
3024 ASSERT(area.height >= 0);
3025
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003026 D3D11_TEXTURE2D_DESC textureDesc;
3027 texture->GetDesc(&textureDesc);
3028
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003029 // Clamp read region to the defined texture boundaries, preventing out of bounds reads
3030 // and reads of uninitialized data.
3031 gl::Rectangle safeArea;
3032 safeArea.x = gl::clamp(area.x, 0, static_cast<int>(textureDesc.Width));
3033 safeArea.y = gl::clamp(area.y, 0, static_cast<int>(textureDesc.Height));
3034 safeArea.width = gl::clamp(area.width + std::min(area.x, 0), 0,
3035 static_cast<int>(textureDesc.Width) - safeArea.x);
3036 safeArea.height = gl::clamp(area.height + std::min(area.y, 0), 0,
3037 static_cast<int>(textureDesc.Height) - safeArea.y);
3038
3039 ASSERT(safeArea.x >= 0 && safeArea.y >= 0);
3040 ASSERT(safeArea.x + safeArea.width <= static_cast<int>(textureDesc.Width));
3041 ASSERT(safeArea.y + safeArea.height <= static_cast<int>(textureDesc.Height));
3042
3043 if (safeArea.width == 0 || safeArea.height == 0)
3044 {
3045 // no work to do
3046 return;
3047 }
3048
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003049 D3D11_TEXTURE2D_DESC stagingDesc;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003050 stagingDesc.Width = safeArea.width;
3051 stagingDesc.Height = safeArea.height;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003052 stagingDesc.MipLevels = 1;
3053 stagingDesc.ArraySize = 1;
3054 stagingDesc.Format = textureDesc.Format;
3055 stagingDesc.SampleDesc.Count = 1;
3056 stagingDesc.SampleDesc.Quality = 0;
3057 stagingDesc.Usage = D3D11_USAGE_STAGING;
3058 stagingDesc.BindFlags = 0;
3059 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3060 stagingDesc.MiscFlags = 0;
3061
3062 ID3D11Texture2D* stagingTex = NULL;
3063 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3064 if (FAILED(result))
3065 {
3066 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3067 return;
3068 }
3069
3070 ID3D11Texture2D* srcTex = NULL;
3071 if (textureDesc.SampleDesc.Count > 1)
3072 {
3073 D3D11_TEXTURE2D_DESC resolveDesc;
3074 resolveDesc.Width = textureDesc.Width;
3075 resolveDesc.Height = textureDesc.Height;
3076 resolveDesc.MipLevels = 1;
3077 resolveDesc.ArraySize = 1;
3078 resolveDesc.Format = textureDesc.Format;
3079 resolveDesc.SampleDesc.Count = 1;
3080 resolveDesc.SampleDesc.Quality = 0;
3081 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3082 resolveDesc.BindFlags = 0;
3083 resolveDesc.CPUAccessFlags = 0;
3084 resolveDesc.MiscFlags = 0;
3085
3086 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3087 if (FAILED(result))
3088 {
3089 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003090 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003091 return;
3092 }
3093
3094 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3095 subResource = 0;
3096 }
3097 else
3098 {
3099 srcTex = texture;
3100 srcTex->AddRef();
3101 }
3102
3103 D3D11_BOX srcBox;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003104 srcBox.left = static_cast<UINT>(safeArea.x);
3105 srcBox.right = static_cast<UINT>(safeArea.x + safeArea.width);
3106 srcBox.top = static_cast<UINT>(safeArea.y);
3107 srcBox.bottom = static_cast<UINT>(safeArea.y + safeArea.height);
3108 srcBox.front = 0;
3109 srcBox.back = 1;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003110
3111 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3112
Geoff Langea228632013-07-30 15:17:12 -04003113 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003114
Jamie Madill7538f7f2014-04-17 11:53:39 -04003115 PackPixelsParams packParams(safeArea, format, type, outputPitch, pack, 0);
3116 packPixels(stagingTex, packParams, pixels);
3117
3118 SafeRelease(stagingTex);
3119}
3120
3121void Renderer11::packPixels(ID3D11Texture2D *readTexture, const PackPixelsParams &params, void *pixelsOut)
3122{
3123 D3D11_TEXTURE2D_DESC textureDesc;
3124 readTexture->GetDesc(&textureDesc);
3125
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003126 D3D11_MAPPED_SUBRESOURCE mapping;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003127 HRESULT hr = mDeviceContext->Map(readTexture, 0, D3D11_MAP_READ, 0, &mapping);
Geoff Lang9cd19152014-05-28 15:54:34 -04003128 UNUSED_ASSERTION_VARIABLE(hr);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003129 ASSERT(SUCCEEDED(hr));
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003130
3131 unsigned char *source;
3132 int inputPitch;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003133 if (params.pack.reverseRowOrder)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003134 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003135 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (params.area.height - 1);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003136 inputPitch = -static_cast<int>(mapping.RowPitch);
3137 }
3138 else
3139 {
3140 source = static_cast<unsigned char*>(mapping.pData);
3141 inputPitch = static_cast<int>(mapping.RowPitch);
3142 }
3143
Geoff Lang697ad3e2013-06-04 10:11:28 -04003144 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003145
Geoff Lang005df412013-10-16 14:12:50 -04003146 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003147 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3148 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3149
3150 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3151
Jamie Madill7538f7f2014-04-17 11:53:39 -04003152 if (sourceFormat == params.format && sourceType == params.type)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003153 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003154 unsigned char *dest = static_cast<unsigned char*>(pixelsOut) + params.offset;
3155 for (int y = 0; y < params.area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003156 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003157 memcpy(dest + y * params.outputPitch, source + y * inputPitch, params.area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003158 }
3159 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003160 else
3161 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003162 GLenum destInternalFormat = gl::GetSizedInternalFormat(params.format, params.type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003163 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3164
Jamie Madill7538f7f2014-04-17 11:53:39 -04003165 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, params.format, params.type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003166 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003167 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003168 // Fast copy is possible through some special function
Jamie Madill7538f7f2014-04-17 11:53:39 -04003169 for (int y = 0; y < params.area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003170 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003171 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003172 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003173 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003174 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3175
3176 fastCopyFunc(src, dest);
3177 }
3178 }
3179 }
3180 else
3181 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003182 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003183 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(params.format, params.type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003184
3185 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3186 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3187 sizeof(temp) >= sizeof(gl::ColorUI) &&
3188 sizeof(temp) >= sizeof(gl::ColorI));
3189
Jamie Madill7538f7f2014-04-17 11:53:39 -04003190 for (int y = 0; y < params.area.height; y++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003191 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003192 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003193 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003194 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003195 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3196
3197 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3198 // will not allow the copy otherwise.
3199 readFunc(src, temp);
3200 writeFunc(temp, dest);
3201 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003202 }
3203 }
3204 }
3205
Jamie Madill7538f7f2014-04-17 11:53:39 -04003206 mDeviceContext->Unmap(readTexture, 0);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003207}
3208
Geoff Lang758d5b22013-06-11 11:42:50 -04003209bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003210 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3211 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003212{
Geoff Lang975af372013-06-12 11:19:22 -04003213 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3214 // it should never be the case that both color and depth/stencil need to be blitted at
3215 // at the same time.
3216 ASSERT(colorBlit != (depthBlit || stencilBlit));
3217
Geoff Langc1f51be2013-06-11 11:49:14 -04003218 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003219
Geoff Lang4d782732013-07-22 10:44:18 -04003220 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3221 if (!drawRenderTarget)
3222 {
3223 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3224 return gl::error(GL_OUT_OF_MEMORY, false);
3225 }
3226
3227 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3228 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3229 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3230 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3231
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003232 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3233 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003234 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003235 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003236 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003237 }
3238
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003239 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003240 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003241 unsigned int readSubresource = 0;
3242 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003243 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003244 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3245 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003246
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003247 if (unresolvedTexture)
3248 {
3249 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3250 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003251
Geoff Langea228632013-07-30 15:17:12 -04003252 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003253
Geoff Lang5c9a29a2014-02-11 10:26:24 -05003254 HRESULT hresult = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3255 if (FAILED(hresult))
Geoff Langc1f51be2013-06-11 11:49:14 -04003256 {
Geoff Langea228632013-07-30 15:17:12 -04003257 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003258 return gl::error(GL_OUT_OF_MEMORY, false);
3259 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003260 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003261 }
3262 else
3263 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003264 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003265 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003266 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003267 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003268 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003269 }
3270
Geoff Lang4d782732013-07-22 10:44:18 -04003271 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003272 {
Geoff Lang4d782732013-07-22 10:44:18 -04003273 SafeRelease(readTexture);
3274 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003275 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003276 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003277 }
3278
Geoff Lang125deab2013-08-09 13:34:16 -04003279 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3280 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3281
3282 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3283
3284 bool wholeBufferCopy = !scissorNeeded &&
3285 readRect.x == 0 && readRect.width == readSize.width &&
3286 readRect.y == 0 && readRect.height == readSize.height &&
3287 drawRect.x == 0 && drawRect.width == drawSize.width &&
3288 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003289
Geoff Langc1f51be2013-06-11 11:49:14 -04003290 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003291
Geoff Lang1cd1b212014-02-11 09:42:27 -05003292 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003293
3294 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3295 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3296 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3297 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3298
3299 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3300 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3301 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3302
Geoff Langc1f51be2013-06-11 11:49:14 -04003303 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003304 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3305 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003306 {
Geoff Lang125deab2013-08-09 13:34:16 -04003307 UINT dstX = drawRect.x;
3308 UINT dstY = drawRect.y;
3309
Geoff Langc1f51be2013-06-11 11:49:14 -04003310 D3D11_BOX readBox;
3311 readBox.left = readRect.x;
3312 readBox.right = readRect.x + readRect.width;
3313 readBox.top = readRect.y;
3314 readBox.bottom = readRect.y + readRect.height;
3315 readBox.front = 0;
3316 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003317
Geoff Lang125deab2013-08-09 13:34:16 -04003318 if (scissorNeeded)
3319 {
3320 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3321 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3322
3323 if (drawRect.x < scissor->x)
3324 {
3325 dstX = scissor->x;
3326 readBox.left += (scissor->x - drawRect.x);
3327 }
3328 if (drawRect.y < scissor->y)
3329 {
3330 dstY = scissor->y;
3331 readBox.top += (scissor->y - drawRect.y);
3332 }
3333 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3334 {
3335 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3336 }
3337 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3338 {
3339 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3340 }
3341 }
3342
Geoff Langc1f51be2013-06-11 11:49:14 -04003343 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3344 // We also require complete framebuffer copies for depth-stencil blit.
3345 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003346
Geoff Lang125deab2013-08-09 13:34:16 -04003347 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003348 readTexture, readSubresource, pSrcBox);
3349 result = true;
3350 }
3351 else
3352 {
3353 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003354 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003355
Geoff Lang975af372013-06-12 11:19:22 -04003356 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003357 {
Geoff Lang975af372013-06-12 11:19:22 -04003358 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003359 drawTexture, drawSubresource, drawArea, drawSize,
3360 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003361 }
3362 else if (depthBlit)
3363 {
Geoff Lang125deab2013-08-09 13:34:16 -04003364 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3365 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003366 }
3367 else if (stencilBlit)
3368 {
3369 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003370 drawTexture, drawSubresource, drawArea, drawSize,
3371 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003372 }
3373 else
3374 {
Geoff Lang685806d2013-06-12 11:16:36 -04003375 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003376 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3377 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003378 }
3379 }
3380
3381 SafeRelease(readTexture);
3382 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003383
3384 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003385}
3386
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003387ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3388{
3389 D3D11_TEXTURE2D_DESC textureDesc;
3390 source->GetDesc(&textureDesc);
3391
3392 if (textureDesc.SampleDesc.Count > 1)
3393 {
3394 D3D11_TEXTURE2D_DESC resolveDesc;
3395 resolveDesc.Width = textureDesc.Width;
3396 resolveDesc.Height = textureDesc.Height;
3397 resolveDesc.MipLevels = 1;
3398 resolveDesc.ArraySize = 1;
3399 resolveDesc.Format = textureDesc.Format;
3400 resolveDesc.SampleDesc.Count = 1;
3401 resolveDesc.SampleDesc.Quality = 0;
3402 resolveDesc.Usage = textureDesc.Usage;
3403 resolveDesc.BindFlags = textureDesc.BindFlags;
3404 resolveDesc.CPUAccessFlags = 0;
3405 resolveDesc.MiscFlags = 0;
3406
3407 ID3D11Texture2D *resolveTexture = NULL;
3408 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3409 if (FAILED(result))
3410 {
3411 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3412 return NULL;
3413 }
3414
3415 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3416 return resolveTexture;
3417 }
3418 else
3419 {
3420 source->AddRef();
3421 return source;
3422 }
3423}
3424
Jamie Madill3c7fa222014-06-05 13:08:51 -04003425void Renderer11::invalidateFBOAttachmentSwizzles(gl::FramebufferAttachment *attachment, int mipLevel)
Geoff Lang42477a42013-09-17 17:07:02 -04003426{
Jamie Madill3c7fa222014-06-05 13:08:51 -04003427 ASSERT(attachment->isTexture());
3428 TextureStorage *texStorage = attachment->getTextureStorage();
Geoff Lang42477a42013-09-17 17:07:02 -04003429 if (texStorage)
3430 {
3431 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3432 if (!texStorage11)
3433 {
3434 ERR("texture storage pointer unexpectedly null.");
3435 return;
3436 }
3437
3438 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3439 }
3440}
3441
3442void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3443{
3444 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3445 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003446 gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(colorAttachment);
3447 if (attachment && attachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003448 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003449 invalidateFBOAttachmentSwizzles(attachment, framebuffer->getColorbufferMipLevel(colorAttachment));
Geoff Lang42477a42013-09-17 17:07:02 -04003450 }
3451 }
3452
Jamie Madill3c7fa222014-06-05 13:08:51 -04003453 gl::FramebufferAttachment *depthAttachment = framebuffer->getDepthbuffer();
3454 if (depthAttachment && depthAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003455 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003456 invalidateFBOAttachmentSwizzles(depthAttachment, framebuffer->getDepthbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003457 }
3458
Jamie Madill3c7fa222014-06-05 13:08:51 -04003459 gl::FramebufferAttachment *stencilAttachment = framebuffer->getStencilbuffer();
3460 if (stencilAttachment && stencilAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003461 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003462 invalidateFBOAttachmentSwizzles(stencilAttachment, framebuffer->getStencilbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003463 }
3464}
3465
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003466bool Renderer11::getLUID(LUID *adapterLuid) const
3467{
3468 adapterLuid->HighPart = 0;
3469 adapterLuid->LowPart = 0;
3470
3471 if (!mDxgiAdapter)
3472 {
3473 return false;
3474 }
3475
3476 DXGI_ADAPTER_DESC adapterDesc;
3477 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3478 {
3479 return false;
3480 }
3481
3482 *adapterLuid = adapterDesc.AdapterLuid;
3483 return true;
3484}
3485
Geoff Lang005df412013-10-16 14:12:50 -04003486GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003487{
3488 int clientVersion = getCurrentClientVersion();
3489 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3490}
3491
Jamie Madill95ffb862014-01-29 09:26:59 -05003492rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3493{
3494 return gl_d3d11::GetVertexConversionType(vertexFormat);
3495}
3496
3497GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3498{
3499 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3500}
3501
Geoff Lang61e49a52013-05-29 10:22:58 -04003502Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3503{
3504 MultisampleSupportInfo supportInfo = { 0 };
3505
3506 UINT formatSupport;
3507 HRESULT result;
3508
3509 result = mDevice->CheckFormatSupport(format, &formatSupport);
3510 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3511 {
3512 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3513 {
3514 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3515 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3516 {
3517 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3518 }
3519 else
3520 {
3521 supportInfo.qualityLevels[i - 1] = 0;
3522 }
3523 }
3524 }
3525
3526 return supportInfo;
3527}
3528
Geoff Langcec35902014-04-16 10:52:36 -04003529gl::Caps Renderer11::generateCaps() const
3530{
3531 return d3d11_gl::GenerateCaps(mDevice);
3532}
3533
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003534}