blob: a58822f13e64a5e371e9cfb4c750c25bb8a9ee79 [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"
Geoff Langd47e0fc2013-08-29 11:40:43 -040016#include "libGLESv2/renderer/d3d11/Renderer11.h"
17#include "libGLESv2/renderer/d3d11/RenderTarget11.h"
18#include "libGLESv2/renderer/d3d11/renderer11_utils.h"
19#include "libGLESv2/renderer/d3d11/formatutils11.h"
20#include "libGLESv2/renderer/d3d11/ShaderExecutable11.h"
21#include "libGLESv2/renderer/d3d11/SwapChain11.h"
22#include "libGLESv2/renderer/d3d11/Image11.h"
23#include "libGLESv2/renderer/d3d11/VertexBuffer11.h"
24#include "libGLESv2/renderer/d3d11/IndexBuffer11.h"
25#include "libGLESv2/renderer/d3d11/BufferStorage11.h"
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000026#include "libGLESv2/renderer/VertexDataManager.h"
27#include "libGLESv2/renderer/IndexDataManager.h"
Geoff Langd47e0fc2013-08-29 11:40:43 -040028#include "libGLESv2/renderer/d3d11/TextureStorage11.h"
29#include "libGLESv2/renderer/d3d11/Query11.h"
30#include "libGLESv2/renderer/d3d11/Fence11.h"
31#include "libGLESv2/renderer/d3d11/Blit11.h"
32#include "libGLESv2/renderer/d3d11/Clear11.h"
33#include "libGLESv2/renderer/d3d11/PixelTransfer11.h"
Brandon Jones5bf98292014-06-06 17:19:38 -070034#include "libGLESv2/renderer/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 {
657 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
658 stencilRef != stencilBackRef ||
659 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
660 {
661 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
662 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000663 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000664 }
665
666 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
667 if (!dxDepthStencilState)
668 {
669 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
670 "setting the default depth stencil state.");
671 }
672
Jamie Madillec91cd32014-01-21 16:38:12 -0500673 // Max D3D11 stencil reference value is 0xFF, corresponding to the max 8 bits in a stencil buffer
674 // GL specifies we should clamp the ref value to the nearest bit depth when doing stencil ops
675 META_ASSERT(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF);
676 META_ASSERT(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF);
Geoff Lang0bf3a982014-02-11 09:40:48 -0500677 UINT dxStencilRef = std::min<UINT>(stencilRef, 0xFFu);
Jamie Madillec91cd32014-01-21 16:38:12 -0500678
Geoff Lang0bf3a982014-02-11 09:40:48 -0500679 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, dxStencilRef);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000680
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000681 mCurDepthStencilState = depthStencilState;
682 mCurStencilRef = stencilRef;
683 mCurStencilBackRef = stencilBackRef;
684 }
685
686 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000687}
688
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000689void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000690{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000691 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
692 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000693 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000694 if (enabled)
695 {
696 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000697 rect.left = std::max(0, scissor.x);
698 rect.top = std::max(0, scissor.y);
699 rect.right = scissor.x + std::max(0, scissor.width);
700 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000701
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000702 mDeviceContext->RSSetScissorRects(1, &rect);
703 }
704
705 if (enabled != mScissorEnabled)
706 {
707 mForceSetRasterState = true;
708 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000709
710 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000711 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000712 }
713
714 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000715}
716
daniel@transgaming.com12985182012-12-20 20:56:31 +0000717bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000718 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000719{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000720 gl::Rectangle actualViewport = viewport;
721 float actualZNear = gl::clamp01(zNear);
722 float actualZFar = gl::clamp01(zFar);
723 if (ignoreViewport)
724 {
725 actualViewport.x = 0;
726 actualViewport.y = 0;
727 actualViewport.width = mRenderTargetDesc.width;
728 actualViewport.height = mRenderTargetDesc.height;
729 actualZNear = 0.0f;
730 actualZFar = 1.0f;
731 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000732
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000733 // Get D3D viewport bounds, which depends on the feature level
734 const Range& viewportBounds = getViewportBounds();
735
736 // 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 +0000737 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000738 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
739 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
740 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
741 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
742 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
743 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000744 dxViewport.MinDepth = actualZNear;
745 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000746
747 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
748 {
749 return false; // Nothing to render
750 }
751
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000752 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
753 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000754
daniel@transgaming.com53670042012-11-28 20:55:51 +0000755 if (viewportChanged)
756 {
757 mDeviceContext->RSSetViewports(1, &dxViewport);
758
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000759 mCurViewport = actualViewport;
760 mCurNear = actualZNear;
761 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000762
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000763 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
764 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
765 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
766 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000767
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000768 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
769 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000770
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000771 mVertexConstants.depthRange[0] = actualZNear;
772 mVertexConstants.depthRange[1] = actualZFar;
773 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000774
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000775 mPixelConstants.depthRange[0] = actualZNear;
776 mPixelConstants.depthRange[1] = actualZFar;
777 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000778 }
779
780 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000781 return true;
782}
783
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000784bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
785{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000786 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000787
Geoff Lang57e713e2013-07-31 17:01:58 -0400788 GLsizei minCount = 0;
789
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000790 switch (mode)
791 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400792 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
793 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
794 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
795 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
796 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
797 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000798 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400799 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000800 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000801 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000802 }
803
Geoff Lang4c095862013-07-22 10:43:36 -0400804 if (primitiveTopology != mCurrentPrimitiveTopology)
805 {
806 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
807 mCurrentPrimitiveTopology = primitiveTopology;
808 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000809
Geoff Lang57e713e2013-07-31 17:01:58 -0400810 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000811}
812
813bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000814{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000815 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000816 // Also extract the render target dimensions and view
817 unsigned int renderTargetWidth = 0;
818 unsigned int renderTargetHeight = 0;
819 GLenum renderTargetFormat = 0;
820 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
821 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
822 bool missingColorRenderTarget = true;
823
824 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000825 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000826 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
827
828 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000829 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000830 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
831 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
832
Jamie Madill3c7fa222014-06-05 13:08:51 -0400833 gl::FramebufferAttachment *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000834
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000835 if (!colorbuffer)
836 {
837 ERR("render target pointer unexpectedly null.");
838 return false;
839 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000840
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000841 // check for zero-sized default framebuffer, which is a special case.
842 // in this case we do not wish to modify any state and just silently return false.
843 // this will not report any gl error but will cause the calling method to return.
844 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
845 {
846 return false;
847 }
848
849 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
850
851 // Extract the render target dimensions and view
852 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
853 if (!renderTarget)
854 {
855 ERR("render target pointer unexpectedly null.");
856 return false;
857 }
858
859 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
860 if (!framebufferRTVs[colorAttachment])
861 {
862 ERR("render target view pointer unexpectedly null.");
863 return false;
864 }
865
866 if (missingColorRenderTarget)
867 {
868 renderTargetWidth = colorbuffer->getWidth();
869 renderTargetHeight = colorbuffer->getHeight();
870 renderTargetFormat = colorbuffer->getActualFormat();
871 missingColorRenderTarget = false;
872 }
Jamie Madillba597af2013-10-22 13:12:15 -0400873
Geoff Lang91382e52014-01-07 16:16:30 -0500874 // TODO: Detect if this color buffer is already bound as a texture and unbind it first to prevent
875 // D3D11 warnings.
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000876 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000877 }
878
879 // Get the depth stencil render buffer and serials
Jamie Madill3c7fa222014-06-05 13:08:51 -0400880 gl::FramebufferAttachment *depthStencil = NULL;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000881 unsigned int depthbufferSerial = 0;
882 unsigned int stencilbufferSerial = 0;
883 if (framebuffer->getDepthbufferType() != GL_NONE)
884 {
885 depthStencil = framebuffer->getDepthbuffer();
886 if (!depthStencil)
887 {
888 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000889 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000890 return false;
891 }
892
893 depthbufferSerial = depthStencil->getSerial();
894 }
895 else if (framebuffer->getStencilbufferType() != GL_NONE)
896 {
897 depthStencil = framebuffer->getStencilbuffer();
898 if (!depthStencil)
899 {
900 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000901 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000902 return false;
903 }
904
905 stencilbufferSerial = depthStencil->getSerial();
906 }
907
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000908 ID3D11DepthStencilView* framebufferDSV = NULL;
909 if (depthStencil)
910 {
911 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
912 if (!depthStencilRenderTarget)
913 {
914 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000915 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000916 return false;
917 }
918
919 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
920 if (!framebufferDSV)
921 {
922 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000923 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000924 return false;
925 }
926
927 // If there is no render buffer, the width, height and format values come from
928 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000929 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000930 {
931 renderTargetWidth = depthStencil->getWidth();
932 renderTargetHeight = depthStencil->getHeight();
933 renderTargetFormat = depthStencil->getActualFormat();
934 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000935 }
936
937 // Apply the render target and depth stencil
938 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000939 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000940 depthbufferSerial != mAppliedDepthbufferSerial ||
941 stencilbufferSerial != mAppliedStencilbufferSerial)
942 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000943 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000944
945 mRenderTargetDesc.width = renderTargetWidth;
946 mRenderTargetDesc.height = renderTargetHeight;
947 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000948 mForceSetViewport = true;
949 mForceSetScissor = true;
Geoff Langc142e9d2013-09-30 15:19:47 -0400950 mForceSetBlendState = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000951
Nicolas Capensaea8e942014-05-09 19:14:08 -0400952 if (!mDepthStencilInitialized)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000953 {
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000954 mForceSetRasterState = true;
955 }
956
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000957 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
958 {
959 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
960 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000961 mAppliedDepthbufferSerial = depthbufferSerial;
962 mAppliedStencilbufferSerial = stencilbufferSerial;
963 mRenderTargetDescInitialized = true;
964 mDepthStencilInitialized = true;
965 }
966
Geoff Lang42477a42013-09-17 17:07:02 -0400967 invalidateFramebufferSwizzles(framebuffer);
968
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000969 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000970}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000971
Jamie Madill57a89722013-07-02 11:57:03 -0400972GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -0400973 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000974{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000975 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -0400976 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000977 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000978 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000979 return err;
980 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000981
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000982 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000983}
984
daniel@transgaming.com31240482012-11-28 21:06:41 +0000985GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000986{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000987 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000988
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000989 if (err == GL_NO_ERROR)
990 {
Geoff Lang7840b172014-03-13 11:20:44 -0400991 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
992
993 ID3D11Buffer *buffer = NULL;
994 DXGI_FORMAT bufferFormat = indexBuffer->getIndexFormat();
995
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000996 if (indexInfo->storage)
997 {
Geoff Lang7840b172014-03-13 11:20:44 -0400998 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
999 buffer = storage->getBuffer(BUFFER_USAGE_INDEX);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001000 }
Geoff Lang7840b172014-03-13 11:20:44 -04001001 else
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001002 {
Geoff Lang7840b172014-03-13 11:20:44 -04001003 buffer = indexBuffer->getBuffer();
1004 }
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001005
Geoff Lang7840b172014-03-13 11:20:44 -04001006 if (buffer != mAppliedIB || bufferFormat != mAppliedIBFormat || indexInfo->startOffset != mAppliedIBOffset)
1007 {
1008 mDeviceContext->IASetIndexBuffer(buffer, bufferFormat, indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001009
Geoff Lang7840b172014-03-13 11:20:44 -04001010 mAppliedIB = buffer;
1011 mAppliedIBFormat = bufferFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001012 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001013 }
1014 }
1015
1016 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001017}
1018
Geoff Langeeba6e12014-02-03 13:12:30 -05001019void Renderer11::applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[])
1020{
1021 ID3D11Buffer* d3dBuffers[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1022 UINT d3dOffsets[gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1023 bool requiresUpdate = false;
1024 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1025 {
1026 if (transformFeedbackBuffers[i])
1027 {
1028 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(transformFeedbackBuffers[i]->getStorage());
1029 ID3D11Buffer *buffer = storage->getBuffer(BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK);
1030
1031 d3dBuffers[i] = buffer;
1032 d3dOffsets[i] = (mAppliedTFBuffers[i] != buffer) ? static_cast<UINT>(offsets[i]) : -1;
1033 }
1034 else
1035 {
1036 d3dBuffers[i] = NULL;
1037 d3dOffsets[i] = 0;
1038 }
1039
1040 if (d3dBuffers[i] != mAppliedTFBuffers[i] || offsets[i] != mAppliedTFOffsets[i])
1041 {
1042 requiresUpdate = true;
1043 }
1044 }
1045
1046 if (requiresUpdate)
1047 {
1048 mDeviceContext->SOSetTargets(ArraySize(d3dBuffers), d3dBuffers, d3dOffsets);
1049 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1050 {
1051 mAppliedTFBuffers[i] = d3dBuffers[i];
1052 mAppliedTFOffsets[i] = offsets[i];
1053 }
1054 }
1055}
1056
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001057void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001058{
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001059 if (mode == GL_POINTS && transformFeedbackActive)
1060 {
1061 // Since point sprites are generated with a geometry shader, too many vertices will
1062 // be written if transform feedback is active. To work around this, draw only the points
1063 // with the stream out shader and no pixel shader to feed the stream out buffers and then
1064 // draw again with the point sprite geometry shader to rasterize the point sprites.
1065
1066 mDeviceContext->PSSetShader(NULL, NULL, 0);
1067
1068 if (instances > 0)
1069 {
1070 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1071 }
1072 else
1073 {
1074 mDeviceContext->Draw(count, 0);
1075 }
1076
1077 mDeviceContext->GSSetShader(mCurPointGeometryShader, NULL, 0);
1078 mDeviceContext->PSSetShader(mAppliedPixelShader, NULL, 0);
1079
1080 if (instances > 0)
1081 {
1082 mDeviceContext->DrawInstanced(count, instances, 0, 0);
1083 }
1084 else
1085 {
1086 mDeviceContext->Draw(count, 0);
1087 }
1088
1089 mDeviceContext->GSSetShader(mAppliedGeometryShader, NULL, 0);
1090 }
1091 else if (mode == GL_LINE_LOOP)
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001092 {
1093 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1094 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001095 else if (mode == GL_TRIANGLE_FAN)
1096 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001097 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001098 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001099 else if (instances > 0)
1100 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001101 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001102 }
1103 else
1104 {
1105 mDeviceContext->Draw(count, 0);
1106 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001107}
1108
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001109void Renderer11::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices,
1110 gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001111{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001112 if (mode == GL_LINE_LOOP)
1113 {
1114 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1115 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001116 else if (mode == GL_TRIANGLE_FAN)
1117 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001118 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1119 }
1120 else if (instances > 0)
1121 {
1122 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001123 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001124 else
1125 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001126 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001127 }
1128}
1129
1130void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1131{
1132 // Get the raw indices for an indexed draw
1133 if (type != GL_NONE && elementArrayBuffer)
1134 {
1135 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001136 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001137 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001138 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001139 }
1140
1141 if (!mLineLoopIB)
1142 {
1143 mLineLoopIB = new StreamingIndexBufferInterface(this);
1144 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1145 {
1146 delete mLineLoopIB;
1147 mLineLoopIB = NULL;
1148
1149 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001150 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001151 }
1152 }
1153
Geoff Lang57e713e2013-07-31 17:01:58 -04001154 // Checked by Renderer11::applyPrimitiveType
1155 ASSERT(count >= 0);
1156
1157 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001158 {
1159 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1160 return gl::error(GL_OUT_OF_MEMORY);
1161 }
1162
Geoff Lang57e713e2013-07-31 17:01:58 -04001163 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001164 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1165 {
1166 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001167 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001168 }
1169
1170 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001171 unsigned int offset;
1172 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001173 {
1174 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001175 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001176 }
1177
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001178 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001179 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001180
1181 switch (type)
1182 {
1183 case GL_NONE: // Non-indexed draw
1184 for (int i = 0; i < count; i++)
1185 {
1186 data[i] = i;
1187 }
1188 data[count] = 0;
1189 break;
1190 case GL_UNSIGNED_BYTE:
1191 for (int i = 0; i < count; i++)
1192 {
1193 data[i] = static_cast<const GLubyte*>(indices)[i];
1194 }
1195 data[count] = static_cast<const GLubyte*>(indices)[0];
1196 break;
1197 case GL_UNSIGNED_SHORT:
1198 for (int i = 0; i < count; i++)
1199 {
1200 data[i] = static_cast<const GLushort*>(indices)[i];
1201 }
1202 data[count] = static_cast<const GLushort*>(indices)[0];
1203 break;
1204 case GL_UNSIGNED_INT:
1205 for (int i = 0; i < count; i++)
1206 {
1207 data[i] = static_cast<const GLuint*>(indices)[i];
1208 }
1209 data[count] = static_cast<const GLuint*>(indices)[0];
1210 break;
1211 default: UNREACHABLE();
1212 }
1213
1214 if (!mLineLoopIB->unmapBuffer())
1215 {
1216 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001217 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001218 }
1219
Geoff Lang7840b172014-03-13 11:20:44 -04001220 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1221 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1222 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001223
Geoff Lang7840b172014-03-13 11:20:44 -04001224 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1225 {
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001226 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001227 mAppliedIB = d3dIndexBuffer;
1228 mAppliedIBFormat = indexFormat;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001229 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001230 }
1231
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001232 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001233}
1234
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001235void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001236{
1237 // Get the raw indices for an indexed draw
1238 if (type != GL_NONE && elementArrayBuffer)
1239 {
1240 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001241 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001242 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001243 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001244 }
1245
1246 if (!mTriangleFanIB)
1247 {
1248 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1249 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1250 {
1251 delete mTriangleFanIB;
1252 mTriangleFanIB = NULL;
1253
1254 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001255 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001256 }
1257 }
1258
Geoff Lang57e713e2013-07-31 17:01:58 -04001259 // Checked by Renderer11::applyPrimitiveType
1260 ASSERT(count >= 3);
1261
Geoff Langeadfd572013-07-09 15:55:07 -04001262 const unsigned int numTris = count - 2;
1263
Geoff Lang57e713e2013-07-31 17:01:58 -04001264 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001265 {
1266 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1267 return gl::error(GL_OUT_OF_MEMORY);
1268 }
1269
1270 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001271 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1272 {
1273 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001274 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001275 }
1276
1277 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001278 unsigned int offset;
1279 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001280 {
1281 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001282 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001283 }
1284
1285 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001286 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001287
1288 switch (type)
1289 {
1290 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001291 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001292 {
1293 data[i*3 + 0] = 0;
1294 data[i*3 + 1] = i + 1;
1295 data[i*3 + 2] = i + 2;
1296 }
1297 break;
1298 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001299 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001300 {
1301 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1302 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1303 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1304 }
1305 break;
1306 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001307 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001308 {
1309 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1310 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1311 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1312 }
1313 break;
1314 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001315 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001316 {
1317 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1318 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1319 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1320 }
1321 break;
1322 default: UNREACHABLE();
1323 }
1324
1325 if (!mTriangleFanIB->unmapBuffer())
1326 {
1327 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001328 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001329 }
1330
Geoff Lang7840b172014-03-13 11:20:44 -04001331 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1332 ID3D11Buffer *d3dIndexBuffer = indexBuffer->getBuffer();
1333 DXGI_FORMAT indexFormat = indexBuffer->getIndexFormat();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001334
Geoff Lang7840b172014-03-13 11:20:44 -04001335 if (mAppliedIB != d3dIndexBuffer || mAppliedIBFormat != indexFormat || mAppliedIBOffset != indexBufferOffset)
1336 {
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001337 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
Geoff Lang7840b172014-03-13 11:20:44 -04001338 mAppliedIB = d3dIndexBuffer;
1339 mAppliedIBFormat = indexFormat;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001340 mAppliedIBOffset = indexBufferOffset;
1341 }
1342
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001343 if (instances > 0)
1344 {
1345 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1346 }
1347 else
1348 {
1349 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1350 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001351}
1352
Geoff Lang04fb89a2014-06-09 15:05:36 -04001353void Renderer11::applyShaders(gl::ProgramBinary *programBinary, const gl::VertexFormat inputLayout[], const gl::Framebuffer *framebuffer,
1354 bool rasterizerDiscard, bool transformFeedbackActive)
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001355{
Jamie Madillc5a83002014-02-14 16:41:25 -05001356 ShaderExecutable *vertexExe = programBinary->getVertexExecutableForInputLayout(inputLayout);
Geoff Lang04fb89a2014-06-09 15:05:36 -04001357 ShaderExecutable *pixelExe = programBinary->getPixelExecutableForFramebuffer(framebuffer);
Jamie Madill6246dc82014-01-29 09:26:47 -05001358 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001359
Jamie Madill6246dc82014-01-29 09:26:47 -05001360 ID3D11VertexShader *vertexShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader() : NULL);
Jamie Madill6246dc82014-01-29 09:26:47 -05001361
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001362 ID3D11PixelShader *pixelShader = NULL;
1363 // Skip pixel shader if we're doing rasterizer discard.
1364 if (!rasterizerDiscard)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001365 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001366 pixelShader = (pixelExe ? ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader() : NULL);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001367 }
1368
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001369 ID3D11GeometryShader *geometryShader = NULL;
1370 if (transformFeedbackActive)
Geoff Lang0550d032014-01-30 11:29:07 -05001371 {
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001372 geometryShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getStreamOutShader() : NULL);
1373 }
1374 else if (mCurRasterState.pointDrawMode)
1375 {
1376 geometryShader = (geometryExe ? ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader() : NULL);
Geoff Lang0550d032014-01-30 11:29:07 -05001377 }
1378
Jamie Madill6246dc82014-01-29 09:26:47 -05001379 bool dirtyUniforms = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001380
Jamie Madill6246dc82014-01-29 09:26:47 -05001381 if (vertexShader != mAppliedVertexShader)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001382 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001383 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1384 mAppliedVertexShader = vertexShader;
1385 dirtyUniforms = true;
1386 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001387
Jamie Madill6246dc82014-01-29 09:26:47 -05001388 if (geometryShader != mAppliedGeometryShader)
1389 {
1390 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1391 mAppliedGeometryShader = geometryShader;
1392 dirtyUniforms = true;
1393 }
1394
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001395 if (geometryExe && mCurRasterState.pointDrawMode)
1396 {
1397 mCurPointGeometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
1398 }
1399 else
1400 {
1401 mCurPointGeometryShader = NULL;
1402 }
1403
Jamie Madill6246dc82014-01-29 09:26:47 -05001404 if (pixelShader != mAppliedPixelShader)
1405 {
1406 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1407 mAppliedPixelShader = pixelShader;
1408 dirtyUniforms = true;
1409 }
1410
1411 if (dirtyUniforms)
1412 {
1413 programBinary->dirtyAllUniforms();
daniel@transgaming.come4991412012-12-20 20:55:34 +00001414 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001415}
1416
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001417void Renderer11::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001418{
Jamie Madill834e8b72014-04-11 13:33:58 -04001419 const std::vector<gl::LinkedUniform*> &uniformArray = programBinary.getUniforms();
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001420
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001421 unsigned int totalRegisterCountVS = 0;
1422 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001423
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001424 bool vertexUniformsDirty = false;
1425 bool pixelUniformsDirty = false;
1426
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001427 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001428 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001429 const gl::LinkedUniform &uniform = *uniformArray[uniformIndex];
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001430
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001431 if (uniform.isReferencedByVertexShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001432 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001433 totalRegisterCountVS += uniform.registerCount;
1434 vertexUniformsDirty = (vertexUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001435 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001436
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001437 if (uniform.isReferencedByFragmentShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001438 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001439 totalRegisterCountPS += uniform.registerCount;
1440 pixelUniformsDirty = (pixelUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001441 }
1442 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001443
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001444 const UniformStorage11 *vertexUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getVertexUniformStorage());
1445 const UniformStorage11 *fragmentUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getFragmentUniformStorage());
1446 ASSERT(vertexUniformStorage);
1447 ASSERT(fragmentUniformStorage);
1448
1449 ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
1450 ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001451
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001452 float (*mapVS)[4] = NULL;
1453 float (*mapPS)[4] = NULL;
1454
Shannon Woods5ab33c82013-06-26 15:31:09 -04001455 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1456 {
1457 D3D11_MAPPED_SUBRESOURCE map = {0};
1458 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001459 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001460 ASSERT(SUCCEEDED(result));
1461 mapVS = (float(*)[4])map.pData;
1462 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001463
Shannon Woods5ab33c82013-06-26 15:31:09 -04001464 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1465 {
1466 D3D11_MAPPED_SUBRESOURCE map = {0};
1467 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
Geoff Lang9cd19152014-05-28 15:54:34 -04001468 UNUSED_ASSERTION_VARIABLE(result);
Shannon Woods5ab33c82013-06-26 15:31:09 -04001469 ASSERT(SUCCEEDED(result));
1470 mapPS = (float(*)[4])map.pData;
1471 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001472
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001473 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001474 {
Jamie Madill834e8b72014-04-11 13:33:58 -04001475 gl::LinkedUniform *uniform = uniformArray[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001476
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001477 if (!uniform->isSampler())
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001478 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001479 unsigned int componentCount = (4 - uniform->registerElement);
1480
Jamie Madill71cc91f2013-09-18 12:51:22 -04001481 // 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 -04001482 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001483
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001484 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001485 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001486 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001487 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001488
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001489 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001490 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001491 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001492 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001493 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001494 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001495
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001496 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001497 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001498 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001499 }
1500
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001501 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001502 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001503 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001504 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001505
1506 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1507 {
1508 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1509 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1510 }
1511
1512 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1513 {
1514 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1515 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1516 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001517
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001518 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001519 if (!mDriverConstantBufferVS)
1520 {
1521 D3D11_BUFFER_DESC constantBufferDescription = {0};
1522 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1523 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1524 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1525 constantBufferDescription.CPUAccessFlags = 0;
1526 constantBufferDescription.MiscFlags = 0;
1527 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001528
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001529 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001530 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001531 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001532
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001533 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1534 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001535
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001536 if (!mDriverConstantBufferPS)
1537 {
1538 D3D11_BUFFER_DESC constantBufferDescription = {0};
1539 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1540 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1541 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1542 constantBufferDescription.CPUAccessFlags = 0;
1543 constantBufferDescription.MiscFlags = 0;
1544 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001545
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001546 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
Geoff Lang9cd19152014-05-28 15:54:34 -04001547 UNUSED_ASSERTION_VARIABLE(result);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001548 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001549
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001550 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1551 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001552
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001553 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1554 {
1555 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1556 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1557 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001558
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001559 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1560 {
1561 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1562 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1563 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001564
1565 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001566 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1567 {
1568 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1569 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1570 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001571}
1572
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001573void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001574{
Geoff Langda507fe2013-08-20 12:01:42 -04001575 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001576 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001577}
1578
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001579void Renderer11::markAllStateDirty()
1580{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001581 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1582 {
1583 mAppliedRenderTargetSerials[rtIndex] = 0;
1584 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001585 mAppliedDepthbufferSerial = 0;
1586 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001587 mDepthStencilInitialized = false;
1588 mRenderTargetDescInitialized = false;
1589
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001590 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001591 {
1592 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001593 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001594 }
1595 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1596 {
1597 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001598 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001599 }
1600
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001601 mForceSetBlendState = true;
1602 mForceSetRasterState = true;
1603 mForceSetDepthStencilState = true;
1604 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001605 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001606
Geoff Lang7840b172014-03-13 11:20:44 -04001607 mAppliedIB = NULL;
1608 mAppliedIBFormat = DXGI_FORMAT_UNKNOWN;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001609 mAppliedIBOffset = 0;
1610
Jamie Madill6246dc82014-01-29 09:26:47 -05001611 mAppliedVertexShader = NULL;
1612 mAppliedGeometryShader = NULL;
Geoff Lang4c5c6bb2014-02-05 16:32:46 -05001613 mCurPointGeometryShader = NULL;
Jamie Madill6246dc82014-01-29 09:26:47 -05001614 mAppliedPixelShader = NULL;
Geoff Langeeba6e12014-02-03 13:12:30 -05001615
1616 for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1617 {
1618 mAppliedTFBuffers[i] = NULL;
1619 mAppliedTFOffsets[i] = 0;
1620 }
1621
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001622 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1623 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001624
1625 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001626
1627 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1628 {
1629 mCurrentConstantBufferVS[i] = -1;
1630 mCurrentConstantBufferPS[i] = -1;
1631 }
1632
1633 mCurrentVertexConstantBuffer = NULL;
1634 mCurrentPixelConstantBuffer = NULL;
1635 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001636
1637 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001638}
1639
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001640void Renderer11::releaseDeviceResources()
1641{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001642 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001643 mInputLayoutCache.clear();
1644
Geoff Langea228632013-07-30 15:17:12 -04001645 SafeDelete(mVertexDataManager);
1646 SafeDelete(mIndexDataManager);
1647 SafeDelete(mLineLoopIB);
1648 SafeDelete(mTriangleFanIB);
1649 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001650 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001651 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001652
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001653 SafeRelease(mDriverConstantBufferVS);
1654 SafeRelease(mDriverConstantBufferPS);
1655 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001656}
1657
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001658void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001659{
1660 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001661 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001662}
1663
1664bool Renderer11::isDeviceLost()
1665{
1666 return mDeviceLost;
1667}
1668
1669// set notify to true to broadcast a message to all contexts of the device loss
1670bool Renderer11::testDeviceLost(bool notify)
1671{
1672 bool isLost = false;
1673
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001674 // GetRemovedReason is used to test if the device is removed
1675 HRESULT result = mDevice->GetDeviceRemovedReason();
1676 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001677
1678 if (isLost)
1679 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001680 // Log error if this is a new device lost event
1681 if (mDeviceLost == false)
1682 {
1683 ERR("The D3D11 device was removed: 0x%08X", result);
1684 }
1685
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001686 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001687 // we'll probably get this done again by notifyDeviceLost
1688 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001689 // Note that we don't want to clear the device loss status here
1690 // -- this needs to be done by resetDevice
1691 mDeviceLost = true;
1692 if (notify)
1693 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001694 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001695 }
1696 }
1697
1698 return isLost;
1699}
1700
1701bool Renderer11::testDeviceResettable()
1702{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001703 // determine if the device is resettable by creating a dummy device
1704 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001705
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001706 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001707 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001708 return false;
1709 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001710
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001711 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001712 {
1713 D3D_FEATURE_LEVEL_11_0,
1714 D3D_FEATURE_LEVEL_10_1,
1715 D3D_FEATURE_LEVEL_10_0,
1716 };
1717
1718 ID3D11Device* dummyDevice;
1719 D3D_FEATURE_LEVEL dummyFeatureLevel;
1720 ID3D11DeviceContext* dummyContext;
1721
1722 HRESULT result = D3D11CreateDevice(NULL,
1723 D3D_DRIVER_TYPE_HARDWARE,
1724 NULL,
1725 #if defined(_DEBUG)
1726 D3D11_CREATE_DEVICE_DEBUG,
1727 #else
1728 0,
1729 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001730 featureLevels,
1731 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001732 D3D11_SDK_VERSION,
1733 &dummyDevice,
1734 &dummyFeatureLevel,
1735 &dummyContext);
1736
1737 if (!mDevice || FAILED(result))
1738 {
1739 return false;
1740 }
1741
Geoff Langea228632013-07-30 15:17:12 -04001742 SafeRelease(dummyContext);
1743 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001744
1745 return true;
1746}
1747
1748void Renderer11::release()
1749{
1750 releaseDeviceResources();
1751
Geoff Langea228632013-07-30 15:17:12 -04001752 SafeRelease(mDxgiFactory);
1753 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001754
1755 if (mDeviceContext)
1756 {
1757 mDeviceContext->ClearState();
1758 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001759 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001760 }
1761
Geoff Langea228632013-07-30 15:17:12 -04001762 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001763
1764 if (mD3d11Module)
1765 {
1766 FreeLibrary(mD3d11Module);
1767 mD3d11Module = NULL;
1768 }
1769
1770 if (mDxgiModule)
1771 {
1772 FreeLibrary(mDxgiModule);
1773 mDxgiModule = NULL;
1774 }
Geoff Langdad5ed32014-02-10 12:59:17 -05001775
1776 mCompiler.release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001777}
1778
1779bool Renderer11::resetDevice()
1780{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001781 // recreate everything
1782 release();
1783 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001784
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001785 if (result != EGL_SUCCESS)
1786 {
1787 ERR("Could not reinitialize D3D11 device: %08X", result);
1788 return false;
1789 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001790
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001791 mDeviceLost = false;
1792
1793 return true;
1794}
1795
1796DWORD Renderer11::getAdapterVendor() const
1797{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001798 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001799}
1800
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001801std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001802{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001803 std::ostringstream rendererString;
1804
1805 rendererString << mDescription;
1806 rendererString << " Direct3D11";
1807
1808 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1809 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1810
1811 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001812}
1813
1814GUID Renderer11::getAdapterIdentifier() const
1815{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001816 // Use the adapter LUID as our adapter ID
1817 // This number is local to a machine is only guaranteed to be unique between restarts
1818 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1819 GUID adapterId = {0};
1820 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1821 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001822}
1823
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001824Range Renderer11::getViewportBounds() const
1825{
1826 switch (mFeatureLevel)
1827 {
1828 case D3D_FEATURE_LEVEL_11_0:
1829 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1830 case D3D_FEATURE_LEVEL_10_1:
1831 case D3D_FEATURE_LEVEL_10_0:
1832 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1833 default: UNREACHABLE();
1834 return Range(0, 0);
1835 }
1836}
1837
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001838unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001839{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001840 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1841 switch (mFeatureLevel)
1842 {
1843 case D3D_FEATURE_LEVEL_11_0:
1844 case D3D_FEATURE_LEVEL_10_1:
1845 case D3D_FEATURE_LEVEL_10_0:
1846 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1847 default: UNREACHABLE();
1848 return 0;
1849 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001850}
1851
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001852unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1853{
1854 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1855}
1856
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001857unsigned int Renderer11::getReservedVertexUniformVectors() const
1858{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001859 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001860}
1861
1862unsigned int Renderer11::getReservedFragmentUniformVectors() const
1863{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001864 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001865}
1866
1867unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001868{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001869 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1870 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1871 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001872}
1873
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001874unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001875{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001876 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1877 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1878 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001879}
1880
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001881unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001882{
1883 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001884 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1885 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001886 switch (mFeatureLevel)
1887 {
1888 case D3D_FEATURE_LEVEL_11_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001889 return D3D11_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001890 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001891 return D3D10_1_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001892 case D3D_FEATURE_LEVEL_10_0:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001893 return D3D10_VS_OUTPUT_REGISTER_COUNT - getReservedVaryings();
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001894 default: UNREACHABLE();
1895 return 0;
1896 }
1897}
1898
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001899unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
1900{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001901 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1902 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1903
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001904 switch (mFeatureLevel)
1905 {
1906 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001907 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001908 case D3D_FEATURE_LEVEL_10_1:
1909 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001910 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001911 default: UNREACHABLE();
1912 return 0;
1913 }
1914}
1915
1916unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
1917{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001918 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1919 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1920
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001921 switch (mFeatureLevel)
1922 {
1923 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001924 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001925 case D3D_FEATURE_LEVEL_10_1:
1926 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001927 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001928 default: UNREACHABLE();
1929 return 0;
1930 }
1931}
1932
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001933unsigned int Renderer11::getReservedVertexUniformBuffers() const
1934{
1935 // we reserve one buffer for the application uniforms, and one for driver uniforms
1936 return 2;
1937}
1938
1939unsigned int Renderer11::getReservedFragmentUniformBuffers() const
1940{
1941 // we reserve one buffer for the application uniforms, and one for driver uniforms
1942 return 2;
1943}
1944
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001945unsigned int Renderer11::getReservedVaryings() const
1946{
Jamie Madill2bf8b372014-06-16 17:18:51 -04001947 // We potentially reserve varyings for gl_Position, dx_Position, gl_FragCoord and gl_PointSize
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001948 return 4;
1949}
1950
1951
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001952unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
1953{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001954 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
1955 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
1956
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001957 switch (mFeatureLevel)
1958 {
1959 case D3D_FEATURE_LEVEL_11_0:
1960 return D3D11_SO_BUFFER_SLOT_COUNT;
1961 case D3D_FEATURE_LEVEL_10_1:
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001962 return D3D10_1_SO_BUFFER_SLOT_COUNT;
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001963 case D3D_FEATURE_LEVEL_10_0:
1964 return D3D10_SO_BUFFER_SLOT_COUNT;
1965 default: UNREACHABLE();
1966 return 0;
1967 }
1968}
1969
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001970unsigned int Renderer11::getMaxTransformFeedbackSeparateComponents() const
1971{
1972 switch (mFeatureLevel)
1973 {
1974 case D3D_FEATURE_LEVEL_11_0:
1975 return getMaxTransformFeedbackInterleavedComponents() / getMaxTransformFeedbackBuffers();
1976 case D3D_FEATURE_LEVEL_10_1:
1977 case D3D_FEATURE_LEVEL_10_0:
1978 // D3D 10 and 10.1 only allow one output per output slot if an output slot other than zero
1979 // is used.
1980 return 4;
1981 default: UNREACHABLE();
1982 return 0;
1983 }
1984}
1985
1986unsigned int Renderer11::getMaxTransformFeedbackInterleavedComponents() const
1987{
1988 return (getMaxVaryingVectors() * 4);
1989}
1990
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00001991unsigned int Renderer11::getMaxUniformBufferSize() const
1992{
1993 // Each component is a 4-element vector of 4-byte units (floats)
1994 const unsigned int bytesPerComponent = 4 * sizeof(float);
1995
1996 switch (mFeatureLevel)
1997 {
1998 case D3D_FEATURE_LEVEL_11_0:
1999 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2000 case D3D_FEATURE_LEVEL_10_1:
2001 case D3D_FEATURE_LEVEL_10_0:
2002 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2003 default: UNREACHABLE();
2004 return 0;
2005 }
2006}
2007
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002008bool Renderer11::getShareHandleSupport() const
2009{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002010 // We only currently support share handles with BGRA surfaces, because
2011 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002012 // PIX doesn't seem to support using share handles, so disable them.
Geoff Langcec35902014-04-16 10:52:36 -04002013 return getCaps().extensions.textureFormatBGRA8888 && !gl::perfActive();
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002014}
2015
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002016bool Renderer11::getPostSubBufferSupport() const
2017{
2018 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2019 return false;
2020}
2021
Jamie Madill13a2f852013-12-11 16:35:08 -05002022int Renderer11::getMaxRecommendedElementsIndices() const
2023{
2024 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2025 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2026
2027 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2028 return std::numeric_limits<GLint>::max();
2029}
2030
2031int Renderer11::getMaxRecommendedElementsVertices() const
2032{
2033 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2034 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2035
2036 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2037 return std::numeric_limits<GLint>::max();
2038}
2039
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002040int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002041{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002042 switch (mFeatureLevel)
2043 {
2044 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002045 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002046 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2047 default: UNREACHABLE(); return 0;
2048 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002049}
2050
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002051int Renderer11::getMinorShaderModel() const
2052{
2053 switch (mFeatureLevel)
2054 {
2055 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2056 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2057 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2058 default: UNREACHABLE(); return 0;
2059 }
2060}
2061
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002062float Renderer11::getMaxPointSize() const
2063{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002064 // choose a reasonable maximum. we enforce this in the shader.
2065 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2066 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002067}
2068
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002069int Renderer11::getMaxViewportDimension() const
2070{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002071 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2072 // In our case return the maximum texture size, which is the maximum render buffer size.
2073 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2074 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2075
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002076 switch (mFeatureLevel)
2077 {
2078 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002079 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002080 case D3D_FEATURE_LEVEL_10_1:
2081 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002082 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002083 default: UNREACHABLE();
2084 return 0;
2085 }
2086}
2087
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002088int Renderer11::getMaxTextureWidth() const
2089{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002090 switch (mFeatureLevel)
2091 {
2092 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2093 case D3D_FEATURE_LEVEL_10_1:
2094 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2095 default: UNREACHABLE(); return 0;
2096 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002097}
2098
2099int Renderer11::getMaxTextureHeight() const
2100{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002101 switch (mFeatureLevel)
2102 {
2103 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2104 case D3D_FEATURE_LEVEL_10_1:
2105 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2106 default: UNREACHABLE(); return 0;
2107 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002108}
2109
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002110int Renderer11::getMaxTextureDepth() const
2111{
2112 switch (mFeatureLevel)
2113 {
2114 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2115 case D3D_FEATURE_LEVEL_10_1:
2116 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2117 default: UNREACHABLE(); return 0;
2118 }
2119}
2120
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002121int Renderer11::getMaxTextureArrayLayers() const
2122{
2123 switch (mFeatureLevel)
2124 {
2125 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2126 case D3D_FEATURE_LEVEL_10_1:
2127 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2128 default: UNREACHABLE(); return 0;
2129 }
2130}
2131
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002132int Renderer11::getMinSwapInterval() const
2133{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002134 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002135}
2136
2137int Renderer11::getMaxSwapInterval() const
2138{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002139 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002140}
2141
2142int Renderer11::getMaxSupportedSamples() const
2143{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002144 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002145}
2146
Geoff Lang005df412013-10-16 14:12:50 -04002147GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002148{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002149 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002150 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2151 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2152}
2153
Geoff Lang005df412013-10-16 14:12:50 -04002154GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002155{
2156 unsigned int numCounts = 0;
2157
2158 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002159 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2160 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002161 {
2162 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2163 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2164
2165 if (iter != mMultisampleSupportMap.end())
2166 {
2167 const MultisampleSupportInfo& info = iter->second;
2168 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2169 {
2170 if (info.qualityLevels[i] > 0)
2171 {
2172 numCounts++;
2173 }
2174 }
2175 }
2176 }
2177
2178 return numCounts;
2179}
2180
Geoff Lang005df412013-10-16 14:12:50 -04002181void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002182{
2183 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002184 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2185 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2186 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002187 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002188 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002189
2190 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2191 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2192
2193 if (iter != mMultisampleSupportMap.end())
2194 {
2195 const MultisampleSupportInfo& info = iter->second;
2196 int bufPos = 0;
2197 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2198 {
2199 if (info.qualityLevels[i] > 0)
2200 {
2201 params[bufPos++] = i + 1;
2202 }
2203 }
2204 }
2205}
2206
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002207int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2208{
2209 if (requested == 0)
2210 {
2211 return 0;
2212 }
2213
2214 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2215 if (iter != mMultisampleSupportMap.end())
2216 {
2217 const MultisampleSupportInfo& info = iter->second;
2218 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2219 {
2220 if (info.qualityLevels[i] > 0)
2221 {
2222 return i + 1;
2223 }
2224 }
2225 }
2226
2227 return -1;
2228}
2229
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002230unsigned int Renderer11::getMaxRenderTargets() const
2231{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002232 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2233 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2234
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002235 switch (mFeatureLevel)
2236 {
2237 case D3D_FEATURE_LEVEL_11_0:
2238 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2239 case D3D_FEATURE_LEVEL_10_1:
2240 case D3D_FEATURE_LEVEL_10_0:
Geoff Langa0dc2192014-06-09 15:37:55 -04002241 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002242 default:
2243 UNREACHABLE();
2244 return 1;
2245 }
2246}
2247
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002248bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002249{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002250 if (source && dest)
2251 {
2252 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2253 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2254
Nicolas Capens76b258f2014-04-03 10:59:42 -04002255 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002256
2257 dest11->invalidateSwizzleCache();
2258
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002259 return true;
2260 }
2261
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002262 return false;
2263}
2264
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002265bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002266{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002267 if (source && dest)
2268 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002269 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2270 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002271
Nicolas Capens76b258f2014-04-03 10:59:42 -04002272 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002273
2274 dest11->invalidateSwizzleCache();
2275
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002276 return true;
2277 }
2278
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002279 return false;
2280}
2281
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002282bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2283{
2284 if (source && dest)
2285 {
2286 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2287 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2288
Nicolas Capens76b258f2014-04-03 10:59:42 -04002289 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002290
2291 dest11->invalidateSwizzleCache();
2292
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002293 return true;
2294 }
2295
2296 return false;
2297}
2298
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002299bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2300{
2301 if (source && dest)
2302 {
2303 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2304 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2305
Nicolas Capens76b258f2014-04-03 10:59:42 -04002306 mDeviceContext->CopyResource(dest11->getResource(), source11->getResource());
Geoff Lang42477a42013-09-17 17:07:02 -04002307
2308 dest11->invalidateSwizzleCache();
2309
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002310 return true;
2311 }
2312
2313 return false;
2314}
2315
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002316bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002317 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002318{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002319 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002320 if (!colorbuffer)
2321 {
2322 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002323 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002324 }
2325
2326 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2327 if (!sourceRenderTarget)
2328 {
2329 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002330 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002331 }
2332
2333 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2334 if (!source)
2335 {
2336 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002337 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002338 }
2339
2340 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2341 if (!storage11)
2342 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002343 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002344 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002345 }
2346
2347 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2348 if (!destRenderTarget)
2349 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002350 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002351 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002352 }
2353
2354 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2355 if (!dest)
2356 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002357 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002358 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002359 }
2360
Geoff Langb86b9792013-06-04 16:32:05 -04002361 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2362 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002363
Geoff Langb86b9792013-06-04 16:32:05 -04002364 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2365 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002366
Geoff Langb86b9792013-06-04 16:32:05 -04002367 // Use nearest filtering because source and destination are the same size for the direct
2368 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002369 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002370 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002371
Geoff Lang42477a42013-09-17 17:07:02 -04002372 storage11->invalidateSwizzleCacheLevel(level);
2373
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002374 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002375}
2376
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002377bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002378 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002379{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002380 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002381 if (!colorbuffer)
2382 {
2383 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002384 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002385 }
2386
2387 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2388 if (!sourceRenderTarget)
2389 {
2390 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002391 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002392 }
2393
2394 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2395 if (!source)
2396 {
2397 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002398 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002399 }
2400
2401 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2402 if (!storage11)
2403 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002404 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002405 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002406 }
2407
Nicolas Capensb13f8662013-06-04 13:30:19 -04002408 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002409 if (!destRenderTarget)
2410 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002411 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002412 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002413 }
2414
2415 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2416 if (!dest)
2417 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002418 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002419 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002420 }
2421
Geoff Langb86b9792013-06-04 16:32:05 -04002422 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2423 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002424
Geoff Langb86b9792013-06-04 16:32:05 -04002425 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2426 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002427
Geoff Langb86b9792013-06-04 16:32:05 -04002428 // Use nearest filtering because source and destination are the same size for the direct
2429 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002430 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002431 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002432
Geoff Lang42477a42013-09-17 17:07:02 -04002433 storage11->invalidateSwizzleCacheLevel(level);
2434
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002435 return ret;
2436}
2437
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002438bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2439 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2440{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002441 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002442 if (!colorbuffer)
2443 {
2444 ERR("Failed to retrieve the color buffer from the frame buffer.");
2445 return gl::error(GL_OUT_OF_MEMORY, false);
2446 }
2447
2448 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2449 if (!sourceRenderTarget)
2450 {
2451 ERR("Failed to retrieve the render target from the frame buffer.");
2452 return gl::error(GL_OUT_OF_MEMORY, false);
2453 }
2454
2455 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2456 if (!source)
2457 {
2458 ERR("Failed to retrieve the render target view from the render target.");
2459 return gl::error(GL_OUT_OF_MEMORY, false);
2460 }
2461
2462 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2463 if (!storage11)
2464 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002465 ERR("Failed to retrieve the texture storage from the destination.");
2466 return gl::error(GL_OUT_OF_MEMORY, false);
2467 }
2468
2469 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2470 if (!destRenderTarget)
2471 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002472 ERR("Failed to retrieve the render target from the destination storage.");
2473 return gl::error(GL_OUT_OF_MEMORY, false);
2474 }
2475
2476 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2477 if (!dest)
2478 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002479 ERR("Failed to retrieve the render target view from the destination render target.");
2480 return gl::error(GL_OUT_OF_MEMORY, false);
2481 }
2482
Geoff Langb86b9792013-06-04 16:32:05 -04002483 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2484 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002485
Geoff Langb86b9792013-06-04 16:32:05 -04002486 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2487 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002488
Geoff Langb86b9792013-06-04 16:32:05 -04002489 // Use nearest filtering because source and destination are the same size for the direct
2490 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002491 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002492 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002493
Geoff Lang42477a42013-09-17 17:07:02 -04002494 storage11->invalidateSwizzleCacheLevel(level);
2495
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002496 return ret;
2497}
2498
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002499bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2500 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2501{
Jamie Madill3c7fa222014-06-05 13:08:51 -04002502 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002503 if (!colorbuffer)
2504 {
2505 ERR("Failed to retrieve the color buffer from the frame buffer.");
2506 return gl::error(GL_OUT_OF_MEMORY, false);
2507 }
2508
2509 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2510 if (!sourceRenderTarget)
2511 {
2512 ERR("Failed to retrieve the render target from the frame buffer.");
2513 return gl::error(GL_OUT_OF_MEMORY, false);
2514 }
2515
2516 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2517 if (!source)
2518 {
2519 ERR("Failed to retrieve the render target view from the render target.");
2520 return gl::error(GL_OUT_OF_MEMORY, false);
2521 }
2522
2523 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2524 if (!storage11)
2525 {
Geoff Langea228632013-07-30 15:17:12 -04002526 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002527 ERR("Failed to retrieve the texture storage from the destination.");
2528 return gl::error(GL_OUT_OF_MEMORY, false);
2529 }
2530
2531 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2532 if (!destRenderTarget)
2533 {
Geoff Langea228632013-07-30 15:17:12 -04002534 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002535 ERR("Failed to retrieve the render target from the destination storage.");
2536 return gl::error(GL_OUT_OF_MEMORY, false);
2537 }
2538
2539 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2540 if (!dest)
2541 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002542 ERR("Failed to retrieve the render target view from the destination render target.");
2543 return gl::error(GL_OUT_OF_MEMORY, false);
2544 }
2545
Geoff Langb86b9792013-06-04 16:32:05 -04002546 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2547 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002548
Geoff Langb86b9792013-06-04 16:32:05 -04002549 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2550 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002551
Geoff Langb86b9792013-06-04 16:32:05 -04002552 // Use nearest filtering because source and destination are the same size for the direct
2553 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002554 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002555 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002556
Geoff Lang42477a42013-09-17 17:07:02 -04002557 storage11->invalidateSwizzleCacheLevel(level);
2558
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002559 return ret;
2560}
2561
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002562void Renderer11::unapplyRenderTargets()
2563{
2564 setOneTimeRenderTarget(NULL);
2565}
2566
2567void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2568{
2569 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2570
2571 rtvArray[0] = renderTargetView;
2572
2573 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2574
2575 // Do not preserve the serial for this one-time-use render target
2576 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2577 {
2578 mAppliedRenderTargetSerials[rtIndex] = 0;
2579 }
2580}
2581
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002582RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2583{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002584 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002585 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002586
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002587 if (depth)
2588 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002589 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002590 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002591 swapChain11->getDepthStencilTexture(),
2592 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002593 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002594 }
2595 else
2596 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002597 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002598 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002599 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002600 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002601 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002602 }
2603 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002604}
2605
Geoff Langa2d97f12013-06-11 11:44:02 -04002606RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002607{
Geoff Langa2d97f12013-06-11 11:44:02 -04002608 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002609 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002610}
2611
Geoff Lang48dcae72014-02-05 16:28:24 -05002612ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type,
2613 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2614 bool separatedOutputBuffers)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002615{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002616 ShaderExecutable11 *executable = NULL;
Geoff Lang48dcae72014-02-05 16:28:24 -05002617 HRESULT result;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002618
2619 switch (type)
2620 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002621 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002622 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002623 ID3D11VertexShader *vertexShader = NULL;
2624 ID3D11GeometryShader *streamOutShader = NULL;
2625
2626 result = mDevice->CreateVertexShader(function, length, NULL, &vertexShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002627 ASSERT(SUCCEEDED(result));
2628
Geoff Lang48dcae72014-02-05 16:28:24 -05002629 if (transformFeedbackVaryings.size() > 0)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002630 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002631 std::vector<D3D11_SO_DECLARATION_ENTRY> soDeclaration;
2632 for (size_t i = 0; i < transformFeedbackVaryings.size(); i++)
2633 {
2634 const gl::LinkedVarying &varying = transformFeedbackVaryings[i];
Jamie Madill6195ef82014-06-18 10:09:43 -04002635 GLenum transposedType = gl::TransposeMatrixType(varying.type);
2636
Geoff Langcebb5aa2014-04-07 14:13:40 -04002637 for (size_t j = 0; j < varying.semanticIndexCount; j++)
Geoff Lang48dcae72014-02-05 16:28:24 -05002638 {
2639 D3D11_SO_DECLARATION_ENTRY entry = { 0 };
2640 entry.Stream = 0;
Geoff Langcebb5aa2014-04-07 14:13:40 -04002641 entry.SemanticName = varying.semanticName.c_str();
2642 entry.SemanticIndex = varying.semanticIndex + j;
Geoff Lang48dcae72014-02-05 16:28:24 -05002643 entry.StartComponent = 0;
Jamie Madill6195ef82014-06-18 10:09:43 -04002644 entry.ComponentCount = gl::VariableColumnCount(transposedType);
Geoff Lang48dcae72014-02-05 16:28:24 -05002645 entry.OutputSlot = (separatedOutputBuffers ? i : 0);
2646 soDeclaration.push_back(entry);
2647 }
2648 }
2649
2650 result = mDevice->CreateGeometryShaderWithStreamOutput(function, length, soDeclaration.data(), soDeclaration.size(),
2651 NULL, 0, 0, NULL, &streamOutShader);
2652 ASSERT(SUCCEEDED(result));
2653 }
2654
2655 if (vertexShader)
2656 {
2657 executable = new ShaderExecutable11(function, length, vertexShader, streamOutShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002658 }
2659 }
2660 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002661 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002662 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002663 ID3D11PixelShader *pixelShader = NULL;
2664
2665 result = mDevice->CreatePixelShader(function, length, NULL, &pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002666 ASSERT(SUCCEEDED(result));
2667
Geoff Lang48dcae72014-02-05 16:28:24 -05002668 if (pixelShader)
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002669 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002670 executable = new ShaderExecutable11(function, length, pixelShader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002671 }
2672 }
2673 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002674 case rx::SHADER_GEOMETRY:
2675 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002676 ID3D11GeometryShader *geometryShader = NULL;
2677
2678 result = mDevice->CreateGeometryShader(function, length, NULL, &geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002679 ASSERT(SUCCEEDED(result));
2680
Geoff Lang48dcae72014-02-05 16:28:24 -05002681 if (geometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002682 {
Geoff Lang48dcae72014-02-05 16:28:24 -05002683 executable = new ShaderExecutable11(function, length, geometryShader);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002684 }
2685 }
2686 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002687 default:
2688 UNREACHABLE();
2689 break;
2690 }
2691
2692 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002693}
2694
Geoff Lang48dcae72014-02-05 16:28:24 -05002695ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
2696 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
2697 bool separatedOutputBuffers, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002698{
Geoff Lang6e05c272014-03-17 10:46:54 -07002699 const char *profileType = NULL;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002700 switch (type)
2701 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002702 case rx::SHADER_VERTEX:
Geoff Lang6e05c272014-03-17 10:46:54 -07002703 profileType = "vs";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002704 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002705 case rx::SHADER_PIXEL:
Geoff Lang6e05c272014-03-17 10:46:54 -07002706 profileType = "ps";
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002707 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002708 case rx::SHADER_GEOMETRY:
Geoff Lang6e05c272014-03-17 10:46:54 -07002709 profileType = "gs";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002710 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002711 default:
2712 UNREACHABLE();
2713 return NULL;
2714 }
2715
Geoff Lang6e05c272014-03-17 10:46:54 -07002716 const char *profileVersion = NULL;
2717 switch (mFeatureLevel)
2718 {
2719 case D3D_FEATURE_LEVEL_11_0:
2720 profileVersion = "5_0";
2721 break;
2722 case D3D_FEATURE_LEVEL_10_1:
2723 profileVersion = "4_1";
2724 break;
2725 case D3D_FEATURE_LEVEL_10_0:
2726 profileVersion = "4_0";
2727 break;
2728 default:
2729 UNREACHABLE();
2730 return NULL;
2731 }
2732
2733 char profile[32];
2734 snprintf(profile, ArraySize(profile), "%s_%s", profileType, profileVersion);
2735
Nicolas Capens93faad92014-05-10 12:14:13 -04002736 UINT flags = D3DCOMPILE_OPTIMIZATION_LEVEL0;
2737
2738 if (gl::perfActive())
2739 {
2740#ifndef NDEBUG
2741 flags = D3DCOMPILE_SKIP_OPTIMIZATION;
2742#endif
2743
2744 flags |= D3DCOMPILE_DEBUG;
2745
2746 std::string sourcePath = getTempPath();
2747 std::string sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(shaderHLSL);
2748 writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size());
2749 }
2750
2751 // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options.
2752 // Try the default flags first and if compilation fails, try some alternatives.
2753 const UINT extraFlags[] =
2754 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002755 flags,
Nicolas Capens2c27db62014-05-10 12:21:11 -04002756 flags | D3DCOMPILE_SKIP_VALIDATION,
2757 flags | D3DCOMPILE_SKIP_OPTIMIZATION
Nicolas Capens93faad92014-05-10 12:14:13 -04002758 };
2759
2760 const static char *extraFlagNames[] =
2761 {
Nicolas Capens1408bae2014-05-10 12:18:42 -04002762 "default",
Nicolas Capens2c27db62014-05-10 12:21:11 -04002763 "skip validation",
2764 "skip optimization"
Nicolas Capens93faad92014-05-10 12:14:13 -04002765 };
2766
2767 int attempts = ArraySize(extraFlags);
2768
2769 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, extraFlags, extraFlagNames, attempts);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002770 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002771 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002772 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002773 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002774
Geoff Lang48dcae72014-02-05 16:28:24 -05002775 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type,
2776 transformFeedbackVaryings, separatedOutputBuffers);
Geoff Langea228632013-07-30 15:17:12 -04002777 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002778
2779 return executable;
2780}
2781
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002782rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2783{
2784 return new UniformStorage11(this, storageSize);
2785}
2786
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002787VertexBuffer *Renderer11::createVertexBuffer()
2788{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002789 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002790}
2791
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002792IndexBuffer *Renderer11::createIndexBuffer()
2793{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002794 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002795}
2796
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002797BufferStorage *Renderer11::createBufferStorage()
2798{
2799 return new BufferStorage11(this);
2800}
2801
Brandon Jones5bf98292014-06-06 17:19:38 -07002802VertexArrayImpl *Renderer11::createVertexArray()
2803{
2804 return new VertexArray11(this);
2805}
2806
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002807QueryImpl *Renderer11::createQuery(GLenum type)
2808{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002809 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002810}
2811
2812FenceImpl *Renderer11::createFence()
2813{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002814 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002815}
2816
Geoff Lang005df412013-10-16 14:12:50 -04002817bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002818{
Jamie Madill4461f092013-10-10 15:10:39 -04002819 int clientVersion = getCurrentClientVersion();
2820
2821 // We only support buffer to texture copies in ES3
2822 if (clientVersion <= 2)
2823 {
2824 return false;
2825 }
2826
2827 // sRGB formats do not work with D3D11 buffer SRVs
2828 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2829 {
2830 return false;
2831 }
2832
2833 // We cannot support direct copies to non-color-renderable formats
Geoff Langcec35902014-04-16 10:52:36 -04002834 if (!getCaps().textureCaps.get(internalFormat).colorRendering)
Jamie Madill4461f092013-10-10 15:10:39 -04002835 {
2836 return false;
2837 }
2838
2839 // We skip all 3-channel formats since sometimes format support is missing
2840 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2841 {
2842 return false;
2843 }
2844
2845 // We don't support formats which we can't represent without conversion
2846 if (getNativeTextureFormat(internalFormat) != internalFormat)
2847 {
2848 return false;
2849 }
2850
2851 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002852}
2853
Jamie Madilla21eea32013-09-18 14:36:25 -04002854bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2855 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2856{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002857 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002858 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2859}
2860
Jamie Madill3c7fa222014-06-05 13:08:51 -04002861bool Renderer11::getRenderTargetResource(gl::FramebufferAttachment *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002862{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002863 ASSERT(colorbuffer != NULL);
2864
2865 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2866 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002867 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002868 *subresourceIndex = renderTarget->getSubresourceIndex();
2869
2870 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2871 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002872 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002873 ID3D11Resource *textureResource = NULL;
2874 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002875
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002876 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002877 {
Geoff Lang8e328842014-02-10 13:11:20 -05002878 HRESULT result = textureResource->QueryInterface(__uuidof(ID3D11Texture2D), (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002879 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002880
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002881 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002882 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002883 return true;
2884 }
2885 else
2886 {
2887 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2888 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002889 }
2890 }
2891 }
2892 }
2893
2894 return false;
2895}
2896
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002897bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002898 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002899{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002900 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002901 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002902 gl::FramebufferAttachment *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002903
2904 if (!readBuffer)
2905 {
2906 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2907 return gl::error(GL_OUT_OF_MEMORY, false);
2908 }
2909
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002910 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002911
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002912 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002913 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002914 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2915 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002916 gl::FramebufferAttachment *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002917
2918 if (!drawBuffer)
2919 {
2920 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2921 return gl::error(GL_OUT_OF_MEMORY, false);
2922 }
2923
2924 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2925
Geoff Lang125deab2013-08-09 13:34:16 -04002926 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002927 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002928 {
2929 return false;
2930 }
2931 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002932 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002933 }
2934
Geoff Lang685806d2013-06-12 11:16:36 -04002935 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002936 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04002937 gl::FramebufferAttachment *readBuffer = readTarget->getDepthOrStencilbuffer();
2938 gl::FramebufferAttachment *drawBuffer = drawTarget->getDepthOrStencilbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002939
2940 if (!readBuffer)
2941 {
2942 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2943 return gl::error(GL_OUT_OF_MEMORY, false);
2944 }
2945
2946 if (!drawBuffer)
2947 {
2948 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2949 return gl::error(GL_OUT_OF_MEMORY, false);
2950 }
2951
2952 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2953 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2954
Geoff Lang125deab2013-08-09 13:34:16 -04002955 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002956 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002957 {
2958 return false;
2959 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002960 }
2961
Geoff Lang42477a42013-09-17 17:07:02 -04002962 invalidateFramebufferSwizzles(drawTarget);
2963
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002964 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002965}
2966
Jamie Madilleb9baab2014-03-24 13:19:43 -04002967void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
2968 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002969{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002970 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002971 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002972
Jamie Madill3c7fa222014-06-05 13:08:51 -04002973 gl::FramebufferAttachment *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002974
2975 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002976 {
2977 gl::Rectangle area;
2978 area.x = x;
2979 area.y = y;
2980 area.width = width;
2981 area.height = height;
2982
Jamie Madill1ef6fe72014-05-01 14:51:05 -04002983 if (pack.pixelBuffer.get() != NULL)
2984 {
2985 rx::BufferStorage11 *packBufferStorage = BufferStorage11::makeBufferStorage11(pack.pixelBuffer.get()->getStorage());
2986 PackPixelsParams packParams(area, format, type, outputPitch, pack, reinterpret_cast<ptrdiff_t>(pixels));
2987 packBufferStorage->packPixels(colorBufferTexture, subresourceIndex, packParams);
2988 }
2989 else
2990 {
2991 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch, pack, pixels);
2992 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002993
Geoff Langea228632013-07-30 15:17:12 -04002994 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002995 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002996}
2997
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002998Image *Renderer11::createImage()
2999{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003000 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003001}
3002
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003003void Renderer11::generateMipmap(Image *dest, Image *src)
3004{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003005 Image11 *dest11 = Image11::makeImage11(dest);
3006 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003007 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003008}
3009
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003010TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3011{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003012 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3013 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003014}
3015
Nicolas Capensbf712d02014-03-31 14:23:35 -04003016TextureStorage *Renderer11::createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003017{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003018 return new TextureStorage11_2D(this, internalformat, renderTarget, width, height, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003019}
3020
Nicolas Capensbf712d02014-03-31 14:23:35 -04003021TextureStorage *Renderer11::createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003022{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003023 return new TextureStorage11_Cube(this, internalformat, renderTarget, size, levels);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003024}
3025
Nicolas Capensbf712d02014-03-31 14:23:35 -04003026TextureStorage *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 +00003027{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003028 return new TextureStorage11_3D(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003029}
3030
Nicolas Capensbf712d02014-03-31 14:23:35 -04003031TextureStorage *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 +00003032{
Nicolas Capensbf712d02014-03-31 14:23:35 -04003033 return new TextureStorage11_2DArray(this, internalformat, renderTarget, width, height, depth, levels);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003034}
3035
Jamie Madilleb9baab2014-03-24 13:19:43 -04003036void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format,
3037 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void *pixels)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003038{
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003039 ASSERT(area.width >= 0);
3040 ASSERT(area.height >= 0);
3041
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003042 D3D11_TEXTURE2D_DESC textureDesc;
3043 texture->GetDesc(&textureDesc);
3044
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003045 // Clamp read region to the defined texture boundaries, preventing out of bounds reads
3046 // and reads of uninitialized data.
3047 gl::Rectangle safeArea;
3048 safeArea.x = gl::clamp(area.x, 0, static_cast<int>(textureDesc.Width));
3049 safeArea.y = gl::clamp(area.y, 0, static_cast<int>(textureDesc.Height));
3050 safeArea.width = gl::clamp(area.width + std::min(area.x, 0), 0,
3051 static_cast<int>(textureDesc.Width) - safeArea.x);
3052 safeArea.height = gl::clamp(area.height + std::min(area.y, 0), 0,
3053 static_cast<int>(textureDesc.Height) - safeArea.y);
3054
3055 ASSERT(safeArea.x >= 0 && safeArea.y >= 0);
3056 ASSERT(safeArea.x + safeArea.width <= static_cast<int>(textureDesc.Width));
3057 ASSERT(safeArea.y + safeArea.height <= static_cast<int>(textureDesc.Height));
3058
3059 if (safeArea.width == 0 || safeArea.height == 0)
3060 {
3061 // no work to do
3062 return;
3063 }
3064
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003065 D3D11_TEXTURE2D_DESC stagingDesc;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003066 stagingDesc.Width = safeArea.width;
3067 stagingDesc.Height = safeArea.height;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003068 stagingDesc.MipLevels = 1;
3069 stagingDesc.ArraySize = 1;
3070 stagingDesc.Format = textureDesc.Format;
3071 stagingDesc.SampleDesc.Count = 1;
3072 stagingDesc.SampleDesc.Quality = 0;
3073 stagingDesc.Usage = D3D11_USAGE_STAGING;
3074 stagingDesc.BindFlags = 0;
3075 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3076 stagingDesc.MiscFlags = 0;
3077
3078 ID3D11Texture2D* stagingTex = NULL;
3079 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3080 if (FAILED(result))
3081 {
3082 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3083 return;
3084 }
3085
3086 ID3D11Texture2D* srcTex = NULL;
3087 if (textureDesc.SampleDesc.Count > 1)
3088 {
3089 D3D11_TEXTURE2D_DESC resolveDesc;
3090 resolveDesc.Width = textureDesc.Width;
3091 resolveDesc.Height = textureDesc.Height;
3092 resolveDesc.MipLevels = 1;
3093 resolveDesc.ArraySize = 1;
3094 resolveDesc.Format = textureDesc.Format;
3095 resolveDesc.SampleDesc.Count = 1;
3096 resolveDesc.SampleDesc.Quality = 0;
3097 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3098 resolveDesc.BindFlags = 0;
3099 resolveDesc.CPUAccessFlags = 0;
3100 resolveDesc.MiscFlags = 0;
3101
3102 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3103 if (FAILED(result))
3104 {
3105 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003106 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003107 return;
3108 }
3109
3110 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3111 subResource = 0;
3112 }
3113 else
3114 {
3115 srcTex = texture;
3116 srcTex->AddRef();
3117 }
3118
3119 D3D11_BOX srcBox;
Jamie Madill1eb5bd72014-03-28 10:43:39 -04003120 srcBox.left = static_cast<UINT>(safeArea.x);
3121 srcBox.right = static_cast<UINT>(safeArea.x + safeArea.width);
3122 srcBox.top = static_cast<UINT>(safeArea.y);
3123 srcBox.bottom = static_cast<UINT>(safeArea.y + safeArea.height);
3124 srcBox.front = 0;
3125 srcBox.back = 1;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003126
3127 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3128
Geoff Langea228632013-07-30 15:17:12 -04003129 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003130
Jamie Madill7538f7f2014-04-17 11:53:39 -04003131 PackPixelsParams packParams(safeArea, format, type, outputPitch, pack, 0);
3132 packPixels(stagingTex, packParams, pixels);
3133
3134 SafeRelease(stagingTex);
3135}
3136
3137void Renderer11::packPixels(ID3D11Texture2D *readTexture, const PackPixelsParams &params, void *pixelsOut)
3138{
3139 D3D11_TEXTURE2D_DESC textureDesc;
3140 readTexture->GetDesc(&textureDesc);
3141
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003142 D3D11_MAPPED_SUBRESOURCE mapping;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003143 HRESULT hr = mDeviceContext->Map(readTexture, 0, D3D11_MAP_READ, 0, &mapping);
Geoff Lang9cd19152014-05-28 15:54:34 -04003144 UNUSED_ASSERTION_VARIABLE(hr);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003145 ASSERT(SUCCEEDED(hr));
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003146
3147 unsigned char *source;
3148 int inputPitch;
Jamie Madill7538f7f2014-04-17 11:53:39 -04003149 if (params.pack.reverseRowOrder)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003150 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003151 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (params.area.height - 1);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003152 inputPitch = -static_cast<int>(mapping.RowPitch);
3153 }
3154 else
3155 {
3156 source = static_cast<unsigned char*>(mapping.pData);
3157 inputPitch = static_cast<int>(mapping.RowPitch);
3158 }
3159
Geoff Lang697ad3e2013-06-04 10:11:28 -04003160 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003161
Geoff Lang005df412013-10-16 14:12:50 -04003162 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003163 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3164 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3165
3166 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3167
Jamie Madill7538f7f2014-04-17 11:53:39 -04003168 if (sourceFormat == params.format && sourceType == params.type)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003169 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003170 unsigned char *dest = static_cast<unsigned char*>(pixelsOut) + params.offset;
3171 for (int y = 0; y < params.area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003172 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003173 memcpy(dest + y * params.outputPitch, source + y * inputPitch, params.area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003174 }
3175 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003176 else
3177 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003178 GLenum destInternalFormat = gl::GetSizedInternalFormat(params.format, params.type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003179 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3180
Jamie Madill7538f7f2014-04-17 11:53:39 -04003181 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, params.format, params.type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003182 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003183 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003184 // Fast copy is possible through some special function
Jamie Madill7538f7f2014-04-17 11:53:39 -04003185 for (int y = 0; y < params.area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003186 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003187 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003188 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003189 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003190 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3191
3192 fastCopyFunc(src, dest);
3193 }
3194 }
3195 }
3196 else
3197 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003198 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Jamie Madill7538f7f2014-04-17 11:53:39 -04003199 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(params.format, params.type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003200
3201 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3202 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3203 sizeof(temp) >= sizeof(gl::ColorUI) &&
3204 sizeof(temp) >= sizeof(gl::ColorI));
3205
Jamie Madill7538f7f2014-04-17 11:53:39 -04003206 for (int y = 0; y < params.area.height; y++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003207 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003208 for (int x = 0; x < params.area.width; x++)
Geoff Lang697ad3e2013-06-04 10:11:28 -04003209 {
Jamie Madill7538f7f2014-04-17 11:53:39 -04003210 void *dest = static_cast<unsigned char*>(pixelsOut) + params.offset + y * params.outputPitch + x * destPixelSize;
Geoff Lang697ad3e2013-06-04 10:11:28 -04003211 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3212
3213 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3214 // will not allow the copy otherwise.
3215 readFunc(src, temp);
3216 writeFunc(temp, dest);
3217 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003218 }
3219 }
3220 }
3221
Jamie Madill7538f7f2014-04-17 11:53:39 -04003222 mDeviceContext->Unmap(readTexture, 0);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003223}
3224
Geoff Lang758d5b22013-06-11 11:42:50 -04003225bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003226 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3227 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003228{
Geoff Lang975af372013-06-12 11:19:22 -04003229 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3230 // it should never be the case that both color and depth/stencil need to be blitted at
3231 // at the same time.
3232 ASSERT(colorBlit != (depthBlit || stencilBlit));
3233
Geoff Langc1f51be2013-06-11 11:49:14 -04003234 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003235
Geoff Lang4d782732013-07-22 10:44:18 -04003236 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3237 if (!drawRenderTarget)
3238 {
3239 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3240 return gl::error(GL_OUT_OF_MEMORY, false);
3241 }
3242
3243 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3244 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3245 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3246 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3247
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003248 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3249 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003250 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003251 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003252 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003253 }
3254
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003255 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003256 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003257 unsigned int readSubresource = 0;
3258 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003259 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003260 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3261 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003262
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003263 if (unresolvedTexture)
3264 {
3265 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3266 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003267
Geoff Langea228632013-07-30 15:17:12 -04003268 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003269
Geoff Lang5c9a29a2014-02-11 10:26:24 -05003270 HRESULT hresult = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3271 if (FAILED(hresult))
Geoff Langc1f51be2013-06-11 11:49:14 -04003272 {
Geoff Langea228632013-07-30 15:17:12 -04003273 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003274 return gl::error(GL_OUT_OF_MEMORY, false);
3275 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003276 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003277 }
3278 else
3279 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003280 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003281 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003282 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003283 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003284 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003285 }
3286
Geoff Lang4d782732013-07-22 10:44:18 -04003287 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003288 {
Geoff Lang4d782732013-07-22 10:44:18 -04003289 SafeRelease(readTexture);
3290 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003291 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003292 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003293 }
3294
Geoff Lang125deab2013-08-09 13:34:16 -04003295 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3296 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3297
3298 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3299
3300 bool wholeBufferCopy = !scissorNeeded &&
3301 readRect.x == 0 && readRect.width == readSize.width &&
3302 readRect.y == 0 && readRect.height == readSize.height &&
3303 drawRect.x == 0 && drawRect.width == drawSize.width &&
3304 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003305
Geoff Langc1f51be2013-06-11 11:49:14 -04003306 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003307
Geoff Lang1cd1b212014-02-11 09:42:27 -05003308 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003309
3310 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3311 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3312 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3313 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3314
3315 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3316 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3317 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3318
Geoff Langc1f51be2013-06-11 11:49:14 -04003319 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003320 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3321 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003322 {
Geoff Lang125deab2013-08-09 13:34:16 -04003323 UINT dstX = drawRect.x;
3324 UINT dstY = drawRect.y;
3325
Geoff Langc1f51be2013-06-11 11:49:14 -04003326 D3D11_BOX readBox;
3327 readBox.left = readRect.x;
3328 readBox.right = readRect.x + readRect.width;
3329 readBox.top = readRect.y;
3330 readBox.bottom = readRect.y + readRect.height;
3331 readBox.front = 0;
3332 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003333
Geoff Lang125deab2013-08-09 13:34:16 -04003334 if (scissorNeeded)
3335 {
3336 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3337 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3338
3339 if (drawRect.x < scissor->x)
3340 {
3341 dstX = scissor->x;
3342 readBox.left += (scissor->x - drawRect.x);
3343 }
3344 if (drawRect.y < scissor->y)
3345 {
3346 dstY = scissor->y;
3347 readBox.top += (scissor->y - drawRect.y);
3348 }
3349 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3350 {
3351 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3352 }
3353 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3354 {
3355 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3356 }
3357 }
3358
Geoff Langc1f51be2013-06-11 11:49:14 -04003359 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3360 // We also require complete framebuffer copies for depth-stencil blit.
3361 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003362
Geoff Lang125deab2013-08-09 13:34:16 -04003363 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003364 readTexture, readSubresource, pSrcBox);
3365 result = true;
3366 }
3367 else
3368 {
3369 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003370 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003371
Geoff Lang975af372013-06-12 11:19:22 -04003372 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003373 {
Geoff Lang975af372013-06-12 11:19:22 -04003374 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003375 drawTexture, drawSubresource, drawArea, drawSize,
3376 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003377 }
3378 else if (depthBlit)
3379 {
Geoff Lang125deab2013-08-09 13:34:16 -04003380 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3381 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003382 }
3383 else if (stencilBlit)
3384 {
3385 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003386 drawTexture, drawSubresource, drawArea, drawSize,
3387 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003388 }
3389 else
3390 {
Geoff Lang685806d2013-06-12 11:16:36 -04003391 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003392 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3393 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003394 }
3395 }
3396
3397 SafeRelease(readTexture);
3398 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003399
3400 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003401}
3402
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003403ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3404{
3405 D3D11_TEXTURE2D_DESC textureDesc;
3406 source->GetDesc(&textureDesc);
3407
3408 if (textureDesc.SampleDesc.Count > 1)
3409 {
3410 D3D11_TEXTURE2D_DESC resolveDesc;
3411 resolveDesc.Width = textureDesc.Width;
3412 resolveDesc.Height = textureDesc.Height;
3413 resolveDesc.MipLevels = 1;
3414 resolveDesc.ArraySize = 1;
3415 resolveDesc.Format = textureDesc.Format;
3416 resolveDesc.SampleDesc.Count = 1;
3417 resolveDesc.SampleDesc.Quality = 0;
3418 resolveDesc.Usage = textureDesc.Usage;
3419 resolveDesc.BindFlags = textureDesc.BindFlags;
3420 resolveDesc.CPUAccessFlags = 0;
3421 resolveDesc.MiscFlags = 0;
3422
3423 ID3D11Texture2D *resolveTexture = NULL;
3424 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3425 if (FAILED(result))
3426 {
3427 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3428 return NULL;
3429 }
3430
3431 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3432 return resolveTexture;
3433 }
3434 else
3435 {
3436 source->AddRef();
3437 return source;
3438 }
3439}
3440
Jamie Madill3c7fa222014-06-05 13:08:51 -04003441void Renderer11::invalidateFBOAttachmentSwizzles(gl::FramebufferAttachment *attachment, int mipLevel)
Geoff Lang42477a42013-09-17 17:07:02 -04003442{
Jamie Madill3c7fa222014-06-05 13:08:51 -04003443 ASSERT(attachment->isTexture());
3444 TextureStorage *texStorage = attachment->getTextureStorage();
Geoff Lang42477a42013-09-17 17:07:02 -04003445 if (texStorage)
3446 {
3447 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3448 if (!texStorage11)
3449 {
3450 ERR("texture storage pointer unexpectedly null.");
3451 return;
3452 }
3453
3454 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3455 }
3456}
3457
3458void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3459{
3460 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3461 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003462 gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(colorAttachment);
3463 if (attachment && attachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003464 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003465 invalidateFBOAttachmentSwizzles(attachment, framebuffer->getColorbufferMipLevel(colorAttachment));
Geoff Lang42477a42013-09-17 17:07:02 -04003466 }
3467 }
3468
Jamie Madill3c7fa222014-06-05 13:08:51 -04003469 gl::FramebufferAttachment *depthAttachment = framebuffer->getDepthbuffer();
3470 if (depthAttachment && depthAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003471 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003472 invalidateFBOAttachmentSwizzles(depthAttachment, framebuffer->getDepthbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003473 }
3474
Jamie Madill3c7fa222014-06-05 13:08:51 -04003475 gl::FramebufferAttachment *stencilAttachment = framebuffer->getStencilbuffer();
3476 if (stencilAttachment && stencilAttachment->isTexture())
Geoff Lang42477a42013-09-17 17:07:02 -04003477 {
Jamie Madill3c7fa222014-06-05 13:08:51 -04003478 invalidateFBOAttachmentSwizzles(stencilAttachment, framebuffer->getStencilbufferMipLevel());
Geoff Lang42477a42013-09-17 17:07:02 -04003479 }
3480}
3481
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003482bool Renderer11::getLUID(LUID *adapterLuid) const
3483{
3484 adapterLuid->HighPart = 0;
3485 adapterLuid->LowPart = 0;
3486
3487 if (!mDxgiAdapter)
3488 {
3489 return false;
3490 }
3491
3492 DXGI_ADAPTER_DESC adapterDesc;
3493 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3494 {
3495 return false;
3496 }
3497
3498 *adapterLuid = adapterDesc.AdapterLuid;
3499 return true;
3500}
3501
Geoff Lang005df412013-10-16 14:12:50 -04003502GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003503{
3504 int clientVersion = getCurrentClientVersion();
3505 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3506}
3507
Jamie Madill95ffb862014-01-29 09:26:59 -05003508rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3509{
3510 return gl_d3d11::GetVertexConversionType(vertexFormat);
3511}
3512
3513GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3514{
3515 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3516}
3517
Geoff Lang61e49a52013-05-29 10:22:58 -04003518Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3519{
3520 MultisampleSupportInfo supportInfo = { 0 };
3521
3522 UINT formatSupport;
3523 HRESULT result;
3524
3525 result = mDevice->CheckFormatSupport(format, &formatSupport);
3526 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3527 {
3528 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3529 {
3530 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3531 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3532 {
3533 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3534 }
3535 else
3536 {
3537 supportInfo.qualityLevels[i - 1] = 0;
3538 }
3539 }
3540 }
3541
3542 return supportInfo;
3543}
3544
Geoff Langcec35902014-04-16 10:52:36 -04003545gl::Caps Renderer11::generateCaps() const
3546{
3547 return d3d11_gl::GenerateCaps(mDevice);
3548}
3549
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003550}