blob: 872133393acf8332a1d69ded944dcf56d9a89da4 [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//
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00003// Copyright (c) 2012-2013 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"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000034#include "libEGL/Display.h"
35
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000036#ifdef _DEBUG
37// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
38// and conformance tests. to enable all warnings, remove this define.
39#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
40#endif
41
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000042namespace rx
43{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000044static const DXGI_FORMAT RenderTargetFormats[] =
45 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000046 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000047 DXGI_FORMAT_R8G8B8A8_UNORM
48 };
49
50static const DXGI_FORMAT DepthStencilFormats[] =
51 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000052 DXGI_FORMAT_UNKNOWN,
53 DXGI_FORMAT_D24_UNORM_S8_UINT,
54 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000055 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000056
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000057enum
58{
59 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
60};
61
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000062Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
63{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000064 mVertexDataManager = NULL;
65 mIndexDataManager = NULL;
66
daniel@transgaming.comc5114302012-12-20 21:11:36 +000067 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000068 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000069
Geoff Langb86b9792013-06-04 16:32:05 -040070 mBlit = NULL;
Jamie Madilla21eea32013-09-18 14:36:25 -040071 mPixelTransfer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000072
Geoff Langda507fe2013-08-20 12:01:42 -040073 mClear = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000074
daniel@transgaming.combdf787f2013-02-01 03:20:36 +000075 mSyncQuery = NULL;
76
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000077 mD3d11Module = NULL;
78 mDxgiModule = NULL;
79
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000080 mDeviceLost = false;
81
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +000082 mMaxSupportedSamples = 0;
83
daniel@transgaming.com25072f62012-11-28 19:31:32 +000084 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000085 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000086 mDxgiAdapter = NULL;
87 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +000088
89 mDriverConstantBufferVS = NULL;
90 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +000091
92 mBGRATextureSupport = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +000093
94 mIsGeometryShaderActive = false;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000095}
96
97Renderer11::~Renderer11()
98{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +000099 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000100}
101
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000102Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
103{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000104 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000105 return static_cast<rx::Renderer11*>(renderer);
106}
107
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000108#ifndef __d3d11_1_h__
109#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
110#endif
111
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000112EGLint Renderer11::initialize()
113{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000114 if (!initializeCompiler())
115 {
116 return EGL_NOT_INITIALIZED;
117 }
118
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000119 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
120 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000121
122 if (mD3d11Module == NULL || mDxgiModule == NULL)
123 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000124 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000125 return EGL_NOT_INITIALIZED;
126 }
127
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000128 // create the D3D11 device
129 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000130 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000131
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000132 if (D3D11CreateDevice == NULL)
133 {
134 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
135 return EGL_NOT_INITIALIZED;
136 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000137
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000138 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000139 {
140 D3D_FEATURE_LEVEL_11_0,
141 D3D_FEATURE_LEVEL_10_1,
142 D3D_FEATURE_LEVEL_10_0,
143 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000144
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000145 HRESULT result = S_OK;
146
147#ifdef _DEBUG
148 result = D3D11CreateDevice(NULL,
149 D3D_DRIVER_TYPE_HARDWARE,
150 NULL,
151 D3D11_CREATE_DEVICE_DEBUG,
152 featureLevels,
153 ArraySize(featureLevels),
154 D3D11_SDK_VERSION,
155 &mDevice,
156 &mFeatureLevel,
157 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000158
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000159 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000160 {
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000161 ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n");
162 }
163
164 if (!mDevice || FAILED(result))
165#endif
166 {
167 result = D3D11CreateDevice(NULL,
168 D3D_DRIVER_TYPE_HARDWARE,
169 NULL,
170 0,
171 featureLevels,
172 ArraySize(featureLevels),
173 D3D11_SDK_VERSION,
174 &mDevice,
175 &mFeatureLevel,
176 &mDeviceContext);
177
178 if (!mDevice || FAILED(result))
179 {
180 ERR("Could not create D3D11 device - aborting!\n");
181 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
182 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000183 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000184
185 IDXGIDevice *dxgiDevice = NULL;
186 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
187
188 if (FAILED(result))
189 {
190 ERR("Could not query DXGI device - aborting!\n");
191 return EGL_NOT_INITIALIZED;
192 }
193
194 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
195
196 if (FAILED(result))
197 {
198 ERR("Could not retrieve DXGI adapter - aborting!\n");
199 return EGL_NOT_INITIALIZED;
200 }
201
Geoff Langea228632013-07-30 15:17:12 -0400202 SafeRelease(dxgiDevice);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000203
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000204 mDxgiAdapter->GetDesc(&mAdapterDescription);
205 memset(mDescription, 0, sizeof(mDescription));
206 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
207
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000208 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
209
210 if (!mDxgiFactory || FAILED(result))
211 {
212 ERR("Could not create DXGI factory - aborting!\n");
213 return EGL_NOT_INITIALIZED;
214 }
215
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000216 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
217#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
218 ID3D11InfoQueue *infoQueue;
219 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
220
221 if (SUCCEEDED(result))
222 {
223 D3D11_MESSAGE_ID hideMessages[] =
224 {
225 D3D11_MESSAGE_ID_DEVICE_OMSETRENDERTARGETS_HAZARD,
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000226 D3D11_MESSAGE_ID_DEVICE_PSSETSHADERRESOURCES_HAZARD,
227 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000228 };
229
230 D3D11_INFO_QUEUE_FILTER filter = {0};
231 filter.DenyList.NumIDs = ArraySize(hideMessages);
232 filter.DenyList.pIDList = hideMessages;
233
234 infoQueue->AddStorageFilterEntries(&filter);
Geoff Langea228632013-07-30 15:17:12 -0400235 SafeRelease(infoQueue);
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000236 }
237#endif
238
Geoff Lang61e49a52013-05-29 10:22:58 -0400239 mMaxSupportedSamples = 0;
240
241 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
242 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000243 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400244 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
245 mMultisampleSupportMap.insert(std::make_pair(*i, support));
246 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000247 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000248
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000249 initializeDevice();
250
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000251 // BGRA texture support is optional in feature levels 10 and 10_1
252 UINT formatSupport;
253 result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport);
254 if (FAILED(result))
255 {
256 ERR("Error checking BGRA format support: 0x%08X", result);
257 }
258 else
259 {
260 const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET);
261 mBGRATextureSupport = (formatSupport & flags) == flags;
262 }
263
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000264 // Check floating point texture support
265 static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE;
266 static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET;
267 static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE;
268
269 DXGI_FORMAT float16Formats[] =
270 {
271 DXGI_FORMAT_R16_FLOAT,
272 DXGI_FORMAT_R16G16_FLOAT,
273 DXGI_FORMAT_R16G16B16A16_FLOAT,
274 };
275
276 DXGI_FORMAT float32Formats[] =
277 {
278 DXGI_FORMAT_R32_FLOAT,
279 DXGI_FORMAT_R32G32_FLOAT,
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000280 DXGI_FORMAT_R32G32B32A32_FLOAT,
281 };
282
283 mFloat16TextureSupport = true;
284 mFloat16FilterSupport = true;
285 mFloat16RenderSupport = true;
286 for (unsigned int i = 0; i < ArraySize(float16Formats); i++)
287 {
288 if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport)))
289 {
290 mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
291 mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
292 mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
293 }
294 else
295 {
296 mFloat16TextureSupport = false;
297 mFloat16RenderSupport = false;
298 mFloat16FilterSupport = false;
299 }
300 }
301
302 mFloat32TextureSupport = true;
303 mFloat32FilterSupport = true;
304 mFloat32RenderSupport = true;
305 for (unsigned int i = 0; i < ArraySize(float32Formats); i++)
306 {
307 if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport)))
308 {
309 mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
310 mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
311 mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
312 }
313 else
314 {
315 mFloat32TextureSupport = false;
316 mFloat32FilterSupport = false;
317 mFloat32RenderSupport = false;
318 }
319 }
320
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +0000321 // Check compressed texture support
322 const unsigned int requiredCompressedTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D;
323
324 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &formatSupport)))
325 {
326 mDXT1TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
327 }
328 else
329 {
330 mDXT1TextureSupport = false;
331 }
332
333 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport)))
334 {
335 mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
336 }
337 else
338 {
339 mDXT3TextureSupport = false;
340 }
341
342 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC5_UNORM, &formatSupport)))
343 {
344 mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
345 }
346 else
347 {
348 mDXT5TextureSupport = false;
349 }
350
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +0000351 // Check depth texture support
352 DXGI_FORMAT depthTextureFormats[] =
353 {
354 DXGI_FORMAT_D16_UNORM,
355 DXGI_FORMAT_D24_UNORM_S8_UINT,
356 };
357
358 static const unsigned int requiredDepthTextureFlags = D3D11_FORMAT_SUPPORT_DEPTH_STENCIL |
359 D3D11_FORMAT_SUPPORT_TEXTURE2D;
360
361 mDepthTextureSupport = true;
362 for (unsigned int i = 0; i < ArraySize(depthTextureFormats); i++)
363 {
364 if (SUCCEEDED(mDevice->CheckFormatSupport(depthTextureFormats[i], &formatSupport)))
365 {
366 mDepthTextureSupport = mDepthTextureSupport && ((formatSupport & requiredDepthTextureFlags) == requiredDepthTextureFlags);
367 }
368 else
369 {
370 mDepthTextureSupport = false;
371 }
372 }
373
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000374 return EGL_SUCCESS;
375}
376
377// do any one-time device initialization
378// NOTE: this is also needed after a device lost/reset
379// to reset the scene status and ensure the default states are reset.
380void Renderer11::initializeDevice()
381{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000382 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000383 mInputLayoutCache.initialize(mDevice, mDeviceContext);
384
385 ASSERT(!mVertexDataManager && !mIndexDataManager);
386 mVertexDataManager = new VertexDataManager(this);
387 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000388
Geoff Langb86b9792013-06-04 16:32:05 -0400389 ASSERT(!mBlit);
390 mBlit = new Blit11(this);
391
Geoff Langda507fe2013-08-20 12:01:42 -0400392 ASSERT(!mClear);
393 mClear = new Clear11(this);
394
Jamie Madilla21eea32013-09-18 14:36:25 -0400395 ASSERT(!mPixelTransfer);
396 mPixelTransfer = new PixelTransfer11(this);
397
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000398 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000399}
400
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000401int Renderer11::generateConfigs(ConfigDesc **configDescList)
402{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000403 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
404 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000405 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
406 int numConfigs = 0;
407
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000408 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000409 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000410 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000411 {
412 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
413
414 UINT formatSupport = 0;
415 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000416
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000417 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
418 {
419 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
420
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000421 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000422
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000423 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
424 {
425 UINT formatSupport = 0;
426 result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
427 depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
428 }
429
430 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000431 {
Jamie Madilld6cb2442013-07-10 15:13:38 -0400432 // FIXME: parse types from context version
433 ASSERT(d3d11_gl::GetInternalFormat(renderTargetFormat, 2) == d3d11_gl::GetInternalFormat(renderTargetFormat, 3));
434 ASSERT(d3d11_gl::GetInternalFormat(depthStencilFormat, 2) == d3d11_gl::GetInternalFormat(depthStencilFormat, 3));
435
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000436 ConfigDesc newConfig;
Jamie Madilld6cb2442013-07-10 15:13:38 -0400437 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat, getCurrentClientVersion());
438 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat, getCurrentClientVersion());
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000439 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
440 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000441 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000442
443 (*configDescList)[numConfigs++] = newConfig;
444 }
445 }
446 }
447 }
448
449 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000450}
451
452void Renderer11::deleteConfigs(ConfigDesc *configDescList)
453{
454 delete [] (configDescList);
455}
456
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000457void Renderer11::sync(bool block)
458{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000459 if (block)
460 {
461 HRESULT result;
462
463 if (!mSyncQuery)
464 {
465 D3D11_QUERY_DESC queryDesc;
466 queryDesc.Query = D3D11_QUERY_EVENT;
467 queryDesc.MiscFlags = 0;
468
469 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
470 ASSERT(SUCCEEDED(result));
471 }
472
473 mDeviceContext->End(mSyncQuery);
474 mDeviceContext->Flush();
475
476 do
477 {
478 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
479
480 // Keep polling, but allow other threads to do something useful first
481 Sleep(0);
482
483 if (testDeviceLost(true))
484 {
485 return;
486 }
487 }
488 while (result == S_FALSE);
489 }
490 else
491 {
492 mDeviceContext->Flush();
493 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000494}
495
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000496SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
497{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000498 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000499}
500
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000501void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
502{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000503 if (type == gl::SAMPLER_PIXEL)
504 {
505 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
506 {
507 ERR("Pixel shader sampler index %i is not valid.", index);
508 return;
509 }
510
511 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
512 {
513 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
514
515 if (!dxSamplerState)
516 {
517 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
518 "sampler state for pixel shaders at slot %i.", index);
519 }
520
521 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
522
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000523 mCurPixelSamplerStates[index] = samplerState;
524 }
525
526 mForceSetPixelSamplerStates[index] = false;
527 }
528 else if (type == gl::SAMPLER_VERTEX)
529 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000530 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000531 {
532 ERR("Vertex shader sampler index %i is not valid.", index);
533 return;
534 }
535
536 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
537 {
538 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
539
540 if (!dxSamplerState)
541 {
542 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
543 "sampler state for vertex shaders at slot %i.", index);
544 }
545
546 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
547
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000548 mCurVertexSamplerStates[index] = samplerState;
549 }
550
551 mForceSetVertexSamplerStates[index] = false;
552 }
553 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000554}
555
556void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
557{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000558 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000559 unsigned int serial = 0;
560 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000561
562 if (texture)
563 {
564 TextureStorageInterface *texStorage = texture->getNativeTexture();
565 if (texStorage)
566 {
567 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
568 textureSRV = storage11->getSRV();
569 }
570
571 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
572 // missing the shader resource view
573 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000574
575 serial = texture->getTextureSerial();
576 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000577 }
578
579 if (type == gl::SAMPLER_PIXEL)
580 {
581 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
582 {
583 ERR("Pixel shader sampler index %i is not valid.", index);
584 return;
585 }
586
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000587 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
588 {
589 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
590 }
591
592 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000593 }
594 else if (type == gl::SAMPLER_VERTEX)
595 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000596 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000597 {
598 ERR("Vertex shader sampler index %i is not valid.", index);
599 return;
600 }
601
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000602 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
603 {
604 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
605 }
606
607 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000608 }
609 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000610}
611
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000612bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
613{
614 // convert buffers to ID3D11Buffer*
615 ID3D11Buffer *vertexConstantBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = { NULL };
616 ID3D11Buffer *pixelConstantBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = { NULL };
617
618 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
619 {
620 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
621 if (uniformBuffer)
622 {
623 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
624 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(GL_UNIFORM_BUFFER);
625
626 if (!constantBuffer)
627 {
628 return false;
629 }
630
Geoff Langc6354ee2013-07-22 10:40:07 -0400631 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
632 {
633 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
634 1, &constantBuffer);
635 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
636 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000637 }
638 }
639
640 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
641 {
642 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
643 if (uniformBuffer)
644 {
645 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
646 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(GL_UNIFORM_BUFFER);
647
648 if (!constantBuffer)
649 {
650 return false;
651 }
652
Geoff Langc6354ee2013-07-22 10:40:07 -0400653 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
654 {
655 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
656 1, &constantBuffer);
657 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
658 }
659
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000660 pixelConstantBuffers[uniformBufferIndex] = constantBuffer;
661 }
662 }
663
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000664 return true;
665}
666
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000667void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000668{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000669 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000670 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000671 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
672 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000673 if (!dxRasterState)
674 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000675 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000676 "rasterizer state.");
677 }
678
679 mDeviceContext->RSSetState(dxRasterState);
680
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000681 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000682 }
683
684 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000685}
686
Geoff Lang2a64ee42013-05-31 11:22:40 -0400687void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000688 unsigned int sampleMask)
689{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000690 if (mForceSetBlendState ||
691 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400692 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000693 sampleMask != mCurSampleMask)
694 {
695 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
696 if (!dxBlendState)
697 {
698 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
699 "blend state.");
700 }
701
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000702 float blendColors[4] = {0.0f};
703 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
704 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
705 {
706 blendColors[0] = blendColor.red;
707 blendColors[1] = blendColor.green;
708 blendColors[2] = blendColor.blue;
709 blendColors[3] = blendColor.alpha;
710 }
711 else
712 {
713 blendColors[0] = blendColor.alpha;
714 blendColors[1] = blendColor.alpha;
715 blendColors[2] = blendColor.alpha;
716 blendColors[3] = blendColor.alpha;
717 }
718
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000719 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
720
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000721 mCurBlendState = blendState;
722 mCurBlendColor = blendColor;
723 mCurSampleMask = sampleMask;
724 }
725
726 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000727}
728
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000729void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000730 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000731{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000732 if (mForceSetDepthStencilState ||
733 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
734 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
735 {
736 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
737 stencilRef != stencilBackRef ||
738 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
739 {
740 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
741 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000742 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000743 }
744
745 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
746 if (!dxDepthStencilState)
747 {
748 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
749 "setting the default depth stencil state.");
750 }
751
752 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
753
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000754 mCurDepthStencilState = depthStencilState;
755 mCurStencilRef = stencilRef;
756 mCurStencilBackRef = stencilBackRef;
757 }
758
759 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000760}
761
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000762void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000763{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000764 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
765 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000766 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000767 if (enabled)
768 {
769 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000770 rect.left = std::max(0, scissor.x);
771 rect.top = std::max(0, scissor.y);
772 rect.right = scissor.x + std::max(0, scissor.width);
773 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000774
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000775 mDeviceContext->RSSetScissorRects(1, &rect);
776 }
777
778 if (enabled != mScissorEnabled)
779 {
780 mForceSetRasterState = true;
781 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000782
783 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000784 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000785 }
786
787 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000788}
789
daniel@transgaming.com12985182012-12-20 20:56:31 +0000790bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000791 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000792{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000793 gl::Rectangle actualViewport = viewport;
794 float actualZNear = gl::clamp01(zNear);
795 float actualZFar = gl::clamp01(zFar);
796 if (ignoreViewport)
797 {
798 actualViewport.x = 0;
799 actualViewport.y = 0;
800 actualViewport.width = mRenderTargetDesc.width;
801 actualViewport.height = mRenderTargetDesc.height;
802 actualZNear = 0.0f;
803 actualZFar = 1.0f;
804 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000805
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000806 // Get D3D viewport bounds, which depends on the feature level
807 const Range& viewportBounds = getViewportBounds();
808
809 // 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 +0000810 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000811 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
812 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
813 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
814 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
815 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
816 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000817 dxViewport.MinDepth = actualZNear;
818 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000819
820 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
821 {
822 return false; // Nothing to render
823 }
824
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000825 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
826 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000827
daniel@transgaming.com53670042012-11-28 20:55:51 +0000828 if (viewportChanged)
829 {
830 mDeviceContext->RSSetViewports(1, &dxViewport);
831
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000832 mCurViewport = actualViewport;
833 mCurNear = actualZNear;
834 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000835
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000836 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
837 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
838 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
839 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000840
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000841 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
842 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000843
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000844 mVertexConstants.depthRange[0] = actualZNear;
845 mVertexConstants.depthRange[1] = actualZFar;
846 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000847
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000848 mPixelConstants.depthRange[0] = actualZNear;
849 mPixelConstants.depthRange[1] = actualZFar;
850 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000851 }
852
853 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000854 return true;
855}
856
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000857bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
858{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000859 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000860
Geoff Lang57e713e2013-07-31 17:01:58 -0400861 GLsizei minCount = 0;
862
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000863 switch (mode)
864 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400865 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
866 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
867 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
868 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
869 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
870 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000871 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400872 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000873 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000874 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000875 }
876
Geoff Lang4c095862013-07-22 10:43:36 -0400877 if (primitiveTopology != mCurrentPrimitiveTopology)
878 {
879 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
880 mCurrentPrimitiveTopology = primitiveTopology;
881 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000882
Geoff Lang57e713e2013-07-31 17:01:58 -0400883 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000884}
885
886bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000887{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000888 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000889 // Also extract the render target dimensions and view
890 unsigned int renderTargetWidth = 0;
891 unsigned int renderTargetHeight = 0;
892 GLenum renderTargetFormat = 0;
893 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
894 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
895 bool missingColorRenderTarget = true;
896
897 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000898 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000899 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
900
901 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000902 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000903 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
904 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
905
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000906 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000907
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000908 if (!colorbuffer)
909 {
910 ERR("render target pointer unexpectedly null.");
911 return false;
912 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000913
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000914 // check for zero-sized default framebuffer, which is a special case.
915 // in this case we do not wish to modify any state and just silently return false.
916 // this will not report any gl error but will cause the calling method to return.
917 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
918 {
919 return false;
920 }
921
922 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
923
924 // Extract the render target dimensions and view
925 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
926 if (!renderTarget)
927 {
928 ERR("render target pointer unexpectedly null.");
929 return false;
930 }
931
932 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
933 if (!framebufferRTVs[colorAttachment])
934 {
935 ERR("render target view pointer unexpectedly null.");
936 return false;
937 }
938
939 if (missingColorRenderTarget)
940 {
941 renderTargetWidth = colorbuffer->getWidth();
942 renderTargetHeight = colorbuffer->getHeight();
943 renderTargetFormat = colorbuffer->getActualFormat();
944 missingColorRenderTarget = false;
945 }
946 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000947 }
948
949 // Get the depth stencil render buffer and serials
950 gl::Renderbuffer *depthStencil = NULL;
951 unsigned int depthbufferSerial = 0;
952 unsigned int stencilbufferSerial = 0;
953 if (framebuffer->getDepthbufferType() != GL_NONE)
954 {
955 depthStencil = framebuffer->getDepthbuffer();
956 if (!depthStencil)
957 {
958 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000959 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000960 return false;
961 }
962
963 depthbufferSerial = depthStencil->getSerial();
964 }
965 else if (framebuffer->getStencilbufferType() != GL_NONE)
966 {
967 depthStencil = framebuffer->getStencilbuffer();
968 if (!depthStencil)
969 {
970 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000971 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000972 return false;
973 }
974
975 stencilbufferSerial = depthStencil->getSerial();
976 }
977
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000978 // Extract the depth stencil sizes and view
979 unsigned int depthSize = 0;
980 unsigned int stencilSize = 0;
981 ID3D11DepthStencilView* framebufferDSV = NULL;
982 if (depthStencil)
983 {
984 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
985 if (!depthStencilRenderTarget)
986 {
987 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000988 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000989 return false;
990 }
991
992 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
993 if (!framebufferDSV)
994 {
995 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000996 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000997 return false;
998 }
999
1000 // If there is no render buffer, the width, height and format values come from
1001 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001002 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001003 {
1004 renderTargetWidth = depthStencil->getWidth();
1005 renderTargetHeight = depthStencil->getHeight();
1006 renderTargetFormat = depthStencil->getActualFormat();
1007 }
1008
1009 depthSize = depthStencil->getDepthSize();
1010 stencilSize = depthStencil->getStencilSize();
1011 }
1012
1013 // Apply the render target and depth stencil
1014 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001015 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001016 depthbufferSerial != mAppliedDepthbufferSerial ||
1017 stencilbufferSerial != mAppliedStencilbufferSerial)
1018 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001019 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001020
1021 mRenderTargetDesc.width = renderTargetWidth;
1022 mRenderTargetDesc.height = renderTargetHeight;
1023 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001024 mForceSetViewport = true;
1025 mForceSetScissor = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001026
1027 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1028 {
1029 mCurDepthSize = depthSize;
1030 mForceSetRasterState = true;
1031 }
1032
1033 mCurStencilSize = stencilSize;
1034
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001035 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1036 {
1037 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
1038 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001039 mAppliedDepthbufferSerial = depthbufferSerial;
1040 mAppliedStencilbufferSerial = stencilbufferSerial;
1041 mRenderTargetDescInitialized = true;
1042 mDepthStencilInitialized = true;
1043 }
1044
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001045 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001046}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001047
Jamie Madill57a89722013-07-02 11:57:03 -04001048GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001049 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001050{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001051 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001052 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001053 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001054 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001055 return err;
1056 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001057
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001058 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001059}
1060
daniel@transgaming.com31240482012-11-28 21:06:41 +00001061GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001062{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001063 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001064
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001065 if (err == GL_NO_ERROR)
1066 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001067 if (indexInfo->storage)
1068 {
1069 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1070 {
1071 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1072 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1073
shannonwoods@chromium.org675526e2013-05-30 00:04:49 +00001074 mDeviceContext->IASetIndexBuffer(storage->getBuffer(GL_ELEMENT_ARRAY_BUFFER), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001075
1076 mAppliedIBSerial = 0;
1077 mAppliedStorageIBSerial = storage->getSerial();
1078 mAppliedIBOffset = indexInfo->startOffset;
1079 }
1080 }
1081 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001082 {
1083 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1084
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001085 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001086
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001087 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001088 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001089 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001090 }
1091 }
1092
1093 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001094}
1095
1096void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1097{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001098 if (mode == GL_LINE_LOOP)
1099 {
1100 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1101 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001102 else if (mode == GL_TRIANGLE_FAN)
1103 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001104 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001105 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001106 else if (instances > 0)
1107 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001108 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001109 }
1110 else
1111 {
1112 mDeviceContext->Draw(count, 0);
1113 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001114}
1115
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001116void Renderer11::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001117{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001118 if (mode == GL_LINE_LOOP)
1119 {
1120 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1121 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001122 else if (mode == GL_TRIANGLE_FAN)
1123 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001124 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1125 }
1126 else if (instances > 0)
1127 {
1128 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001129 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001130 else
1131 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001132 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001133 }
1134}
1135
1136void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1137{
1138 // Get the raw indices for an indexed draw
1139 if (type != GL_NONE && elementArrayBuffer)
1140 {
1141 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001142 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001143 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001144 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001145 }
1146
1147 if (!mLineLoopIB)
1148 {
1149 mLineLoopIB = new StreamingIndexBufferInterface(this);
1150 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1151 {
1152 delete mLineLoopIB;
1153 mLineLoopIB = NULL;
1154
1155 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001156 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001157 }
1158 }
1159
Geoff Lang57e713e2013-07-31 17:01:58 -04001160 // Checked by Renderer11::applyPrimitiveType
1161 ASSERT(count >= 0);
1162
1163 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001164 {
1165 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1166 return gl::error(GL_OUT_OF_MEMORY);
1167 }
1168
Geoff Lang57e713e2013-07-31 17:01:58 -04001169 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001170 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1171 {
1172 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001173 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001174 }
1175
1176 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001177 unsigned int offset;
1178 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001179 {
1180 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001181 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001182 }
1183
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001184 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001185 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001186
1187 switch (type)
1188 {
1189 case GL_NONE: // Non-indexed draw
1190 for (int i = 0; i < count; i++)
1191 {
1192 data[i] = i;
1193 }
1194 data[count] = 0;
1195 break;
1196 case GL_UNSIGNED_BYTE:
1197 for (int i = 0; i < count; i++)
1198 {
1199 data[i] = static_cast<const GLubyte*>(indices)[i];
1200 }
1201 data[count] = static_cast<const GLubyte*>(indices)[0];
1202 break;
1203 case GL_UNSIGNED_SHORT:
1204 for (int i = 0; i < count; i++)
1205 {
1206 data[i] = static_cast<const GLushort*>(indices)[i];
1207 }
1208 data[count] = static_cast<const GLushort*>(indices)[0];
1209 break;
1210 case GL_UNSIGNED_INT:
1211 for (int i = 0; i < count; i++)
1212 {
1213 data[i] = static_cast<const GLuint*>(indices)[i];
1214 }
1215 data[count] = static_cast<const GLuint*>(indices)[0];
1216 break;
1217 default: UNREACHABLE();
1218 }
1219
1220 if (!mLineLoopIB->unmapBuffer())
1221 {
1222 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001223 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001224 }
1225
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001226 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001227 {
1228 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1229
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001230 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001231 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001232 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001233 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001234 }
1235
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001236 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001237}
1238
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001239void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001240{
1241 // Get the raw indices for an indexed draw
1242 if (type != GL_NONE && elementArrayBuffer)
1243 {
1244 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001245 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001246 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001247 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001248 }
1249
1250 if (!mTriangleFanIB)
1251 {
1252 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1253 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1254 {
1255 delete mTriangleFanIB;
1256 mTriangleFanIB = NULL;
1257
1258 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001259 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001260 }
1261 }
1262
Geoff Lang57e713e2013-07-31 17:01:58 -04001263 // Checked by Renderer11::applyPrimitiveType
1264 ASSERT(count >= 3);
1265
Geoff Langeadfd572013-07-09 15:55:07 -04001266 const unsigned int numTris = count - 2;
1267
Geoff Lang57e713e2013-07-31 17:01:58 -04001268 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001269 {
1270 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1271 return gl::error(GL_OUT_OF_MEMORY);
1272 }
1273
1274 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001275 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1276 {
1277 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001278 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001279 }
1280
1281 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001282 unsigned int offset;
1283 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001284 {
1285 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001286 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001287 }
1288
1289 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001290 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001291
1292 switch (type)
1293 {
1294 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001295 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001296 {
1297 data[i*3 + 0] = 0;
1298 data[i*3 + 1] = i + 1;
1299 data[i*3 + 2] = i + 2;
1300 }
1301 break;
1302 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001303 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001304 {
1305 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1306 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1307 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1308 }
1309 break;
1310 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001311 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001312 {
1313 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1314 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1315 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1316 }
1317 break;
1318 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001319 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001320 {
1321 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1322 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1323 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1324 }
1325 break;
1326 default: UNREACHABLE();
1327 }
1328
1329 if (!mTriangleFanIB->unmapBuffer())
1330 {
1331 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001332 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001333 }
1334
1335 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1336 {
1337 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1338
1339 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1340 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001341 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001342 mAppliedIBOffset = indexBufferOffset;
1343 }
1344
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001345 if (instances > 0)
1346 {
1347 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1348 }
1349 else
1350 {
1351 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1352 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001353}
1354
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001355void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1356{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001357 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001358 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1359
1360 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001361 {
1362 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1363 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001364
daniel@transgaming.come4991412012-12-20 20:55:34 +00001365 ID3D11VertexShader *vertexShader = NULL;
1366 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001367
daniel@transgaming.come4991412012-12-20 20:55:34 +00001368 ID3D11PixelShader *pixelShader = NULL;
1369 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001370
daniel@transgaming.come4991412012-12-20 20:55:34 +00001371 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1372 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001373
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001374 programBinary->dirtyAllUniforms();
1375
1376 mAppliedProgramBinarySerial = programBinarySerial;
1377 }
1378
1379 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001380 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001381
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001382 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001383 {
1384 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001385 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001386 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1387 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001388 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1389 }
1390 else
1391 {
1392 mDeviceContext->GSSetShader(NULL, NULL, 0);
1393 }
1394
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001395 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001396 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001397}
1398
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001399void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001400{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001401 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1402 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001403
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001404 unsigned int totalRegisterCountVS = 0;
1405 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001406
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001407 bool vertexUniformsDirty = false;
1408 bool pixelUniformsDirty = false;
1409
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001410 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1411 {
1412 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001413
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001414 if (uniform->isReferencedByVertexShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001415 {
1416 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001417 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001418 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001419
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001420 if (uniform->isReferencedByFragmentShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001421 {
1422 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001423 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001424 }
1425 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001426
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001427 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1428 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1429
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001430 float (*mapVS)[4] = NULL;
1431 float (*mapPS)[4] = NULL;
1432
Shannon Woods5ab33c82013-06-26 15:31:09 -04001433 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1434 {
1435 D3D11_MAPPED_SUBRESOURCE map = {0};
1436 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1437 ASSERT(SUCCEEDED(result));
1438 mapVS = (float(*)[4])map.pData;
1439 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001440
Shannon Woods5ab33c82013-06-26 15:31:09 -04001441 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1442 {
1443 D3D11_MAPPED_SUBRESOURCE map = {0};
1444 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1445 ASSERT(SUCCEEDED(result));
1446 mapPS = (float(*)[4])map.pData;
1447 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001448
Jamie Madill5b085dc2013-08-30 13:21:11 -04001449 for (size_t uniformIndex = 0; uniformIndex < uniformArray->size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001450 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001451 gl::Uniform *uniform = (*uniformArray)[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001452
Nicolas Capense6050882013-07-08 10:43:10 -04001453 if (!gl::IsSampler(uniform->type))
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001454 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001455 unsigned int componentCount = (4 - uniform->registerElement);
1456
Jamie Madill71cc91f2013-09-18 12:51:22 -04001457 // 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 -04001458 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001459
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001460 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001461 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001462 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001463 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001464
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001465 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001466 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001467 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001468 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001469 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001470
1471 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001472 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001473
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001474 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001475 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001476 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001477 }
1478
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001479 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001480 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001481 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001482 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001483
1484 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1485 {
1486 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1487 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1488 }
1489
1490 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1491 {
1492 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1493 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1494 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001495
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001496 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001497 if (!mDriverConstantBufferVS)
1498 {
1499 D3D11_BUFFER_DESC constantBufferDescription = {0};
1500 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1501 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1502 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1503 constantBufferDescription.CPUAccessFlags = 0;
1504 constantBufferDescription.MiscFlags = 0;
1505 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001506
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001507 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001508 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001509
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001510 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1511 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001512
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001513 if (!mDriverConstantBufferPS)
1514 {
1515 D3D11_BUFFER_DESC constantBufferDescription = {0};
1516 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1517 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1518 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1519 constantBufferDescription.CPUAccessFlags = 0;
1520 constantBufferDescription.MiscFlags = 0;
1521 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001522
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001523 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001524 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001525
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001526 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1527 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001528
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001529 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1530 {
1531 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1532 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1533 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001534
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001535 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1536 {
1537 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1538 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1539 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001540
1541 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001542 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1543 {
1544 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1545 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1546 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001547}
1548
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001549void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001550{
Geoff Langda507fe2013-08-20 12:01:42 -04001551 mClear->clearFramebuffer(clearParams, frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001552}
1553
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001554void Renderer11::markAllStateDirty()
1555{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001556 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1557 {
1558 mAppliedRenderTargetSerials[rtIndex] = 0;
1559 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001560 mAppliedDepthbufferSerial = 0;
1561 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001562 mDepthStencilInitialized = false;
1563 mRenderTargetDescInitialized = false;
1564
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001565 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001566 {
1567 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001568 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001569 }
1570 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1571 {
1572 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001573 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001574 }
1575
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001576 mForceSetBlendState = true;
1577 mForceSetRasterState = true;
1578 mForceSetDepthStencilState = true;
1579 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001580 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001581
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001582 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001583 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001584 mAppliedIBOffset = 0;
1585
daniel@transgaming.come4991412012-12-20 20:55:34 +00001586 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001587 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1588 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001589
1590 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001591
1592 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1593 {
1594 mCurrentConstantBufferVS[i] = -1;
1595 mCurrentConstantBufferPS[i] = -1;
1596 }
1597
1598 mCurrentVertexConstantBuffer = NULL;
1599 mCurrentPixelConstantBuffer = NULL;
1600 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001601
1602 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001603}
1604
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001605void Renderer11::releaseDeviceResources()
1606{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001607 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001608 mInputLayoutCache.clear();
1609
Geoff Langea228632013-07-30 15:17:12 -04001610 SafeDelete(mVertexDataManager);
1611 SafeDelete(mIndexDataManager);
1612 SafeDelete(mLineLoopIB);
1613 SafeDelete(mTriangleFanIB);
1614 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001615 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001616 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001617
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001618 SafeRelease(mDriverConstantBufferVS);
1619 SafeRelease(mDriverConstantBufferPS);
1620 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001621}
1622
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001623void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001624{
1625 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001626 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001627}
1628
1629bool Renderer11::isDeviceLost()
1630{
1631 return mDeviceLost;
1632}
1633
1634// set notify to true to broadcast a message to all contexts of the device loss
1635bool Renderer11::testDeviceLost(bool notify)
1636{
1637 bool isLost = false;
1638
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001639 // GetRemovedReason is used to test if the device is removed
1640 HRESULT result = mDevice->GetDeviceRemovedReason();
1641 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001642
1643 if (isLost)
1644 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001645 // Log error if this is a new device lost event
1646 if (mDeviceLost == false)
1647 {
1648 ERR("The D3D11 device was removed: 0x%08X", result);
1649 }
1650
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001651 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001652 // we'll probably get this done again by notifyDeviceLost
1653 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001654 // Note that we don't want to clear the device loss status here
1655 // -- this needs to be done by resetDevice
1656 mDeviceLost = true;
1657 if (notify)
1658 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001659 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001660 }
1661 }
1662
1663 return isLost;
1664}
1665
1666bool Renderer11::testDeviceResettable()
1667{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001668 // determine if the device is resettable by creating a dummy device
1669 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001670
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001671 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001672 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001673 return false;
1674 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001675
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001676 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001677 {
1678 D3D_FEATURE_LEVEL_11_0,
1679 D3D_FEATURE_LEVEL_10_1,
1680 D3D_FEATURE_LEVEL_10_0,
1681 };
1682
1683 ID3D11Device* dummyDevice;
1684 D3D_FEATURE_LEVEL dummyFeatureLevel;
1685 ID3D11DeviceContext* dummyContext;
1686
1687 HRESULT result = D3D11CreateDevice(NULL,
1688 D3D_DRIVER_TYPE_HARDWARE,
1689 NULL,
1690 #if defined(_DEBUG)
1691 D3D11_CREATE_DEVICE_DEBUG,
1692 #else
1693 0,
1694 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001695 featureLevels,
1696 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001697 D3D11_SDK_VERSION,
1698 &dummyDevice,
1699 &dummyFeatureLevel,
1700 &dummyContext);
1701
1702 if (!mDevice || FAILED(result))
1703 {
1704 return false;
1705 }
1706
Geoff Langea228632013-07-30 15:17:12 -04001707 SafeRelease(dummyContext);
1708 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001709
1710 return true;
1711}
1712
1713void Renderer11::release()
1714{
1715 releaseDeviceResources();
1716
Geoff Langea228632013-07-30 15:17:12 -04001717 SafeRelease(mDxgiFactory);
1718 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001719
1720 if (mDeviceContext)
1721 {
1722 mDeviceContext->ClearState();
1723 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001724 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001725 }
1726
Geoff Langea228632013-07-30 15:17:12 -04001727 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001728
1729 if (mD3d11Module)
1730 {
1731 FreeLibrary(mD3d11Module);
1732 mD3d11Module = NULL;
1733 }
1734
1735 if (mDxgiModule)
1736 {
1737 FreeLibrary(mDxgiModule);
1738 mDxgiModule = NULL;
1739 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001740}
1741
1742bool Renderer11::resetDevice()
1743{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001744 // recreate everything
1745 release();
1746 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001747
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001748 if (result != EGL_SUCCESS)
1749 {
1750 ERR("Could not reinitialize D3D11 device: %08X", result);
1751 return false;
1752 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001753
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001754 mDeviceLost = false;
1755
1756 return true;
1757}
1758
1759DWORD Renderer11::getAdapterVendor() const
1760{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001761 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001762}
1763
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001764std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001765{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001766 std::ostringstream rendererString;
1767
1768 rendererString << mDescription;
1769 rendererString << " Direct3D11";
1770
1771 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1772 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1773
1774 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001775}
1776
1777GUID Renderer11::getAdapterIdentifier() const
1778{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001779 // Use the adapter LUID as our adapter ID
1780 // This number is local to a machine is only guaranteed to be unique between restarts
1781 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1782 GUID adapterId = {0};
1783 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1784 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001785}
1786
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001787bool Renderer11::getBGRATextureSupport() const
1788{
1789 return mBGRATextureSupport;
1790}
1791
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001792bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001793{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001794 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001795}
1796
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001797bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001798{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001799 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001800}
1801
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001802bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001803{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001804 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001805}
1806
1807bool Renderer11::getDepthTextureSupport() const
1808{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001809 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001810}
1811
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001812bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001813{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001814 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001815}
1816
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001817bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001818{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001819 return mFloat32FilterSupport;
1820}
1821
1822bool Renderer11::getFloat32TextureRenderingSupport() const
1823{
1824 return mFloat32RenderSupport;
1825}
1826
1827bool Renderer11::getFloat16TextureSupport() const
1828{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001829 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001830}
1831
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001832bool Renderer11::getFloat16TextureFilteringSupport() const
1833{
1834 return mFloat16FilterSupport;
1835}
1836
1837bool Renderer11::getFloat16TextureRenderingSupport() const
1838{
1839 return mFloat16RenderSupport;
1840}
1841
Geoff Langd42cf4e2013-06-05 16:09:17 -04001842bool Renderer11::getRGB565TextureSupport() const
1843{
1844 return false;
1845}
1846
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001847bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001848{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001849 return false;
1850}
1851
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001852bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001853{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001854 return false;
1855}
1856
1857bool Renderer11::getTextureFilterAnisotropySupport() const
1858{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001859 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001860}
1861
1862float Renderer11::getTextureMaxAnisotropy() const
1863{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001864 switch (mFeatureLevel)
1865 {
1866 case D3D_FEATURE_LEVEL_11_0:
1867 return D3D11_MAX_MAXANISOTROPY;
1868 case D3D_FEATURE_LEVEL_10_1:
1869 case D3D_FEATURE_LEVEL_10_0:
1870 return D3D10_MAX_MAXANISOTROPY;
1871 default: UNREACHABLE();
1872 return 0;
1873 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001874}
1875
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001876bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001877{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00001878 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001879}
1880
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001881Range Renderer11::getViewportBounds() const
1882{
1883 switch (mFeatureLevel)
1884 {
1885 case D3D_FEATURE_LEVEL_11_0:
1886 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1887 case D3D_FEATURE_LEVEL_10_1:
1888 case D3D_FEATURE_LEVEL_10_0:
1889 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1890 default: UNREACHABLE();
1891 return Range(0, 0);
1892 }
1893}
1894
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001895unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001896{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001897 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1898 switch (mFeatureLevel)
1899 {
1900 case D3D_FEATURE_LEVEL_11_0:
1901 case D3D_FEATURE_LEVEL_10_1:
1902 case D3D_FEATURE_LEVEL_10_0:
1903 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1904 default: UNREACHABLE();
1905 return 0;
1906 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001907}
1908
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001909unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1910{
1911 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1912}
1913
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001914unsigned int Renderer11::getReservedVertexUniformVectors() const
1915{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001916 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001917}
1918
1919unsigned int Renderer11::getReservedFragmentUniformVectors() const
1920{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001921 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001922}
1923
1924unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001925{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001926 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1927 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1928 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001929}
1930
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001931unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001932{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001933 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1934 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1935 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001936}
1937
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001938unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001939{
1940 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001941 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1942 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001943 switch (mFeatureLevel)
1944 {
1945 case D3D_FEATURE_LEVEL_11_0:
1946 return D3D11_VS_OUTPUT_REGISTER_COUNT;
1947 case D3D_FEATURE_LEVEL_10_1:
1948 case D3D_FEATURE_LEVEL_10_0:
1949 return D3D10_VS_OUTPUT_REGISTER_COUNT;
1950 default: UNREACHABLE();
1951 return 0;
1952 }
1953}
1954
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001955unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
1956{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001957 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1958 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1959
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001960 switch (mFeatureLevel)
1961 {
1962 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001963 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001964 case D3D_FEATURE_LEVEL_10_1:
1965 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001966 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001967 default: UNREACHABLE();
1968 return 0;
1969 }
1970}
1971
1972unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
1973{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001974 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1975 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1976
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001977 switch (mFeatureLevel)
1978 {
1979 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001980 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001981 case D3D_FEATURE_LEVEL_10_1:
1982 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001983 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001984 default: UNREACHABLE();
1985 return 0;
1986 }
1987}
1988
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001989unsigned int Renderer11::getReservedVertexUniformBuffers() const
1990{
1991 // we reserve one buffer for the application uniforms, and one for driver uniforms
1992 return 2;
1993}
1994
1995unsigned int Renderer11::getReservedFragmentUniformBuffers() const
1996{
1997 // we reserve one buffer for the application uniforms, and one for driver uniforms
1998 return 2;
1999}
2000
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002001unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2002{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002003 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2004 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2005
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002006 switch (mFeatureLevel)
2007 {
2008 case D3D_FEATURE_LEVEL_11_0:
2009 return D3D11_SO_BUFFER_SLOT_COUNT;
2010 case D3D_FEATURE_LEVEL_10_1:
2011 case D3D_FEATURE_LEVEL_10_0:
2012 return D3D10_SO_BUFFER_SLOT_COUNT;
2013 default: UNREACHABLE();
2014 return 0;
2015 }
2016}
2017
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002018unsigned int Renderer11::getMaxUniformBufferSize() const
2019{
2020 // Each component is a 4-element vector of 4-byte units (floats)
2021 const unsigned int bytesPerComponent = 4 * sizeof(float);
2022
2023 switch (mFeatureLevel)
2024 {
2025 case D3D_FEATURE_LEVEL_11_0:
2026 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2027 case D3D_FEATURE_LEVEL_10_1:
2028 case D3D_FEATURE_LEVEL_10_0:
2029 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2030 default: UNREACHABLE();
2031 return 0;
2032 }
2033}
2034
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002035bool Renderer11::getNonPower2TextureSupport() const
2036{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002037 switch (mFeatureLevel)
2038 {
2039 case D3D_FEATURE_LEVEL_11_0:
2040 case D3D_FEATURE_LEVEL_10_1:
2041 case D3D_FEATURE_LEVEL_10_0:
2042 return true;
2043 default: UNREACHABLE();
2044 return false;
2045 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002046}
2047
2048bool Renderer11::getOcclusionQuerySupport() const
2049{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002050 switch (mFeatureLevel)
2051 {
2052 case D3D_FEATURE_LEVEL_11_0:
2053 case D3D_FEATURE_LEVEL_10_1:
2054 case D3D_FEATURE_LEVEL_10_0:
2055 return true;
2056 default: UNREACHABLE();
2057 return false;
2058 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002059}
2060
2061bool Renderer11::getInstancingSupport() const
2062{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002063 switch (mFeatureLevel)
2064 {
2065 case D3D_FEATURE_LEVEL_11_0:
2066 case D3D_FEATURE_LEVEL_10_1:
2067 case D3D_FEATURE_LEVEL_10_0:
2068 return true;
2069 default: UNREACHABLE();
2070 return false;
2071 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002072}
2073
2074bool Renderer11::getShareHandleSupport() const
2075{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002076 // We only currently support share handles with BGRA surfaces, because
2077 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002078 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002079 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002080}
2081
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002082bool Renderer11::getDerivativeInstructionSupport() const
2083{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002084 switch (mFeatureLevel)
2085 {
2086 case D3D_FEATURE_LEVEL_11_0:
2087 case D3D_FEATURE_LEVEL_10_1:
2088 case D3D_FEATURE_LEVEL_10_0:
2089 return true;
2090 default: UNREACHABLE();
2091 return false;
2092 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002093}
2094
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002095bool Renderer11::getPostSubBufferSupport() const
2096{
2097 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2098 return false;
2099}
2100
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002101int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002102{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002103 switch (mFeatureLevel)
2104 {
2105 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002106 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002107 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2108 default: UNREACHABLE(); return 0;
2109 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002110}
2111
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002112int Renderer11::getMinorShaderModel() const
2113{
2114 switch (mFeatureLevel)
2115 {
2116 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2117 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2118 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2119 default: UNREACHABLE(); return 0;
2120 }
2121}
2122
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002123float Renderer11::getMaxPointSize() const
2124{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002125 // choose a reasonable maximum. we enforce this in the shader.
2126 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2127 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002128}
2129
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002130int Renderer11::getMaxViewportDimension() const
2131{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002132 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2133 // In our case return the maximum texture size, which is the maximum render buffer size.
2134 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2135 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2136
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002137 switch (mFeatureLevel)
2138 {
2139 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002140 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002141 case D3D_FEATURE_LEVEL_10_1:
2142 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002143 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002144 default: UNREACHABLE();
2145 return 0;
2146 }
2147}
2148
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002149int Renderer11::getMaxTextureWidth() const
2150{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002151 switch (mFeatureLevel)
2152 {
2153 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2154 case D3D_FEATURE_LEVEL_10_1:
2155 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2156 default: UNREACHABLE(); return 0;
2157 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002158}
2159
2160int Renderer11::getMaxTextureHeight() const
2161{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002162 switch (mFeatureLevel)
2163 {
2164 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2165 case D3D_FEATURE_LEVEL_10_1:
2166 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2167 default: UNREACHABLE(); return 0;
2168 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002169}
2170
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002171int Renderer11::getMaxTextureDepth() const
2172{
2173 switch (mFeatureLevel)
2174 {
2175 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2176 case D3D_FEATURE_LEVEL_10_1:
2177 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2178 default: UNREACHABLE(); return 0;
2179 }
2180}
2181
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002182int Renderer11::getMaxTextureArrayLayers() const
2183{
2184 switch (mFeatureLevel)
2185 {
2186 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2187 case D3D_FEATURE_LEVEL_10_1:
2188 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2189 default: UNREACHABLE(); return 0;
2190 }
2191}
2192
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002193bool Renderer11::get32BitIndexSupport() const
2194{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002195 switch (mFeatureLevel)
2196 {
2197 case D3D_FEATURE_LEVEL_11_0:
2198 case D3D_FEATURE_LEVEL_10_1:
2199 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2200 default: UNREACHABLE(); return false;
2201 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002202}
2203
2204int Renderer11::getMinSwapInterval() const
2205{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002206 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002207}
2208
2209int Renderer11::getMaxSwapInterval() const
2210{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002211 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002212}
2213
2214int Renderer11::getMaxSupportedSamples() const
2215{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002216 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002217}
2218
Geoff Lang0e120e32013-05-29 10:23:55 -04002219GLsizei Renderer11::getMaxSupportedFormatSamples(GLint internalFormat) const
2220{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002221 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002222 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2223 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2224}
2225
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002226GLsizei Renderer11::getNumSampleCounts(GLint internalFormat) const
2227{
2228 unsigned int numCounts = 0;
2229
2230 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002231 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2232 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002233 {
2234 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2235 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2236
2237 if (iter != mMultisampleSupportMap.end())
2238 {
2239 const MultisampleSupportInfo& info = iter->second;
2240 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2241 {
2242 if (info.qualityLevels[i] > 0)
2243 {
2244 numCounts++;
2245 }
2246 }
2247 }
2248 }
2249
2250 return numCounts;
2251}
2252
2253void Renderer11::getSampleCounts(GLint internalFormat, GLsizei bufSize, GLint *params) const
2254{
2255 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002256 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2257 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2258 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002259 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002260 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002261
2262 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2263 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2264
2265 if (iter != mMultisampleSupportMap.end())
2266 {
2267 const MultisampleSupportInfo& info = iter->second;
2268 int bufPos = 0;
2269 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2270 {
2271 if (info.qualityLevels[i] > 0)
2272 {
2273 params[bufPos++] = i + 1;
2274 }
2275 }
2276 }
2277}
2278
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002279int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2280{
2281 if (requested == 0)
2282 {
2283 return 0;
2284 }
2285
2286 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2287 if (iter != mMultisampleSupportMap.end())
2288 {
2289 const MultisampleSupportInfo& info = iter->second;
2290 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2291 {
2292 if (info.qualityLevels[i] > 0)
2293 {
2294 return i + 1;
2295 }
2296 }
2297 }
2298
2299 return -1;
2300}
2301
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002302unsigned int Renderer11::getMaxRenderTargets() const
2303{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002304 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2305 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2306
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002307 switch (mFeatureLevel)
2308 {
2309 case D3D_FEATURE_LEVEL_11_0:
2310 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2311 case D3D_FEATURE_LEVEL_10_1:
2312 case D3D_FEATURE_LEVEL_10_0:
2313 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2314 default:
2315 UNREACHABLE();
2316 return 1;
2317 }
2318}
2319
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002320bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002321{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002322 if (source && dest)
2323 {
2324 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2325 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2326
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002327 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002328 return true;
2329 }
2330
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002331 return false;
2332}
2333
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002334bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002335{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002336 if (source && dest)
2337 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002338 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2339 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002340
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002341 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002342 return true;
2343 }
2344
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002345 return false;
2346}
2347
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002348bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2349{
2350 if (source && dest)
2351 {
2352 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2353 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2354
2355 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2356 return true;
2357 }
2358
2359 return false;
2360}
2361
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002362bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2363{
2364 if (source && dest)
2365 {
2366 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2367 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2368
2369 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2370 return true;
2371 }
2372
2373 return false;
2374}
2375
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002376bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002377 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002378{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002379 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002380 if (!colorbuffer)
2381 {
2382 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002383 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002384 }
2385
2386 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2387 if (!sourceRenderTarget)
2388 {
2389 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002390 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002391 }
2392
2393 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2394 if (!source)
2395 {
2396 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002397 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002398 }
2399
2400 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2401 if (!storage11)
2402 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002403 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002404 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002405 }
2406
2407 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2408 if (!destRenderTarget)
2409 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002410 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002411 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002412 }
2413
2414 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2415 if (!dest)
2416 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002417 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002418 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002419 }
2420
Geoff Langb86b9792013-06-04 16:32:05 -04002421 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2422 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002423
Geoff Langb86b9792013-06-04 16:32:05 -04002424 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2425 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002426
Geoff Langb86b9792013-06-04 16:32:05 -04002427 // Use nearest filtering because source and destination are the same size for the direct
2428 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002429 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002430 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002431
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002432 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002433}
2434
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002435bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002436 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002437{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002438 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002439 if (!colorbuffer)
2440 {
2441 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002442 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002443 }
2444
2445 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2446 if (!sourceRenderTarget)
2447 {
2448 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002449 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002450 }
2451
2452 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2453 if (!source)
2454 {
2455 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002456 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002457 }
2458
2459 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2460 if (!storage11)
2461 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002462 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002463 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002464 }
2465
Nicolas Capensb13f8662013-06-04 13:30:19 -04002466 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002467 if (!destRenderTarget)
2468 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002469 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002470 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002471 }
2472
2473 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2474 if (!dest)
2475 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002476 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002477 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002478 }
2479
Geoff Langb86b9792013-06-04 16:32:05 -04002480 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2481 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002482
Geoff Langb86b9792013-06-04 16:32:05 -04002483 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2484 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002485
Geoff Langb86b9792013-06-04 16:32:05 -04002486 // Use nearest filtering because source and destination are the same size for the direct
2487 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002488 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002489 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002490
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002491 return ret;
2492}
2493
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002494bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2495 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2496{
2497 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2498 if (!colorbuffer)
2499 {
2500 ERR("Failed to retrieve the color buffer from the frame buffer.");
2501 return gl::error(GL_OUT_OF_MEMORY, false);
2502 }
2503
2504 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2505 if (!sourceRenderTarget)
2506 {
2507 ERR("Failed to retrieve the render target from the frame buffer.");
2508 return gl::error(GL_OUT_OF_MEMORY, false);
2509 }
2510
2511 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2512 if (!source)
2513 {
2514 ERR("Failed to retrieve the render target view from the render target.");
2515 return gl::error(GL_OUT_OF_MEMORY, false);
2516 }
2517
2518 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2519 if (!storage11)
2520 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002521 ERR("Failed to retrieve the texture storage from the destination.");
2522 return gl::error(GL_OUT_OF_MEMORY, false);
2523 }
2524
2525 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2526 if (!destRenderTarget)
2527 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002528 ERR("Failed to retrieve the render target from the destination storage.");
2529 return gl::error(GL_OUT_OF_MEMORY, false);
2530 }
2531
2532 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2533 if (!dest)
2534 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002535 ERR("Failed to retrieve the render target view from the destination render target.");
2536 return gl::error(GL_OUT_OF_MEMORY, false);
2537 }
2538
Geoff Langb86b9792013-06-04 16:32:05 -04002539 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2540 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002541
Geoff Langb86b9792013-06-04 16:32:05 -04002542 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2543 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002544
Geoff Langb86b9792013-06-04 16:32:05 -04002545 // Use nearest filtering because source and destination are the same size for the direct
2546 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002547 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002548 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002549
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002550 return ret;
2551}
2552
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002553bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2554 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2555{
2556 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2557 if (!colorbuffer)
2558 {
2559 ERR("Failed to retrieve the color buffer from the frame buffer.");
2560 return gl::error(GL_OUT_OF_MEMORY, false);
2561 }
2562
2563 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2564 if (!sourceRenderTarget)
2565 {
2566 ERR("Failed to retrieve the render target from the frame buffer.");
2567 return gl::error(GL_OUT_OF_MEMORY, false);
2568 }
2569
2570 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2571 if (!source)
2572 {
2573 ERR("Failed to retrieve the render target view from the render target.");
2574 return gl::error(GL_OUT_OF_MEMORY, false);
2575 }
2576
2577 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2578 if (!storage11)
2579 {
Geoff Langea228632013-07-30 15:17:12 -04002580 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002581 ERR("Failed to retrieve the texture storage from the destination.");
2582 return gl::error(GL_OUT_OF_MEMORY, false);
2583 }
2584
2585 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2586 if (!destRenderTarget)
2587 {
Geoff Langea228632013-07-30 15:17:12 -04002588 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002589 ERR("Failed to retrieve the render target from the destination storage.");
2590 return gl::error(GL_OUT_OF_MEMORY, false);
2591 }
2592
2593 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2594 if (!dest)
2595 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002596 ERR("Failed to retrieve the render target view from the destination render target.");
2597 return gl::error(GL_OUT_OF_MEMORY, false);
2598 }
2599
Geoff Langb86b9792013-06-04 16:32:05 -04002600 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2601 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002602
Geoff Langb86b9792013-06-04 16:32:05 -04002603 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2604 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002605
Geoff Langb86b9792013-06-04 16:32:05 -04002606 // Use nearest filtering because source and destination are the same size for the direct
2607 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002608 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002609 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002610
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002611 return ret;
2612}
2613
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002614void Renderer11::unapplyRenderTargets()
2615{
2616 setOneTimeRenderTarget(NULL);
2617}
2618
2619void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2620{
2621 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2622
2623 rtvArray[0] = renderTargetView;
2624
2625 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2626
2627 // Do not preserve the serial for this one-time-use render target
2628 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2629 {
2630 mAppliedRenderTargetSerials[rtIndex] = 0;
2631 }
2632}
2633
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002634RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2635{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002636 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002637 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002638
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002639 if (depth)
2640 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002641 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002642 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
2643 swapChain11->getDepthStencilTexture(), NULL,
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002644 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002645 }
2646 else
2647 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002648 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002649 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002650 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002651 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002652 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002653 }
2654 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002655}
2656
Geoff Langa2d97f12013-06-11 11:44:02 -04002657RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002658{
Geoff Langa2d97f12013-06-11 11:44:02 -04002659 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002660 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002661}
2662
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002663ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002664{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002665 ShaderExecutable11 *executable = NULL;
2666
2667 switch (type)
2668 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002669 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002670 {
2671 ID3D11VertexShader *vshader = NULL;
2672 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2673 ASSERT(SUCCEEDED(result));
2674
2675 if (vshader)
2676 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002677 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002678 }
2679 }
2680 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002681 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002682 {
2683 ID3D11PixelShader *pshader = NULL;
2684 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2685 ASSERT(SUCCEEDED(result));
2686
2687 if (pshader)
2688 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002689 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002690 }
2691 }
2692 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002693 case rx::SHADER_GEOMETRY:
2694 {
2695 ID3D11GeometryShader *gshader = NULL;
2696 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2697 ASSERT(SUCCEEDED(result));
2698
2699 if (gshader)
2700 {
2701 executable = new ShaderExecutable11(function, length, gshader);
2702 }
2703 }
2704 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002705 default:
2706 UNREACHABLE();
2707 break;
2708 }
2709
2710 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002711}
2712
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002713ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002714{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002715 const char *profile = NULL;
2716
2717 switch (type)
2718 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002719 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002720 profile = "vs_4_0";
2721 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002722 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002723 profile = "ps_4_0";
2724 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002725 case rx::SHADER_GEOMETRY:
2726 profile = "gs_4_0";
2727 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002728 default:
2729 UNREACHABLE();
2730 return NULL;
2731 }
2732
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00002733 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002734 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002735 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002736 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002737 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002738
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002739 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002740 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002741
2742 return executable;
2743}
2744
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002745VertexBuffer *Renderer11::createVertexBuffer()
2746{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002747 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002748}
2749
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002750IndexBuffer *Renderer11::createIndexBuffer()
2751{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002752 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002753}
2754
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002755BufferStorage *Renderer11::createBufferStorage()
2756{
2757 return new BufferStorage11(this);
2758}
2759
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002760QueryImpl *Renderer11::createQuery(GLenum type)
2761{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002762 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002763}
2764
2765FenceImpl *Renderer11::createFence()
2766{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002767 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002768}
2769
Jamie Madill0e0510f2013-10-10 15:46:23 -04002770bool Renderer11::supportsFastCopyBufferToTexture(GLint internalFormat) const
2771{
2772 //TODO
2773 return false;
2774}
2775
Jamie Madilla21eea32013-09-18 14:36:25 -04002776bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2777 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2778{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002779 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002780 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2781}
2782
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002783bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002784{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002785 ASSERT(colorbuffer != NULL);
2786
2787 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2788 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002789 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002790 *subresourceIndex = renderTarget->getSubresourceIndex();
2791
2792 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2793 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002794 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002795 ID3D11Resource *textureResource = NULL;
2796 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002797
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002798 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002799 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002800 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002801 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002802
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002803 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002804 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002805 return true;
2806 }
2807 else
2808 {
2809 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2810 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002811 }
2812 }
2813 }
2814 }
2815
2816 return false;
2817}
2818
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002819bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002820 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002821{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002822 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002823 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002824 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002825
2826 if (!readBuffer)
2827 {
2828 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2829 return gl::error(GL_OUT_OF_MEMORY, false);
2830 }
2831
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002832 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002833
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002834 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002835 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002836 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2837 {
2838 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
2839
2840 if (!drawBuffer)
2841 {
2842 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2843 return gl::error(GL_OUT_OF_MEMORY, false);
2844 }
2845
2846 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2847
Geoff Lang125deab2013-08-09 13:34:16 -04002848 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002849 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002850 {
2851 return false;
2852 }
2853 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002854 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002855 }
2856
Geoff Lang685806d2013-06-12 11:16:36 -04002857 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002858 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002859 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
2860 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
2861
2862 if (!readBuffer)
2863 {
2864 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2865 return gl::error(GL_OUT_OF_MEMORY, false);
2866 }
2867
2868 if (!drawBuffer)
2869 {
2870 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2871 return gl::error(GL_OUT_OF_MEMORY, false);
2872 }
2873
2874 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2875 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2876
Geoff Lang125deab2013-08-09 13:34:16 -04002877 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002878 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002879 {
2880 return false;
2881 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002882 }
2883
2884 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002885}
2886
2887void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2888 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2889{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002890 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002891 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002892
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002893 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2894
2895 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002896 {
2897 gl::Rectangle area;
2898 area.x = x;
2899 area.y = y;
2900 area.width = width;
2901 area.height = height;
2902
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002903 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
2904 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002905
Geoff Langea228632013-07-30 15:17:12 -04002906 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002907 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002908}
2909
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002910Image *Renderer11::createImage()
2911{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002912 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002913}
2914
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002915void Renderer11::generateMipmap(Image *dest, Image *src)
2916{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002917 Image11 *dest11 = Image11::makeImage11(dest);
2918 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04002919 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002920}
2921
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002922TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
2923{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002924 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
2925 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002926}
2927
2928TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
2929{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002930 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002931}
2932
2933TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
2934{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002935 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002936}
2937
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00002938TextureStorage *Renderer11::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
2939{
2940 return new TextureStorage11_3D(this, levels, internalformat, usage, width, height, depth);
2941}
2942
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002943TextureStorage *Renderer11::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
2944{
2945 return new TextureStorage11_2DArray(this, levels, internalformat, usage, width, height, depth);
2946}
2947
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002948void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
2949 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
2950 GLint packAlignment, void *pixels)
2951{
2952 D3D11_TEXTURE2D_DESC textureDesc;
2953 texture->GetDesc(&textureDesc);
2954
2955 D3D11_TEXTURE2D_DESC stagingDesc;
2956 stagingDesc.Width = area.width;
2957 stagingDesc.Height = area.height;
2958 stagingDesc.MipLevels = 1;
2959 stagingDesc.ArraySize = 1;
2960 stagingDesc.Format = textureDesc.Format;
2961 stagingDesc.SampleDesc.Count = 1;
2962 stagingDesc.SampleDesc.Quality = 0;
2963 stagingDesc.Usage = D3D11_USAGE_STAGING;
2964 stagingDesc.BindFlags = 0;
2965 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
2966 stagingDesc.MiscFlags = 0;
2967
2968 ID3D11Texture2D* stagingTex = NULL;
2969 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
2970 if (FAILED(result))
2971 {
2972 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
2973 return;
2974 }
2975
2976 ID3D11Texture2D* srcTex = NULL;
2977 if (textureDesc.SampleDesc.Count > 1)
2978 {
2979 D3D11_TEXTURE2D_DESC resolveDesc;
2980 resolveDesc.Width = textureDesc.Width;
2981 resolveDesc.Height = textureDesc.Height;
2982 resolveDesc.MipLevels = 1;
2983 resolveDesc.ArraySize = 1;
2984 resolveDesc.Format = textureDesc.Format;
2985 resolveDesc.SampleDesc.Count = 1;
2986 resolveDesc.SampleDesc.Quality = 0;
2987 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
2988 resolveDesc.BindFlags = 0;
2989 resolveDesc.CPUAccessFlags = 0;
2990 resolveDesc.MiscFlags = 0;
2991
2992 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
2993 if (FAILED(result))
2994 {
2995 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04002996 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002997 return;
2998 }
2999
3000 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3001 subResource = 0;
3002 }
3003 else
3004 {
3005 srcTex = texture;
3006 srcTex->AddRef();
3007 }
3008
3009 D3D11_BOX srcBox;
3010 srcBox.left = area.x;
3011 srcBox.right = area.x + area.width;
3012 srcBox.top = area.y;
3013 srcBox.bottom = area.y + area.height;
3014 srcBox.front = 0;
3015 srcBox.back = 1;
3016
3017 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3018
Geoff Langea228632013-07-30 15:17:12 -04003019 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003020
3021 D3D11_MAPPED_SUBRESOURCE mapping;
3022 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3023
3024 unsigned char *source;
3025 int inputPitch;
3026 if (packReverseRowOrder)
3027 {
3028 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3029 inputPitch = -static_cast<int>(mapping.RowPitch);
3030 }
3031 else
3032 {
3033 source = static_cast<unsigned char*>(mapping.pData);
3034 inputPitch = static_cast<int>(mapping.RowPitch);
3035 }
3036
Geoff Lang697ad3e2013-06-04 10:11:28 -04003037 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003038
Jamie Madilld6cb2442013-07-10 15:13:38 -04003039 GLint sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003040 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3041 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3042
3043 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3044
3045 if (sourceFormat == format && sourceType == type)
3046 {
3047 // Direct copy possible
3048 unsigned char *dest = static_cast<unsigned char*>(pixels);
3049 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003050 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003051 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003052 }
3053 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003054 else
3055 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003056 GLint destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
3057 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3058
3059 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type, getCurrentClientVersion());
3060 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003061 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003062 // Fast copy is possible through some special function
3063 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003064 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003065 for (int x = 0; x < area.width; x++)
3066 {
3067 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3068 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3069
3070 fastCopyFunc(src, dest);
3071 }
3072 }
3073 }
3074 else
3075 {
Jamie Madilld6cb2442013-07-10 15:13:38 -04003076 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003077 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3078
3079 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3080 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3081 sizeof(temp) >= sizeof(gl::ColorUI) &&
3082 sizeof(temp) >= sizeof(gl::ColorI));
3083
3084 for (int y = 0; y < area.height; y++)
3085 {
3086 for (int x = 0; x < area.width; x++)
3087 {
3088 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3089 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3090
3091 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3092 // will not allow the copy otherwise.
3093 readFunc(src, temp);
3094 writeFunc(temp, dest);
3095 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003096 }
3097 }
3098 }
3099
3100 mDeviceContext->Unmap(stagingTex, 0);
3101
Geoff Langea228632013-07-30 15:17:12 -04003102 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003103}
3104
Geoff Lang758d5b22013-06-11 11:42:50 -04003105bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003106 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3107 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003108{
Geoff Lang975af372013-06-12 11:19:22 -04003109 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3110 // it should never be the case that both color and depth/stencil need to be blitted at
3111 // at the same time.
3112 ASSERT(colorBlit != (depthBlit || stencilBlit));
3113
Geoff Langc1f51be2013-06-11 11:49:14 -04003114 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003115
Geoff Lang4d782732013-07-22 10:44:18 -04003116 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3117 if (!drawRenderTarget)
3118 {
3119 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3120 return gl::error(GL_OUT_OF_MEMORY, false);
3121 }
3122
3123 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3124 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3125 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3126 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3127
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003128 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3129 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003130 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003131 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003132 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003133 }
3134
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003135 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003136 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003137 unsigned int readSubresource = 0;
3138 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003139 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003140 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3141 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003142
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003143 if (unresolvedTexture)
3144 {
3145 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3146 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003147
Geoff Langea228632013-07-30 15:17:12 -04003148 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003149
3150 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3151 if (FAILED(result))
3152 {
Geoff Langea228632013-07-30 15:17:12 -04003153 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003154 return gl::error(GL_OUT_OF_MEMORY, false);
3155 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003156 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003157 }
3158 else
3159 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003160 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003161 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003162 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003163 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003164 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003165 }
3166
Geoff Lang4d782732013-07-22 10:44:18 -04003167 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003168 {
Geoff Lang4d782732013-07-22 10:44:18 -04003169 SafeRelease(readTexture);
3170 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003171 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003172 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003173 }
3174
Geoff Lang125deab2013-08-09 13:34:16 -04003175 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3176 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3177
3178 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3179
3180 bool wholeBufferCopy = !scissorNeeded &&
3181 readRect.x == 0 && readRect.width == readSize.width &&
3182 readRect.y == 0 && readRect.height == readSize.height &&
3183 drawRect.x == 0 && drawRect.width == drawSize.width &&
3184 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003185
Geoff Langc1f51be2013-06-11 11:49:14 -04003186 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003187
Geoff Lang125deab2013-08-09 13:34:16 -04003188 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || readRect.height < 0;
3189
3190 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3191 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3192 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3193 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3194
3195 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3196 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3197 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3198
Geoff Langc1f51be2013-06-11 11:49:14 -04003199 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003200 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3201 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003202 {
Geoff Lang125deab2013-08-09 13:34:16 -04003203 UINT dstX = drawRect.x;
3204 UINT dstY = drawRect.y;
3205
Geoff Langc1f51be2013-06-11 11:49:14 -04003206 D3D11_BOX readBox;
3207 readBox.left = readRect.x;
3208 readBox.right = readRect.x + readRect.width;
3209 readBox.top = readRect.y;
3210 readBox.bottom = readRect.y + readRect.height;
3211 readBox.front = 0;
3212 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003213
Geoff Lang125deab2013-08-09 13:34:16 -04003214 if (scissorNeeded)
3215 {
3216 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3217 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3218
3219 if (drawRect.x < scissor->x)
3220 {
3221 dstX = scissor->x;
3222 readBox.left += (scissor->x - drawRect.x);
3223 }
3224 if (drawRect.y < scissor->y)
3225 {
3226 dstY = scissor->y;
3227 readBox.top += (scissor->y - drawRect.y);
3228 }
3229 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3230 {
3231 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3232 }
3233 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3234 {
3235 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3236 }
3237 }
3238
Geoff Langc1f51be2013-06-11 11:49:14 -04003239 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3240 // We also require complete framebuffer copies for depth-stencil blit.
3241 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003242
Geoff Lang125deab2013-08-09 13:34:16 -04003243 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003244 readTexture, readSubresource, pSrcBox);
3245 result = true;
3246 }
3247 else
3248 {
3249 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003250 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003251
Geoff Lang975af372013-06-12 11:19:22 -04003252 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003253 {
Geoff Lang975af372013-06-12 11:19:22 -04003254 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003255 drawTexture, drawSubresource, drawArea, drawSize,
3256 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003257 }
3258 else if (depthBlit)
3259 {
Geoff Lang125deab2013-08-09 13:34:16 -04003260 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3261 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003262 }
3263 else if (stencilBlit)
3264 {
3265 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003266 drawTexture, drawSubresource, drawArea, drawSize,
3267 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003268 }
3269 else
3270 {
Geoff Lang685806d2013-06-12 11:16:36 -04003271 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003272 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3273 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003274 }
3275 }
3276
3277 SafeRelease(readTexture);
3278 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003279
3280 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003281}
3282
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003283ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3284{
3285 D3D11_TEXTURE2D_DESC textureDesc;
3286 source->GetDesc(&textureDesc);
3287
3288 if (textureDesc.SampleDesc.Count > 1)
3289 {
3290 D3D11_TEXTURE2D_DESC resolveDesc;
3291 resolveDesc.Width = textureDesc.Width;
3292 resolveDesc.Height = textureDesc.Height;
3293 resolveDesc.MipLevels = 1;
3294 resolveDesc.ArraySize = 1;
3295 resolveDesc.Format = textureDesc.Format;
3296 resolveDesc.SampleDesc.Count = 1;
3297 resolveDesc.SampleDesc.Quality = 0;
3298 resolveDesc.Usage = textureDesc.Usage;
3299 resolveDesc.BindFlags = textureDesc.BindFlags;
3300 resolveDesc.CPUAccessFlags = 0;
3301 resolveDesc.MiscFlags = 0;
3302
3303 ID3D11Texture2D *resolveTexture = NULL;
3304 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3305 if (FAILED(result))
3306 {
3307 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3308 return NULL;
3309 }
3310
3311 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3312 return resolveTexture;
3313 }
3314 else
3315 {
3316 source->AddRef();
3317 return source;
3318 }
3319}
3320
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003321bool Renderer11::getLUID(LUID *adapterLuid) const
3322{
3323 adapterLuid->HighPart = 0;
3324 adapterLuid->LowPart = 0;
3325
3326 if (!mDxgiAdapter)
3327 {
3328 return false;
3329 }
3330
3331 DXGI_ADAPTER_DESC adapterDesc;
3332 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3333 {
3334 return false;
3335 }
3336
3337 *adapterLuid = adapterDesc.AdapterLuid;
3338 return true;
3339}
3340
Jamie Madillc8c102b2013-10-10 15:10:24 -04003341GLint Renderer11::getNativeTextureFormat(GLint internalFormat) const
3342{
3343 int clientVersion = getCurrentClientVersion();
3344 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3345}
3346
Geoff Lang61e49a52013-05-29 10:22:58 -04003347Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3348{
3349 MultisampleSupportInfo supportInfo = { 0 };
3350
3351 UINT formatSupport;
3352 HRESULT result;
3353
3354 result = mDevice->CheckFormatSupport(format, &formatSupport);
3355 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3356 {
3357 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3358 {
3359 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3360 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3361 {
3362 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3363 }
3364 else
3365 {
3366 supportInfo.qualityLevels[i - 1] = 0;
3367 }
3368 }
3369 }
3370
3371 return supportInfo;
3372}
3373
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003374}