blob: 0a8849c7026fe5ef5d931c1587f2e01e97bcf33b [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 {
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000225 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000226 };
227
228 D3D11_INFO_QUEUE_FILTER filter = {0};
229 filter.DenyList.NumIDs = ArraySize(hideMessages);
230 filter.DenyList.pIDList = hideMessages;
231
232 infoQueue->AddStorageFilterEntries(&filter);
Geoff Langea228632013-07-30 15:17:12 -0400233 SafeRelease(infoQueue);
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000234 }
235#endif
236
Geoff Lang61e49a52013-05-29 10:22:58 -0400237 mMaxSupportedSamples = 0;
238
239 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
240 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000241 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400242 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
243 mMultisampleSupportMap.insert(std::make_pair(*i, support));
244 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000245 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000246
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000247 initializeDevice();
248
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000249 // BGRA texture support is optional in feature levels 10 and 10_1
250 UINT formatSupport;
251 result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport);
252 if (FAILED(result))
253 {
254 ERR("Error checking BGRA format support: 0x%08X", result);
255 }
256 else
257 {
258 const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET);
259 mBGRATextureSupport = (formatSupport & flags) == flags;
260 }
261
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000262 // Check floating point texture support
263 static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE;
264 static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET;
265 static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE;
266
267 DXGI_FORMAT float16Formats[] =
268 {
269 DXGI_FORMAT_R16_FLOAT,
270 DXGI_FORMAT_R16G16_FLOAT,
271 DXGI_FORMAT_R16G16B16A16_FLOAT,
272 };
273
274 DXGI_FORMAT float32Formats[] =
275 {
276 DXGI_FORMAT_R32_FLOAT,
277 DXGI_FORMAT_R32G32_FLOAT,
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000278 DXGI_FORMAT_R32G32B32A32_FLOAT,
279 };
280
281 mFloat16TextureSupport = true;
282 mFloat16FilterSupport = true;
283 mFloat16RenderSupport = true;
284 for (unsigned int i = 0; i < ArraySize(float16Formats); i++)
285 {
286 if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport)))
287 {
288 mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
289 mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
290 mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
291 }
292 else
293 {
294 mFloat16TextureSupport = false;
295 mFloat16RenderSupport = false;
296 mFloat16FilterSupport = false;
297 }
298 }
299
300 mFloat32TextureSupport = true;
301 mFloat32FilterSupport = true;
302 mFloat32RenderSupport = true;
303 for (unsigned int i = 0; i < ArraySize(float32Formats); i++)
304 {
305 if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport)))
306 {
307 mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
308 mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
309 mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
310 }
311 else
312 {
313 mFloat32TextureSupport = false;
314 mFloat32FilterSupport = false;
315 mFloat32RenderSupport = false;
316 }
317 }
318
Geoff Lang632192d2013-10-04 13:40:46 -0400319 DXGI_FORMAT rgTextureFormats[] =
320 {
321 DXGI_FORMAT_R8_UNORM,
322 DXGI_FORMAT_R8G8_UNORM,
323 DXGI_FORMAT_R16_FLOAT,
324 DXGI_FORMAT_R16G16_FLOAT,
325 DXGI_FORMAT_R32_FLOAT,
326 DXGI_FORMAT_R32G32_FLOAT,
327 };
328
329 mRGTextureSupport = true;
330 for (unsigned int i = 0; i < ArraySize(rgTextureFormats); i++)
331 {
332 if (SUCCEEDED(mDevice->CheckFormatSupport(rgTextureFormats[i], &formatSupport)))
333 {
334 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
335 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
336 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
337 }
338 else
339 {
340 mRGTextureSupport = false;
341 }
342 }
343
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +0000344 // Check compressed texture support
345 const unsigned int requiredCompressedTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D;
346
347 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &formatSupport)))
348 {
349 mDXT1TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
350 }
351 else
352 {
353 mDXT1TextureSupport = false;
354 }
355
356 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport)))
357 {
358 mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
359 }
360 else
361 {
362 mDXT3TextureSupport = false;
363 }
364
365 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC5_UNORM, &formatSupport)))
366 {
367 mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
368 }
369 else
370 {
371 mDXT5TextureSupport = false;
372 }
373
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +0000374 // Check depth texture support
375 DXGI_FORMAT depthTextureFormats[] =
376 {
377 DXGI_FORMAT_D16_UNORM,
378 DXGI_FORMAT_D24_UNORM_S8_UINT,
379 };
380
381 static const unsigned int requiredDepthTextureFlags = D3D11_FORMAT_SUPPORT_DEPTH_STENCIL |
382 D3D11_FORMAT_SUPPORT_TEXTURE2D;
383
384 mDepthTextureSupport = true;
385 for (unsigned int i = 0; i < ArraySize(depthTextureFormats); i++)
386 {
387 if (SUCCEEDED(mDevice->CheckFormatSupport(depthTextureFormats[i], &formatSupport)))
388 {
389 mDepthTextureSupport = mDepthTextureSupport && ((formatSupport & requiredDepthTextureFlags) == requiredDepthTextureFlags);
390 }
391 else
392 {
393 mDepthTextureSupport = false;
394 }
395 }
396
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000397 return EGL_SUCCESS;
398}
399
400// do any one-time device initialization
401// NOTE: this is also needed after a device lost/reset
402// to reset the scene status and ensure the default states are reset.
403void Renderer11::initializeDevice()
404{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000405 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000406 mInputLayoutCache.initialize(mDevice, mDeviceContext);
407
408 ASSERT(!mVertexDataManager && !mIndexDataManager);
409 mVertexDataManager = new VertexDataManager(this);
410 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000411
Geoff Langb86b9792013-06-04 16:32:05 -0400412 ASSERT(!mBlit);
413 mBlit = new Blit11(this);
414
Geoff Langda507fe2013-08-20 12:01:42 -0400415 ASSERT(!mClear);
416 mClear = new Clear11(this);
417
Jamie Madilla21eea32013-09-18 14:36:25 -0400418 ASSERT(!mPixelTransfer);
419 mPixelTransfer = new PixelTransfer11(this);
420
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000421 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000422}
423
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000424int Renderer11::generateConfigs(ConfigDesc **configDescList)
425{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000426 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
427 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000428 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
429 int numConfigs = 0;
430
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000431 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000432 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000433 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000434 {
435 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
436
437 UINT formatSupport = 0;
438 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000439
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000440 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
441 {
442 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
443
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000444 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000445
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000446 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
447 {
448 UINT formatSupport = 0;
449 result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
450 depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
451 }
452
453 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000454 {
Jamie Madilld6cb2442013-07-10 15:13:38 -0400455 // FIXME: parse types from context version
456 ASSERT(d3d11_gl::GetInternalFormat(renderTargetFormat, 2) == d3d11_gl::GetInternalFormat(renderTargetFormat, 3));
457 ASSERT(d3d11_gl::GetInternalFormat(depthStencilFormat, 2) == d3d11_gl::GetInternalFormat(depthStencilFormat, 3));
458
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000459 ConfigDesc newConfig;
Jamie Madilld6cb2442013-07-10 15:13:38 -0400460 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat, getCurrentClientVersion());
461 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat, getCurrentClientVersion());
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000462 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
463 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000464 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000465
466 (*configDescList)[numConfigs++] = newConfig;
467 }
468 }
469 }
470 }
471
472 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000473}
474
475void Renderer11::deleteConfigs(ConfigDesc *configDescList)
476{
477 delete [] (configDescList);
478}
479
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000480void Renderer11::sync(bool block)
481{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000482 if (block)
483 {
484 HRESULT result;
485
486 if (!mSyncQuery)
487 {
488 D3D11_QUERY_DESC queryDesc;
489 queryDesc.Query = D3D11_QUERY_EVENT;
490 queryDesc.MiscFlags = 0;
491
492 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
493 ASSERT(SUCCEEDED(result));
494 }
495
496 mDeviceContext->End(mSyncQuery);
497 mDeviceContext->Flush();
498
499 do
500 {
501 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
502
503 // Keep polling, but allow other threads to do something useful first
504 Sleep(0);
505
506 if (testDeviceLost(true))
507 {
508 return;
509 }
510 }
511 while (result == S_FALSE);
512 }
513 else
514 {
515 mDeviceContext->Flush();
516 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000517}
518
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000519SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
520{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000521 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000522}
523
Geoff Lange2e0ce02013-09-17 17:05:08 -0400524void Renderer11::generateSwizzle(gl::Texture *texture)
525{
526 UNIMPLEMENTED();
527}
528
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000529void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
530{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000531 if (type == gl::SAMPLER_PIXEL)
532 {
533 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
534 {
535 ERR("Pixel shader sampler index %i is not valid.", index);
536 return;
537 }
538
539 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
540 {
541 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
542
543 if (!dxSamplerState)
544 {
545 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
546 "sampler state for pixel shaders at slot %i.", index);
547 }
548
549 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
550
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000551 mCurPixelSamplerStates[index] = samplerState;
552 }
553
554 mForceSetPixelSamplerStates[index] = false;
555 }
556 else if (type == gl::SAMPLER_VERTEX)
557 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000558 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000559 {
560 ERR("Vertex shader sampler index %i is not valid.", index);
561 return;
562 }
563
564 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
565 {
566 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
567
568 if (!dxSamplerState)
569 {
570 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
571 "sampler state for vertex shaders at slot %i.", index);
572 }
573
574 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
575
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000576 mCurVertexSamplerStates[index] = samplerState;
577 }
578
579 mForceSetVertexSamplerStates[index] = false;
580 }
581 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000582}
583
584void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
585{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000586 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000587 unsigned int serial = 0;
588 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000589
590 if (texture)
591 {
592 TextureStorageInterface *texStorage = texture->getNativeTexture();
593 if (texStorage)
594 {
595 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
Geoff Lang644bbf22013-09-17 17:02:43 -0400596 textureSRV = storage11->getSRV(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
597 texture->getSwizzleAlpha());
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000598 }
599
600 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
601 // missing the shader resource view
602 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000603
604 serial = texture->getTextureSerial();
605 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000606 }
607
608 if (type == gl::SAMPLER_PIXEL)
609 {
610 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
611 {
612 ERR("Pixel shader sampler index %i is not valid.", index);
613 return;
614 }
615
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000616 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
617 {
618 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
619 }
620
621 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000622 }
623 else if (type == gl::SAMPLER_VERTEX)
624 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000625 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000626 {
627 ERR("Vertex shader sampler index %i is not valid.", index);
628 return;
629 }
630
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000631 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
632 {
633 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
634 }
635
636 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000637 }
638 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000639}
640
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000641bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
642{
643 // convert buffers to ID3D11Buffer*
644 ID3D11Buffer *vertexConstantBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = { NULL };
645 ID3D11Buffer *pixelConstantBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = { NULL };
646
647 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
648 {
649 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
650 if (uniformBuffer)
651 {
652 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Jamie Madill171ca0e2013-10-10 15:10:31 -0400653 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(true);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000654
655 if (!constantBuffer)
656 {
657 return false;
658 }
659
Geoff Langc6354ee2013-07-22 10:40:07 -0400660 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
661 {
662 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
663 1, &constantBuffer);
664 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
665 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000666 }
667 }
668
669 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
670 {
671 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
672 if (uniformBuffer)
673 {
674 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Jamie Madill171ca0e2013-10-10 15:10:31 -0400675 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(true);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000676
677 if (!constantBuffer)
678 {
679 return false;
680 }
681
Geoff Langc6354ee2013-07-22 10:40:07 -0400682 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
683 {
684 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
685 1, &constantBuffer);
686 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
687 }
688
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000689 pixelConstantBuffers[uniformBufferIndex] = constantBuffer;
690 }
691 }
692
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000693 return true;
694}
695
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000696void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000697{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000698 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000699 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000700 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
701 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000702 if (!dxRasterState)
703 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000704 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000705 "rasterizer state.");
706 }
707
708 mDeviceContext->RSSetState(dxRasterState);
709
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000710 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000711 }
712
713 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000714}
715
Geoff Lang2a64ee42013-05-31 11:22:40 -0400716void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000717 unsigned int sampleMask)
718{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000719 if (mForceSetBlendState ||
720 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400721 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000722 sampleMask != mCurSampleMask)
723 {
724 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
725 if (!dxBlendState)
726 {
727 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
728 "blend state.");
729 }
730
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000731 float blendColors[4] = {0.0f};
732 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
733 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
734 {
735 blendColors[0] = blendColor.red;
736 blendColors[1] = blendColor.green;
737 blendColors[2] = blendColor.blue;
738 blendColors[3] = blendColor.alpha;
739 }
740 else
741 {
742 blendColors[0] = blendColor.alpha;
743 blendColors[1] = blendColor.alpha;
744 blendColors[2] = blendColor.alpha;
745 blendColors[3] = blendColor.alpha;
746 }
747
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000748 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
749
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000750 mCurBlendState = blendState;
751 mCurBlendColor = blendColor;
752 mCurSampleMask = sampleMask;
753 }
754
755 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000756}
757
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000758void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000759 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000760{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000761 if (mForceSetDepthStencilState ||
762 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
763 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
764 {
765 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
766 stencilRef != stencilBackRef ||
767 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
768 {
769 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
770 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000771 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000772 }
773
774 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
775 if (!dxDepthStencilState)
776 {
777 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
778 "setting the default depth stencil state.");
779 }
780
781 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
782
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000783 mCurDepthStencilState = depthStencilState;
784 mCurStencilRef = stencilRef;
785 mCurStencilBackRef = stencilBackRef;
786 }
787
788 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000789}
790
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000791void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000792{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000793 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
794 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000795 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000796 if (enabled)
797 {
798 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000799 rect.left = std::max(0, scissor.x);
800 rect.top = std::max(0, scissor.y);
801 rect.right = scissor.x + std::max(0, scissor.width);
802 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000803
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000804 mDeviceContext->RSSetScissorRects(1, &rect);
805 }
806
807 if (enabled != mScissorEnabled)
808 {
809 mForceSetRasterState = true;
810 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000811
812 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000813 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000814 }
815
816 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000817}
818
daniel@transgaming.com12985182012-12-20 20:56:31 +0000819bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000820 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000821{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000822 gl::Rectangle actualViewport = viewport;
823 float actualZNear = gl::clamp01(zNear);
824 float actualZFar = gl::clamp01(zFar);
825 if (ignoreViewport)
826 {
827 actualViewport.x = 0;
828 actualViewport.y = 0;
829 actualViewport.width = mRenderTargetDesc.width;
830 actualViewport.height = mRenderTargetDesc.height;
831 actualZNear = 0.0f;
832 actualZFar = 1.0f;
833 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000834
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000835 // Get D3D viewport bounds, which depends on the feature level
836 const Range& viewportBounds = getViewportBounds();
837
838 // 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 +0000839 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000840 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
841 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
842 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
843 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
844 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
845 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000846 dxViewport.MinDepth = actualZNear;
847 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000848
849 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
850 {
851 return false; // Nothing to render
852 }
853
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000854 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
855 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000856
daniel@transgaming.com53670042012-11-28 20:55:51 +0000857 if (viewportChanged)
858 {
859 mDeviceContext->RSSetViewports(1, &dxViewport);
860
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000861 mCurViewport = actualViewport;
862 mCurNear = actualZNear;
863 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000864
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000865 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
866 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
867 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
868 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000869
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000870 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
871 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000872
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000873 mVertexConstants.depthRange[0] = actualZNear;
874 mVertexConstants.depthRange[1] = actualZFar;
875 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000876
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000877 mPixelConstants.depthRange[0] = actualZNear;
878 mPixelConstants.depthRange[1] = actualZFar;
879 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000880 }
881
882 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000883 return true;
884}
885
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000886bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
887{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000888 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000889
Geoff Lang57e713e2013-07-31 17:01:58 -0400890 GLsizei minCount = 0;
891
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000892 switch (mode)
893 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400894 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
895 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
896 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
897 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
898 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
899 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000900 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400901 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000902 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000903 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000904 }
905
Geoff Lang4c095862013-07-22 10:43:36 -0400906 if (primitiveTopology != mCurrentPrimitiveTopology)
907 {
908 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
909 mCurrentPrimitiveTopology = primitiveTopology;
910 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000911
Geoff Lang57e713e2013-07-31 17:01:58 -0400912 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000913}
914
915bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000916{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000917 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000918 // Also extract the render target dimensions and view
919 unsigned int renderTargetWidth = 0;
920 unsigned int renderTargetHeight = 0;
921 GLenum renderTargetFormat = 0;
922 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
923 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
924 bool missingColorRenderTarget = true;
925
926 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000927 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000928 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
929
930 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000931 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000932 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
933 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
934
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000935 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000936
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000937 if (!colorbuffer)
938 {
939 ERR("render target pointer unexpectedly null.");
940 return false;
941 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000942
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000943 // check for zero-sized default framebuffer, which is a special case.
944 // in this case we do not wish to modify any state and just silently return false.
945 // this will not report any gl error but will cause the calling method to return.
946 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
947 {
948 return false;
949 }
950
951 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
952
953 // Extract the render target dimensions and view
954 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
955 if (!renderTarget)
956 {
957 ERR("render target pointer unexpectedly null.");
958 return false;
959 }
960
961 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
962 if (!framebufferRTVs[colorAttachment])
963 {
964 ERR("render target view pointer unexpectedly null.");
965 return false;
966 }
967
968 if (missingColorRenderTarget)
969 {
970 renderTargetWidth = colorbuffer->getWidth();
971 renderTargetHeight = colorbuffer->getHeight();
972 renderTargetFormat = colorbuffer->getActualFormat();
973 missingColorRenderTarget = false;
974 }
Jamie Madillba597af2013-10-22 13:12:15 -0400975
976#ifdef _DEBUG
977 // Workaround for Debug SETSHADERRESOURCES_HAZARD D3D11 warnings
978 for (unsigned int vertexSerialIndex = 0; vertexSerialIndex < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; vertexSerialIndex++)
979 {
980 if (colorbuffer->getTextureSerial() != 0 && mCurVertexTextureSerials[vertexSerialIndex] == colorbuffer->getTextureSerial())
981 {
982 setTexture(gl::SAMPLER_VERTEX, vertexSerialIndex, NULL);
983 }
984 }
985
986 for (unsigned int pixelSerialIndex = 0; pixelSerialIndex < gl::MAX_TEXTURE_IMAGE_UNITS; pixelSerialIndex++)
987 {
988 if (colorbuffer->getTextureSerial() != 0 && mCurPixelTextureSerials[pixelSerialIndex] == colorbuffer->getTextureSerial())
989 {
990 setTexture(gl::SAMPLER_PIXEL, pixelSerialIndex, NULL);
991 }
992 }
993#endif
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000994 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000995 }
996
997 // Get the depth stencil render buffer and serials
998 gl::Renderbuffer *depthStencil = NULL;
999 unsigned int depthbufferSerial = 0;
1000 unsigned int stencilbufferSerial = 0;
1001 if (framebuffer->getDepthbufferType() != GL_NONE)
1002 {
1003 depthStencil = framebuffer->getDepthbuffer();
1004 if (!depthStencil)
1005 {
1006 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001007 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001008 return false;
1009 }
1010
1011 depthbufferSerial = depthStencil->getSerial();
1012 }
1013 else if (framebuffer->getStencilbufferType() != GL_NONE)
1014 {
1015 depthStencil = framebuffer->getStencilbuffer();
1016 if (!depthStencil)
1017 {
1018 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001019 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001020 return false;
1021 }
1022
1023 stencilbufferSerial = depthStencil->getSerial();
1024 }
1025
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001026 // Extract the depth stencil sizes and view
1027 unsigned int depthSize = 0;
1028 unsigned int stencilSize = 0;
1029 ID3D11DepthStencilView* framebufferDSV = NULL;
1030 if (depthStencil)
1031 {
1032 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
1033 if (!depthStencilRenderTarget)
1034 {
1035 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001036 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001037 return false;
1038 }
1039
1040 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
1041 if (!framebufferDSV)
1042 {
1043 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001044 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001045 return false;
1046 }
1047
1048 // If there is no render buffer, the width, height and format values come from
1049 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001050 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001051 {
1052 renderTargetWidth = depthStencil->getWidth();
1053 renderTargetHeight = depthStencil->getHeight();
1054 renderTargetFormat = depthStencil->getActualFormat();
1055 }
1056
1057 depthSize = depthStencil->getDepthSize();
1058 stencilSize = depthStencil->getStencilSize();
1059 }
1060
1061 // Apply the render target and depth stencil
1062 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001063 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001064 depthbufferSerial != mAppliedDepthbufferSerial ||
1065 stencilbufferSerial != mAppliedStencilbufferSerial)
1066 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001067 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001068
1069 mRenderTargetDesc.width = renderTargetWidth;
1070 mRenderTargetDesc.height = renderTargetHeight;
1071 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001072 mForceSetViewport = true;
1073 mForceSetScissor = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001074
1075 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1076 {
1077 mCurDepthSize = depthSize;
1078 mForceSetRasterState = true;
1079 }
1080
1081 mCurStencilSize = stencilSize;
1082
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001083 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1084 {
1085 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
1086 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001087 mAppliedDepthbufferSerial = depthbufferSerial;
1088 mAppliedStencilbufferSerial = stencilbufferSerial;
1089 mRenderTargetDescInitialized = true;
1090 mDepthStencilInitialized = true;
1091 }
1092
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001093 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001094}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001095
Jamie Madill57a89722013-07-02 11:57:03 -04001096GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001097 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001098{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001099 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001100 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001101 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001102 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001103 return err;
1104 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001105
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001106 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001107}
1108
daniel@transgaming.com31240482012-11-28 21:06:41 +00001109GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001110{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001111 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001112
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001113 if (err == GL_NO_ERROR)
1114 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001115 if (indexInfo->storage)
1116 {
1117 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1118 {
1119 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1120 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1121
Jamie Madill171ca0e2013-10-10 15:10:31 -04001122 mDeviceContext->IASetIndexBuffer(storage->getBuffer(false), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001123
1124 mAppliedIBSerial = 0;
1125 mAppliedStorageIBSerial = storage->getSerial();
1126 mAppliedIBOffset = indexInfo->startOffset;
1127 }
1128 }
1129 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001130 {
1131 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1132
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001133 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001134
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001135 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001136 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001137 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001138 }
1139 }
1140
1141 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001142}
1143
1144void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1145{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001146 if (mode == GL_LINE_LOOP)
1147 {
1148 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1149 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001150 else if (mode == GL_TRIANGLE_FAN)
1151 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001152 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001153 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001154 else if (instances > 0)
1155 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001156 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001157 }
1158 else
1159 {
1160 mDeviceContext->Draw(count, 0);
1161 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001162}
1163
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001164void 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 +00001165{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001166 if (mode == GL_LINE_LOOP)
1167 {
1168 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1169 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001170 else if (mode == GL_TRIANGLE_FAN)
1171 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001172 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1173 }
1174 else if (instances > 0)
1175 {
1176 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001177 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001178 else
1179 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001180 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001181 }
1182}
1183
1184void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1185{
1186 // Get the raw indices for an indexed draw
1187 if (type != GL_NONE && elementArrayBuffer)
1188 {
1189 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001190 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001191 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001192 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001193 }
1194
1195 if (!mLineLoopIB)
1196 {
1197 mLineLoopIB = new StreamingIndexBufferInterface(this);
1198 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1199 {
1200 delete mLineLoopIB;
1201 mLineLoopIB = NULL;
1202
1203 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001204 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001205 }
1206 }
1207
Geoff Lang57e713e2013-07-31 17:01:58 -04001208 // Checked by Renderer11::applyPrimitiveType
1209 ASSERT(count >= 0);
1210
1211 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001212 {
1213 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1214 return gl::error(GL_OUT_OF_MEMORY);
1215 }
1216
Geoff Lang57e713e2013-07-31 17:01:58 -04001217 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001218 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1219 {
1220 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001221 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001222 }
1223
1224 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001225 unsigned int offset;
1226 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001227 {
1228 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001229 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001230 }
1231
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001232 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001233 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001234
1235 switch (type)
1236 {
1237 case GL_NONE: // Non-indexed draw
1238 for (int i = 0; i < count; i++)
1239 {
1240 data[i] = i;
1241 }
1242 data[count] = 0;
1243 break;
1244 case GL_UNSIGNED_BYTE:
1245 for (int i = 0; i < count; i++)
1246 {
1247 data[i] = static_cast<const GLubyte*>(indices)[i];
1248 }
1249 data[count] = static_cast<const GLubyte*>(indices)[0];
1250 break;
1251 case GL_UNSIGNED_SHORT:
1252 for (int i = 0; i < count; i++)
1253 {
1254 data[i] = static_cast<const GLushort*>(indices)[i];
1255 }
1256 data[count] = static_cast<const GLushort*>(indices)[0];
1257 break;
1258 case GL_UNSIGNED_INT:
1259 for (int i = 0; i < count; i++)
1260 {
1261 data[i] = static_cast<const GLuint*>(indices)[i];
1262 }
1263 data[count] = static_cast<const GLuint*>(indices)[0];
1264 break;
1265 default: UNREACHABLE();
1266 }
1267
1268 if (!mLineLoopIB->unmapBuffer())
1269 {
1270 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001271 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001272 }
1273
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001274 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001275 {
1276 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1277
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001278 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001279 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001280 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001281 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001282 }
1283
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001284 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001285}
1286
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001287void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001288{
1289 // Get the raw indices for an indexed draw
1290 if (type != GL_NONE && elementArrayBuffer)
1291 {
1292 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001293 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001294 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001295 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001296 }
1297
1298 if (!mTriangleFanIB)
1299 {
1300 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1301 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1302 {
1303 delete mTriangleFanIB;
1304 mTriangleFanIB = NULL;
1305
1306 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001307 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001308 }
1309 }
1310
Geoff Lang57e713e2013-07-31 17:01:58 -04001311 // Checked by Renderer11::applyPrimitiveType
1312 ASSERT(count >= 3);
1313
Geoff Langeadfd572013-07-09 15:55:07 -04001314 const unsigned int numTris = count - 2;
1315
Geoff Lang57e713e2013-07-31 17:01:58 -04001316 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001317 {
1318 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1319 return gl::error(GL_OUT_OF_MEMORY);
1320 }
1321
1322 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001323 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1324 {
1325 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001326 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001327 }
1328
1329 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001330 unsigned int offset;
1331 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001332 {
1333 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001334 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001335 }
1336
1337 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001338 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001339
1340 switch (type)
1341 {
1342 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001343 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001344 {
1345 data[i*3 + 0] = 0;
1346 data[i*3 + 1] = i + 1;
1347 data[i*3 + 2] = i + 2;
1348 }
1349 break;
1350 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001351 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001352 {
1353 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1354 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1355 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1356 }
1357 break;
1358 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001359 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001360 {
1361 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1362 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1363 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1364 }
1365 break;
1366 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001367 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001368 {
1369 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1370 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1371 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1372 }
1373 break;
1374 default: UNREACHABLE();
1375 }
1376
1377 if (!mTriangleFanIB->unmapBuffer())
1378 {
1379 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001380 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001381 }
1382
1383 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1384 {
1385 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1386
1387 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1388 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001389 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001390 mAppliedIBOffset = indexBufferOffset;
1391 }
1392
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001393 if (instances > 0)
1394 {
1395 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1396 }
1397 else
1398 {
1399 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1400 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001401}
1402
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001403void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1404{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001405 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001406 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1407
1408 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001409 {
1410 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1411 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001412
daniel@transgaming.come4991412012-12-20 20:55:34 +00001413 ID3D11VertexShader *vertexShader = NULL;
1414 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001415
daniel@transgaming.come4991412012-12-20 20:55:34 +00001416 ID3D11PixelShader *pixelShader = NULL;
1417 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001418
daniel@transgaming.come4991412012-12-20 20:55:34 +00001419 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1420 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001421
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001422 programBinary->dirtyAllUniforms();
1423
1424 mAppliedProgramBinarySerial = programBinarySerial;
1425 }
1426
1427 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001428 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001429
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001430 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001431 {
1432 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001433 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001434 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1435 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001436 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1437 }
1438 else
1439 {
1440 mDeviceContext->GSSetShader(NULL, NULL, 0);
1441 }
1442
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001443 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001444 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001445}
1446
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001447void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001448{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001449 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1450 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001451
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001452 unsigned int totalRegisterCountVS = 0;
1453 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001454
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001455 bool vertexUniformsDirty = false;
1456 bool pixelUniformsDirty = false;
1457
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001458 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1459 {
1460 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001461
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001462 if (uniform->isReferencedByVertexShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001463 {
1464 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001465 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001466 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001467
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001468 if (uniform->isReferencedByFragmentShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001469 {
1470 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001471 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001472 }
1473 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001474
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001475 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1476 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1477
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001478 float (*mapVS)[4] = NULL;
1479 float (*mapPS)[4] = NULL;
1480
Shannon Woods5ab33c82013-06-26 15:31:09 -04001481 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1482 {
1483 D3D11_MAPPED_SUBRESOURCE map = {0};
1484 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1485 ASSERT(SUCCEEDED(result));
1486 mapVS = (float(*)[4])map.pData;
1487 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001488
Shannon Woods5ab33c82013-06-26 15:31:09 -04001489 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1490 {
1491 D3D11_MAPPED_SUBRESOURCE map = {0};
1492 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1493 ASSERT(SUCCEEDED(result));
1494 mapPS = (float(*)[4])map.pData;
1495 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001496
Jamie Madill5b085dc2013-08-30 13:21:11 -04001497 for (size_t uniformIndex = 0; uniformIndex < uniformArray->size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001498 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001499 gl::Uniform *uniform = (*uniformArray)[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001500
Nicolas Capense6050882013-07-08 10:43:10 -04001501 if (!gl::IsSampler(uniform->type))
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001502 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001503 unsigned int componentCount = (4 - uniform->registerElement);
1504
Jamie Madill71cc91f2013-09-18 12:51:22 -04001505 // 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 -04001506 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001507
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001508 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001509 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001510 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001511 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001512
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001513 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001514 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001515 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001516 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001517 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001518
1519 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001520 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001521
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001522 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001523 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001524 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001525 }
1526
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001527 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001528 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001529 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001530 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001531
1532 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1533 {
1534 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1535 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1536 }
1537
1538 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1539 {
1540 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1541 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1542 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001543
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001544 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001545 if (!mDriverConstantBufferVS)
1546 {
1547 D3D11_BUFFER_DESC constantBufferDescription = {0};
1548 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1549 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1550 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1551 constantBufferDescription.CPUAccessFlags = 0;
1552 constantBufferDescription.MiscFlags = 0;
1553 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001554
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001555 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001556 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001557
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001558 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1559 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001560
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001561 if (!mDriverConstantBufferPS)
1562 {
1563 D3D11_BUFFER_DESC constantBufferDescription = {0};
1564 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1565 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1566 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1567 constantBufferDescription.CPUAccessFlags = 0;
1568 constantBufferDescription.MiscFlags = 0;
1569 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001570
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001571 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001572 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001573
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001574 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1575 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001576
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001577 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1578 {
1579 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1580 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1581 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001582
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001583 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1584 {
1585 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1586 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1587 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001588
1589 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001590 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1591 {
1592 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1593 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1594 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001595}
1596
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001597void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001598{
Geoff Langda507fe2013-08-20 12:01:42 -04001599 mClear->clearFramebuffer(clearParams, frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001600}
1601
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001602void Renderer11::markAllStateDirty()
1603{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001604 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1605 {
1606 mAppliedRenderTargetSerials[rtIndex] = 0;
1607 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001608 mAppliedDepthbufferSerial = 0;
1609 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001610 mDepthStencilInitialized = false;
1611 mRenderTargetDescInitialized = false;
1612
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001613 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001614 {
1615 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001616 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001617 }
1618 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1619 {
1620 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001621 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001622 }
1623
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001624 mForceSetBlendState = true;
1625 mForceSetRasterState = true;
1626 mForceSetDepthStencilState = true;
1627 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001628 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001629
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001630 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001631 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001632 mAppliedIBOffset = 0;
1633
daniel@transgaming.come4991412012-12-20 20:55:34 +00001634 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001635 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1636 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001637
1638 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001639
1640 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1641 {
1642 mCurrentConstantBufferVS[i] = -1;
1643 mCurrentConstantBufferPS[i] = -1;
1644 }
1645
1646 mCurrentVertexConstantBuffer = NULL;
1647 mCurrentPixelConstantBuffer = NULL;
1648 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001649
1650 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001651}
1652
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001653void Renderer11::releaseDeviceResources()
1654{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001655 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001656 mInputLayoutCache.clear();
1657
Geoff Langea228632013-07-30 15:17:12 -04001658 SafeDelete(mVertexDataManager);
1659 SafeDelete(mIndexDataManager);
1660 SafeDelete(mLineLoopIB);
1661 SafeDelete(mTriangleFanIB);
1662 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001663 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001664 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001665
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001666 SafeRelease(mDriverConstantBufferVS);
1667 SafeRelease(mDriverConstantBufferPS);
1668 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001669}
1670
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001671void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001672{
1673 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001674 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001675}
1676
1677bool Renderer11::isDeviceLost()
1678{
1679 return mDeviceLost;
1680}
1681
1682// set notify to true to broadcast a message to all contexts of the device loss
1683bool Renderer11::testDeviceLost(bool notify)
1684{
1685 bool isLost = false;
1686
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001687 // GetRemovedReason is used to test if the device is removed
1688 HRESULT result = mDevice->GetDeviceRemovedReason();
1689 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001690
1691 if (isLost)
1692 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001693 // Log error if this is a new device lost event
1694 if (mDeviceLost == false)
1695 {
1696 ERR("The D3D11 device was removed: 0x%08X", result);
1697 }
1698
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001699 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001700 // we'll probably get this done again by notifyDeviceLost
1701 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001702 // Note that we don't want to clear the device loss status here
1703 // -- this needs to be done by resetDevice
1704 mDeviceLost = true;
1705 if (notify)
1706 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001707 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001708 }
1709 }
1710
1711 return isLost;
1712}
1713
1714bool Renderer11::testDeviceResettable()
1715{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001716 // determine if the device is resettable by creating a dummy device
1717 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001718
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001719 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001720 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001721 return false;
1722 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001723
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001724 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001725 {
1726 D3D_FEATURE_LEVEL_11_0,
1727 D3D_FEATURE_LEVEL_10_1,
1728 D3D_FEATURE_LEVEL_10_0,
1729 };
1730
1731 ID3D11Device* dummyDevice;
1732 D3D_FEATURE_LEVEL dummyFeatureLevel;
1733 ID3D11DeviceContext* dummyContext;
1734
1735 HRESULT result = D3D11CreateDevice(NULL,
1736 D3D_DRIVER_TYPE_HARDWARE,
1737 NULL,
1738 #if defined(_DEBUG)
1739 D3D11_CREATE_DEVICE_DEBUG,
1740 #else
1741 0,
1742 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001743 featureLevels,
1744 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001745 D3D11_SDK_VERSION,
1746 &dummyDevice,
1747 &dummyFeatureLevel,
1748 &dummyContext);
1749
1750 if (!mDevice || FAILED(result))
1751 {
1752 return false;
1753 }
1754
Geoff Langea228632013-07-30 15:17:12 -04001755 SafeRelease(dummyContext);
1756 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001757
1758 return true;
1759}
1760
1761void Renderer11::release()
1762{
1763 releaseDeviceResources();
1764
Geoff Langea228632013-07-30 15:17:12 -04001765 SafeRelease(mDxgiFactory);
1766 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001767
1768 if (mDeviceContext)
1769 {
1770 mDeviceContext->ClearState();
1771 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001772 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001773 }
1774
Geoff Langea228632013-07-30 15:17:12 -04001775 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001776
1777 if (mD3d11Module)
1778 {
1779 FreeLibrary(mD3d11Module);
1780 mD3d11Module = NULL;
1781 }
1782
1783 if (mDxgiModule)
1784 {
1785 FreeLibrary(mDxgiModule);
1786 mDxgiModule = NULL;
1787 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001788}
1789
1790bool Renderer11::resetDevice()
1791{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001792 // recreate everything
1793 release();
1794 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001795
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001796 if (result != EGL_SUCCESS)
1797 {
1798 ERR("Could not reinitialize D3D11 device: %08X", result);
1799 return false;
1800 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001801
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001802 mDeviceLost = false;
1803
1804 return true;
1805}
1806
1807DWORD Renderer11::getAdapterVendor() const
1808{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001809 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001810}
1811
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001812std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001813{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001814 std::ostringstream rendererString;
1815
1816 rendererString << mDescription;
1817 rendererString << " Direct3D11";
1818
1819 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1820 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1821
1822 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001823}
1824
1825GUID Renderer11::getAdapterIdentifier() const
1826{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001827 // Use the adapter LUID as our adapter ID
1828 // This number is local to a machine is only guaranteed to be unique between restarts
1829 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1830 GUID adapterId = {0};
1831 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1832 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001833}
1834
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001835bool Renderer11::getBGRATextureSupport() const
1836{
1837 return mBGRATextureSupport;
1838}
1839
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001840bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001841{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001842 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001843}
1844
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001845bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001846{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001847 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001848}
1849
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001850bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001851{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001852 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001853}
1854
1855bool Renderer11::getDepthTextureSupport() const
1856{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001857 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001858}
1859
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001860bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001861{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001862 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001863}
1864
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001865bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001866{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001867 return mFloat32FilterSupport;
1868}
1869
1870bool Renderer11::getFloat32TextureRenderingSupport() const
1871{
1872 return mFloat32RenderSupport;
1873}
1874
1875bool Renderer11::getFloat16TextureSupport() const
1876{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001877 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001878}
1879
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001880bool Renderer11::getFloat16TextureFilteringSupport() const
1881{
1882 return mFloat16FilterSupport;
1883}
1884
1885bool Renderer11::getFloat16TextureRenderingSupport() const
1886{
1887 return mFloat16RenderSupport;
1888}
1889
Geoff Langd42cf4e2013-06-05 16:09:17 -04001890bool Renderer11::getRGB565TextureSupport() const
1891{
1892 return false;
1893}
1894
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001895bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001896{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001897 return false;
1898}
1899
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001900bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001901{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001902 return false;
1903}
1904
Geoff Lang632192d2013-10-04 13:40:46 -04001905bool Renderer11::getRGTextureSupport() const
1906{
1907 return mRGTextureSupport;
1908}
1909
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001910bool Renderer11::getTextureFilterAnisotropySupport() const
1911{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001912 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001913}
1914
1915float Renderer11::getTextureMaxAnisotropy() const
1916{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001917 switch (mFeatureLevel)
1918 {
1919 case D3D_FEATURE_LEVEL_11_0:
1920 return D3D11_MAX_MAXANISOTROPY;
1921 case D3D_FEATURE_LEVEL_10_1:
1922 case D3D_FEATURE_LEVEL_10_0:
1923 return D3D10_MAX_MAXANISOTROPY;
1924 default: UNREACHABLE();
1925 return 0;
1926 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001927}
1928
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001929bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001930{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00001931 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001932}
1933
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001934Range Renderer11::getViewportBounds() const
1935{
1936 switch (mFeatureLevel)
1937 {
1938 case D3D_FEATURE_LEVEL_11_0:
1939 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1940 case D3D_FEATURE_LEVEL_10_1:
1941 case D3D_FEATURE_LEVEL_10_0:
1942 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1943 default: UNREACHABLE();
1944 return Range(0, 0);
1945 }
1946}
1947
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001948unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001949{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001950 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1951 switch (mFeatureLevel)
1952 {
1953 case D3D_FEATURE_LEVEL_11_0:
1954 case D3D_FEATURE_LEVEL_10_1:
1955 case D3D_FEATURE_LEVEL_10_0:
1956 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1957 default: UNREACHABLE();
1958 return 0;
1959 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001960}
1961
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001962unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1963{
1964 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1965}
1966
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001967unsigned int Renderer11::getReservedVertexUniformVectors() const
1968{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001969 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001970}
1971
1972unsigned int Renderer11::getReservedFragmentUniformVectors() const
1973{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001974 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001975}
1976
1977unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001978{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001979 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1980 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1981 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001982}
1983
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001984unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001985{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001986 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1987 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1988 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001989}
1990
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001991unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001992{
1993 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001994 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1995 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001996 switch (mFeatureLevel)
1997 {
1998 case D3D_FEATURE_LEVEL_11_0:
1999 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2000 case D3D_FEATURE_LEVEL_10_1:
2001 case D3D_FEATURE_LEVEL_10_0:
2002 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2003 default: UNREACHABLE();
2004 return 0;
2005 }
2006}
2007
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002008unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2009{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002010 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2011 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2012
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002013 switch (mFeatureLevel)
2014 {
2015 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002016 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002017 case D3D_FEATURE_LEVEL_10_1:
2018 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002019 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002020 default: UNREACHABLE();
2021 return 0;
2022 }
2023}
2024
2025unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2026{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002027 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2028 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2029
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002030 switch (mFeatureLevel)
2031 {
2032 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002033 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002034 case D3D_FEATURE_LEVEL_10_1:
2035 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002036 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002037 default: UNREACHABLE();
2038 return 0;
2039 }
2040}
2041
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002042unsigned int Renderer11::getReservedVertexUniformBuffers() const
2043{
2044 // we reserve one buffer for the application uniforms, and one for driver uniforms
2045 return 2;
2046}
2047
2048unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2049{
2050 // we reserve one buffer for the application uniforms, and one for driver uniforms
2051 return 2;
2052}
2053
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002054unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2055{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002056 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2057 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2058
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002059 switch (mFeatureLevel)
2060 {
2061 case D3D_FEATURE_LEVEL_11_0:
2062 return D3D11_SO_BUFFER_SLOT_COUNT;
2063 case D3D_FEATURE_LEVEL_10_1:
2064 case D3D_FEATURE_LEVEL_10_0:
2065 return D3D10_SO_BUFFER_SLOT_COUNT;
2066 default: UNREACHABLE();
2067 return 0;
2068 }
2069}
2070
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002071unsigned int Renderer11::getMaxUniformBufferSize() const
2072{
2073 // Each component is a 4-element vector of 4-byte units (floats)
2074 const unsigned int bytesPerComponent = 4 * sizeof(float);
2075
2076 switch (mFeatureLevel)
2077 {
2078 case D3D_FEATURE_LEVEL_11_0:
2079 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2080 case D3D_FEATURE_LEVEL_10_1:
2081 case D3D_FEATURE_LEVEL_10_0:
2082 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2083 default: UNREACHABLE();
2084 return 0;
2085 }
2086}
2087
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002088bool Renderer11::getNonPower2TextureSupport() const
2089{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002090 switch (mFeatureLevel)
2091 {
2092 case D3D_FEATURE_LEVEL_11_0:
2093 case D3D_FEATURE_LEVEL_10_1:
2094 case D3D_FEATURE_LEVEL_10_0:
2095 return true;
2096 default: UNREACHABLE();
2097 return false;
2098 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002099}
2100
2101bool Renderer11::getOcclusionQuerySupport() const
2102{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002103 switch (mFeatureLevel)
2104 {
2105 case D3D_FEATURE_LEVEL_11_0:
2106 case D3D_FEATURE_LEVEL_10_1:
2107 case D3D_FEATURE_LEVEL_10_0:
2108 return true;
2109 default: UNREACHABLE();
2110 return false;
2111 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002112}
2113
2114bool Renderer11::getInstancingSupport() const
2115{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002116 switch (mFeatureLevel)
2117 {
2118 case D3D_FEATURE_LEVEL_11_0:
2119 case D3D_FEATURE_LEVEL_10_1:
2120 case D3D_FEATURE_LEVEL_10_0:
2121 return true;
2122 default: UNREACHABLE();
2123 return false;
2124 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002125}
2126
2127bool Renderer11::getShareHandleSupport() const
2128{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002129 // We only currently support share handles with BGRA surfaces, because
2130 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002131 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002132 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002133}
2134
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002135bool Renderer11::getDerivativeInstructionSupport() const
2136{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002137 switch (mFeatureLevel)
2138 {
2139 case D3D_FEATURE_LEVEL_11_0:
2140 case D3D_FEATURE_LEVEL_10_1:
2141 case D3D_FEATURE_LEVEL_10_0:
2142 return true;
2143 default: UNREACHABLE();
2144 return false;
2145 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002146}
2147
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002148bool Renderer11::getPostSubBufferSupport() const
2149{
2150 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2151 return false;
2152}
2153
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002154int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002155{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002156 switch (mFeatureLevel)
2157 {
2158 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002159 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002160 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2161 default: UNREACHABLE(); return 0;
2162 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002163}
2164
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002165int Renderer11::getMinorShaderModel() const
2166{
2167 switch (mFeatureLevel)
2168 {
2169 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2170 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2171 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2172 default: UNREACHABLE(); return 0;
2173 }
2174}
2175
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002176float Renderer11::getMaxPointSize() const
2177{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002178 // choose a reasonable maximum. we enforce this in the shader.
2179 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2180 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002181}
2182
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002183int Renderer11::getMaxViewportDimension() const
2184{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002185 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2186 // In our case return the maximum texture size, which is the maximum render buffer size.
2187 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2188 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2189
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002190 switch (mFeatureLevel)
2191 {
2192 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002193 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002194 case D3D_FEATURE_LEVEL_10_1:
2195 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002196 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002197 default: UNREACHABLE();
2198 return 0;
2199 }
2200}
2201
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002202int Renderer11::getMaxTextureWidth() const
2203{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002204 switch (mFeatureLevel)
2205 {
2206 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2207 case D3D_FEATURE_LEVEL_10_1:
2208 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2209 default: UNREACHABLE(); return 0;
2210 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002211}
2212
2213int Renderer11::getMaxTextureHeight() const
2214{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002215 switch (mFeatureLevel)
2216 {
2217 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2218 case D3D_FEATURE_LEVEL_10_1:
2219 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2220 default: UNREACHABLE(); return 0;
2221 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002222}
2223
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002224int Renderer11::getMaxTextureDepth() const
2225{
2226 switch (mFeatureLevel)
2227 {
2228 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2229 case D3D_FEATURE_LEVEL_10_1:
2230 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2231 default: UNREACHABLE(); return 0;
2232 }
2233}
2234
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002235int Renderer11::getMaxTextureArrayLayers() const
2236{
2237 switch (mFeatureLevel)
2238 {
2239 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2240 case D3D_FEATURE_LEVEL_10_1:
2241 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2242 default: UNREACHABLE(); return 0;
2243 }
2244}
2245
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002246bool Renderer11::get32BitIndexSupport() const
2247{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002248 switch (mFeatureLevel)
2249 {
2250 case D3D_FEATURE_LEVEL_11_0:
2251 case D3D_FEATURE_LEVEL_10_1:
2252 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2253 default: UNREACHABLE(); return false;
2254 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002255}
2256
2257int Renderer11::getMinSwapInterval() const
2258{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002259 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002260}
2261
2262int Renderer11::getMaxSwapInterval() const
2263{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002264 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002265}
2266
2267int Renderer11::getMaxSupportedSamples() const
2268{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002269 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002270}
2271
Geoff Lang005df412013-10-16 14:12:50 -04002272GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002273{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002274 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002275 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2276 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2277}
2278
Geoff Lang005df412013-10-16 14:12:50 -04002279GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002280{
2281 unsigned int numCounts = 0;
2282
2283 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002284 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2285 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002286 {
2287 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2288 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2289
2290 if (iter != mMultisampleSupportMap.end())
2291 {
2292 const MultisampleSupportInfo& info = iter->second;
2293 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2294 {
2295 if (info.qualityLevels[i] > 0)
2296 {
2297 numCounts++;
2298 }
2299 }
2300 }
2301 }
2302
2303 return numCounts;
2304}
2305
Geoff Lang005df412013-10-16 14:12:50 -04002306void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002307{
2308 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002309 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2310 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2311 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002312 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002313 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002314
2315 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2316 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2317
2318 if (iter != mMultisampleSupportMap.end())
2319 {
2320 const MultisampleSupportInfo& info = iter->second;
2321 int bufPos = 0;
2322 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2323 {
2324 if (info.qualityLevels[i] > 0)
2325 {
2326 params[bufPos++] = i + 1;
2327 }
2328 }
2329 }
2330}
2331
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002332int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2333{
2334 if (requested == 0)
2335 {
2336 return 0;
2337 }
2338
2339 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2340 if (iter != mMultisampleSupportMap.end())
2341 {
2342 const MultisampleSupportInfo& info = iter->second;
2343 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2344 {
2345 if (info.qualityLevels[i] > 0)
2346 {
2347 return i + 1;
2348 }
2349 }
2350 }
2351
2352 return -1;
2353}
2354
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002355unsigned int Renderer11::getMaxRenderTargets() const
2356{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002357 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2358 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2359
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002360 switch (mFeatureLevel)
2361 {
2362 case D3D_FEATURE_LEVEL_11_0:
2363 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2364 case D3D_FEATURE_LEVEL_10_1:
2365 case D3D_FEATURE_LEVEL_10_0:
2366 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2367 default:
2368 UNREACHABLE();
2369 return 1;
2370 }
2371}
2372
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002373bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002374{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002375 if (source && dest)
2376 {
2377 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2378 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2379
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002380 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002381 return true;
2382 }
2383
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002384 return false;
2385}
2386
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002387bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002388{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002389 if (source && dest)
2390 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002391 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2392 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002393
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002394 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002395 return true;
2396 }
2397
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002398 return false;
2399}
2400
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002401bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2402{
2403 if (source && dest)
2404 {
2405 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2406 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2407
2408 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2409 return true;
2410 }
2411
2412 return false;
2413}
2414
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002415bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2416{
2417 if (source && dest)
2418 {
2419 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2420 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2421
2422 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2423 return true;
2424 }
2425
2426 return false;
2427}
2428
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002429bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002430 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002431{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002432 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002433 if (!colorbuffer)
2434 {
2435 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002436 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002437 }
2438
2439 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2440 if (!sourceRenderTarget)
2441 {
2442 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002443 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002444 }
2445
2446 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2447 if (!source)
2448 {
2449 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002450 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002451 }
2452
2453 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2454 if (!storage11)
2455 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002456 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002457 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002458 }
2459
2460 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2461 if (!destRenderTarget)
2462 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002463 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002464 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002465 }
2466
2467 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2468 if (!dest)
2469 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002470 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002471 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002472 }
2473
Geoff Langb86b9792013-06-04 16:32:05 -04002474 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2475 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002476
Geoff Langb86b9792013-06-04 16:32:05 -04002477 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2478 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002479
Geoff Langb86b9792013-06-04 16:32:05 -04002480 // Use nearest filtering because source and destination are the same size for the direct
2481 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002482 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002483 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002484
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002485 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002486}
2487
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002488bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002489 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002490{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002491 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002492 if (!colorbuffer)
2493 {
2494 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002495 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002496 }
2497
2498 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2499 if (!sourceRenderTarget)
2500 {
2501 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002502 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002503 }
2504
2505 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2506 if (!source)
2507 {
2508 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002509 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002510 }
2511
2512 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2513 if (!storage11)
2514 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002515 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002516 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002517 }
2518
Nicolas Capensb13f8662013-06-04 13:30:19 -04002519 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002520 if (!destRenderTarget)
2521 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002522 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002523 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002524 }
2525
2526 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2527 if (!dest)
2528 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002529 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002530 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002531 }
2532
Geoff Langb86b9792013-06-04 16:32:05 -04002533 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2534 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002535
Geoff Langb86b9792013-06-04 16:32:05 -04002536 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2537 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002538
Geoff Langb86b9792013-06-04 16:32:05 -04002539 // Use nearest filtering because source and destination are the same size for the direct
2540 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002541 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002542 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002543
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002544 return ret;
2545}
2546
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002547bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2548 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2549{
2550 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2551 if (!colorbuffer)
2552 {
2553 ERR("Failed to retrieve the color buffer from the frame buffer.");
2554 return gl::error(GL_OUT_OF_MEMORY, false);
2555 }
2556
2557 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2558 if (!sourceRenderTarget)
2559 {
2560 ERR("Failed to retrieve the render target from the frame buffer.");
2561 return gl::error(GL_OUT_OF_MEMORY, false);
2562 }
2563
2564 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2565 if (!source)
2566 {
2567 ERR("Failed to retrieve the render target view from the render target.");
2568 return gl::error(GL_OUT_OF_MEMORY, false);
2569 }
2570
2571 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2572 if (!storage11)
2573 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002574 ERR("Failed to retrieve the texture storage from the destination.");
2575 return gl::error(GL_OUT_OF_MEMORY, false);
2576 }
2577
2578 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2579 if (!destRenderTarget)
2580 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002581 ERR("Failed to retrieve the render target from the destination storage.");
2582 return gl::error(GL_OUT_OF_MEMORY, false);
2583 }
2584
2585 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2586 if (!dest)
2587 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002588 ERR("Failed to retrieve the render target view from the destination render target.");
2589 return gl::error(GL_OUT_OF_MEMORY, false);
2590 }
2591
Geoff Langb86b9792013-06-04 16:32:05 -04002592 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2593 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002594
Geoff Langb86b9792013-06-04 16:32:05 -04002595 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2596 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002597
Geoff Langb86b9792013-06-04 16:32:05 -04002598 // Use nearest filtering because source and destination are the same size for the direct
2599 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002600 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002601 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002602
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002603 return ret;
2604}
2605
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002606bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2607 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2608{
2609 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2610 if (!colorbuffer)
2611 {
2612 ERR("Failed to retrieve the color buffer from the frame buffer.");
2613 return gl::error(GL_OUT_OF_MEMORY, false);
2614 }
2615
2616 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2617 if (!sourceRenderTarget)
2618 {
2619 ERR("Failed to retrieve the render target from the frame buffer.");
2620 return gl::error(GL_OUT_OF_MEMORY, false);
2621 }
2622
2623 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2624 if (!source)
2625 {
2626 ERR("Failed to retrieve the render target view from the render target.");
2627 return gl::error(GL_OUT_OF_MEMORY, false);
2628 }
2629
2630 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2631 if (!storage11)
2632 {
Geoff Langea228632013-07-30 15:17:12 -04002633 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002634 ERR("Failed to retrieve the texture storage from the destination.");
2635 return gl::error(GL_OUT_OF_MEMORY, false);
2636 }
2637
2638 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2639 if (!destRenderTarget)
2640 {
Geoff Langea228632013-07-30 15:17:12 -04002641 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002642 ERR("Failed to retrieve the render target from the destination storage.");
2643 return gl::error(GL_OUT_OF_MEMORY, false);
2644 }
2645
2646 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2647 if (!dest)
2648 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002649 ERR("Failed to retrieve the render target view from the destination render target.");
2650 return gl::error(GL_OUT_OF_MEMORY, false);
2651 }
2652
Geoff Langb86b9792013-06-04 16:32:05 -04002653 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2654 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002655
Geoff Langb86b9792013-06-04 16:32:05 -04002656 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2657 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002658
Geoff Langb86b9792013-06-04 16:32:05 -04002659 // Use nearest filtering because source and destination are the same size for the direct
2660 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002661 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002662 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002663
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002664 return ret;
2665}
2666
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002667void Renderer11::unapplyRenderTargets()
2668{
2669 setOneTimeRenderTarget(NULL);
2670}
2671
2672void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2673{
2674 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2675
2676 rtvArray[0] = renderTargetView;
2677
2678 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2679
2680 // Do not preserve the serial for this one-time-use render target
2681 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2682 {
2683 mAppliedRenderTargetSerials[rtIndex] = 0;
2684 }
2685}
2686
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002687RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2688{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002689 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002690 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002691
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002692 if (depth)
2693 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002694 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002695 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002696 swapChain11->getDepthStencilTexture(),
2697 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002698 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002699 }
2700 else
2701 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002702 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002703 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002704 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002705 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002706 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002707 }
2708 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002709}
2710
Geoff Langa2d97f12013-06-11 11:44:02 -04002711RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002712{
Geoff Langa2d97f12013-06-11 11:44:02 -04002713 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002714 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002715}
2716
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002717ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002718{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002719 ShaderExecutable11 *executable = NULL;
2720
2721 switch (type)
2722 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002723 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002724 {
2725 ID3D11VertexShader *vshader = NULL;
2726 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2727 ASSERT(SUCCEEDED(result));
2728
2729 if (vshader)
2730 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002731 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002732 }
2733 }
2734 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002735 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002736 {
2737 ID3D11PixelShader *pshader = NULL;
2738 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2739 ASSERT(SUCCEEDED(result));
2740
2741 if (pshader)
2742 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002743 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002744 }
2745 }
2746 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002747 case rx::SHADER_GEOMETRY:
2748 {
2749 ID3D11GeometryShader *gshader = NULL;
2750 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2751 ASSERT(SUCCEEDED(result));
2752
2753 if (gshader)
2754 {
2755 executable = new ShaderExecutable11(function, length, gshader);
2756 }
2757 }
2758 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002759 default:
2760 UNREACHABLE();
2761 break;
2762 }
2763
2764 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002765}
2766
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002767ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002768{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002769 const char *profile = NULL;
2770
2771 switch (type)
2772 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002773 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002774 profile = "vs_4_0";
2775 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002776 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002777 profile = "ps_4_0";
2778 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002779 case rx::SHADER_GEOMETRY:
2780 profile = "gs_4_0";
2781 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002782 default:
2783 UNREACHABLE();
2784 return NULL;
2785 }
2786
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00002787 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002788 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002789 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002790 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002791 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002792
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002793 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002794 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002795
2796 return executable;
2797}
2798
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002799VertexBuffer *Renderer11::createVertexBuffer()
2800{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002801 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002802}
2803
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002804IndexBuffer *Renderer11::createIndexBuffer()
2805{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002806 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002807}
2808
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002809BufferStorage *Renderer11::createBufferStorage()
2810{
2811 return new BufferStorage11(this);
2812}
2813
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002814QueryImpl *Renderer11::createQuery(GLenum type)
2815{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002816 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002817}
2818
2819FenceImpl *Renderer11::createFence()
2820{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002821 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002822}
2823
Geoff Lang005df412013-10-16 14:12:50 -04002824bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002825{
Jamie Madill4461f092013-10-10 15:10:39 -04002826 int clientVersion = getCurrentClientVersion();
2827
2828 // We only support buffer to texture copies in ES3
2829 if (clientVersion <= 2)
2830 {
2831 return false;
2832 }
2833
2834 // sRGB formats do not work with D3D11 buffer SRVs
2835 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2836 {
2837 return false;
2838 }
2839
2840 // We cannot support direct copies to non-color-renderable formats
2841 if (!gl::IsColorRenderingSupported(internalFormat, this))
2842 {
2843 return false;
2844 }
2845
2846 // We skip all 3-channel formats since sometimes format support is missing
2847 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2848 {
2849 return false;
2850 }
2851
2852 // We don't support formats which we can't represent without conversion
2853 if (getNativeTextureFormat(internalFormat) != internalFormat)
2854 {
2855 return false;
2856 }
2857
2858 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002859}
2860
Jamie Madilla21eea32013-09-18 14:36:25 -04002861bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2862 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2863{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002864 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002865 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2866}
2867
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002868bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002869{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002870 ASSERT(colorbuffer != NULL);
2871
2872 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2873 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002874 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002875 *subresourceIndex = renderTarget->getSubresourceIndex();
2876
2877 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2878 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002879 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002880 ID3D11Resource *textureResource = NULL;
2881 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002882
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002883 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002884 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002885 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002886 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002887
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002888 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002889 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002890 return true;
2891 }
2892 else
2893 {
2894 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2895 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002896 }
2897 }
2898 }
2899 }
2900
2901 return false;
2902}
2903
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002904bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002905 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002906{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002907 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002908 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002909 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002910
2911 if (!readBuffer)
2912 {
2913 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2914 return gl::error(GL_OUT_OF_MEMORY, false);
2915 }
2916
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002917 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002918
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002919 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002920 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002921 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2922 {
2923 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
2924
2925 if (!drawBuffer)
2926 {
2927 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2928 return gl::error(GL_OUT_OF_MEMORY, false);
2929 }
2930
2931 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2932
Geoff Lang125deab2013-08-09 13:34:16 -04002933 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002934 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002935 {
2936 return false;
2937 }
2938 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002939 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002940 }
2941
Geoff Lang685806d2013-06-12 11:16:36 -04002942 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002943 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002944 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
2945 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
2946
2947 if (!readBuffer)
2948 {
2949 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2950 return gl::error(GL_OUT_OF_MEMORY, false);
2951 }
2952
2953 if (!drawBuffer)
2954 {
2955 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2956 return gl::error(GL_OUT_OF_MEMORY, false);
2957 }
2958
2959 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2960 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2961
Geoff Lang125deab2013-08-09 13:34:16 -04002962 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002963 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002964 {
2965 return false;
2966 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002967 }
2968
2969 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002970}
2971
2972void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2973 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2974{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002975 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002976 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002977
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002978 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2979
2980 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002981 {
2982 gl::Rectangle area;
2983 area.x = x;
2984 area.y = y;
2985 area.width = width;
2986 area.height = height;
2987
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002988 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
2989 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002990
Geoff Langea228632013-07-30 15:17:12 -04002991 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002992 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002993}
2994
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002995Image *Renderer11::createImage()
2996{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002997 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002998}
2999
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003000void Renderer11::generateMipmap(Image *dest, Image *src)
3001{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003002 Image11 *dest11 = Image11::makeImage11(dest);
3003 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003004 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003005}
3006
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003007TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3008{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003009 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3010 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003011}
3012
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003013TextureStorage *Renderer11::createTextureStorage2D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003014{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003015 return new TextureStorage11_2D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003016}
3017
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003018TextureStorage *Renderer11::createTextureStorageCube(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, int size)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003019{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003020 return new TextureStorage11_Cube(this, baseLevel, maxLevel, internalformat, renderTarget, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003021}
3022
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003023TextureStorage *Renderer11::createTextureStorage3D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth)
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003024{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003025 return new TextureStorage11_3D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003026}
3027
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003028TextureStorage *Renderer11::createTextureStorage2DArray(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth)
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003029{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003030 return new TextureStorage11_2DArray(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003031}
3032
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003033void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3034 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3035 GLint packAlignment, void *pixels)
3036{
3037 D3D11_TEXTURE2D_DESC textureDesc;
3038 texture->GetDesc(&textureDesc);
3039
3040 D3D11_TEXTURE2D_DESC stagingDesc;
3041 stagingDesc.Width = area.width;
3042 stagingDesc.Height = area.height;
3043 stagingDesc.MipLevels = 1;
3044 stagingDesc.ArraySize = 1;
3045 stagingDesc.Format = textureDesc.Format;
3046 stagingDesc.SampleDesc.Count = 1;
3047 stagingDesc.SampleDesc.Quality = 0;
3048 stagingDesc.Usage = D3D11_USAGE_STAGING;
3049 stagingDesc.BindFlags = 0;
3050 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3051 stagingDesc.MiscFlags = 0;
3052
3053 ID3D11Texture2D* stagingTex = NULL;
3054 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3055 if (FAILED(result))
3056 {
3057 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3058 return;
3059 }
3060
3061 ID3D11Texture2D* srcTex = NULL;
3062 if (textureDesc.SampleDesc.Count > 1)
3063 {
3064 D3D11_TEXTURE2D_DESC resolveDesc;
3065 resolveDesc.Width = textureDesc.Width;
3066 resolveDesc.Height = textureDesc.Height;
3067 resolveDesc.MipLevels = 1;
3068 resolveDesc.ArraySize = 1;
3069 resolveDesc.Format = textureDesc.Format;
3070 resolveDesc.SampleDesc.Count = 1;
3071 resolveDesc.SampleDesc.Quality = 0;
3072 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3073 resolveDesc.BindFlags = 0;
3074 resolveDesc.CPUAccessFlags = 0;
3075 resolveDesc.MiscFlags = 0;
3076
3077 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3078 if (FAILED(result))
3079 {
3080 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003081 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003082 return;
3083 }
3084
3085 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3086 subResource = 0;
3087 }
3088 else
3089 {
3090 srcTex = texture;
3091 srcTex->AddRef();
3092 }
3093
3094 D3D11_BOX srcBox;
3095 srcBox.left = area.x;
3096 srcBox.right = area.x + area.width;
3097 srcBox.top = area.y;
3098 srcBox.bottom = area.y + area.height;
3099 srcBox.front = 0;
3100 srcBox.back = 1;
3101
3102 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3103
Geoff Langea228632013-07-30 15:17:12 -04003104 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003105
3106 D3D11_MAPPED_SUBRESOURCE mapping;
3107 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3108
3109 unsigned char *source;
3110 int inputPitch;
3111 if (packReverseRowOrder)
3112 {
3113 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3114 inputPitch = -static_cast<int>(mapping.RowPitch);
3115 }
3116 else
3117 {
3118 source = static_cast<unsigned char*>(mapping.pData);
3119 inputPitch = static_cast<int>(mapping.RowPitch);
3120 }
3121
Geoff Lang697ad3e2013-06-04 10:11:28 -04003122 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003123
Geoff Lang005df412013-10-16 14:12:50 -04003124 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003125 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3126 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3127
3128 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3129
3130 if (sourceFormat == format && sourceType == type)
3131 {
3132 // Direct copy possible
3133 unsigned char *dest = static_cast<unsigned char*>(pixels);
3134 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003135 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003136 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003137 }
3138 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003139 else
3140 {
Geoff Lang005df412013-10-16 14:12:50 -04003141 GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003142 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3143
3144 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type, getCurrentClientVersion());
3145 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003146 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003147 // Fast copy is possible through some special function
3148 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003149 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003150 for (int x = 0; x < area.width; x++)
3151 {
3152 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3153 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3154
3155 fastCopyFunc(src, dest);
3156 }
3157 }
3158 }
3159 else
3160 {
Jamie Madilld6cb2442013-07-10 15:13:38 -04003161 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003162 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3163
3164 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3165 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3166 sizeof(temp) >= sizeof(gl::ColorUI) &&
3167 sizeof(temp) >= sizeof(gl::ColorI));
3168
3169 for (int y = 0; y < area.height; y++)
3170 {
3171 for (int x = 0; x < area.width; x++)
3172 {
3173 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3174 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3175
3176 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3177 // will not allow the copy otherwise.
3178 readFunc(src, temp);
3179 writeFunc(temp, dest);
3180 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003181 }
3182 }
3183 }
3184
3185 mDeviceContext->Unmap(stagingTex, 0);
3186
Geoff Langea228632013-07-30 15:17:12 -04003187 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003188}
3189
Geoff Lang758d5b22013-06-11 11:42:50 -04003190bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003191 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3192 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003193{
Geoff Lang975af372013-06-12 11:19:22 -04003194 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3195 // it should never be the case that both color and depth/stencil need to be blitted at
3196 // at the same time.
3197 ASSERT(colorBlit != (depthBlit || stencilBlit));
3198
Geoff Langc1f51be2013-06-11 11:49:14 -04003199 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003200
Geoff Lang4d782732013-07-22 10:44:18 -04003201 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3202 if (!drawRenderTarget)
3203 {
3204 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3205 return gl::error(GL_OUT_OF_MEMORY, false);
3206 }
3207
3208 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3209 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3210 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3211 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3212
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003213 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3214 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003215 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003216 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003217 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003218 }
3219
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003220 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003221 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003222 unsigned int readSubresource = 0;
3223 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003224 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003225 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3226 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003227
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003228 if (unresolvedTexture)
3229 {
3230 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3231 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003232
Geoff Langea228632013-07-30 15:17:12 -04003233 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003234
3235 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3236 if (FAILED(result))
3237 {
Geoff Langea228632013-07-30 15:17:12 -04003238 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003239 return gl::error(GL_OUT_OF_MEMORY, false);
3240 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003241 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003242 }
3243 else
3244 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003245 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003246 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003247 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003248 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003249 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003250 }
3251
Geoff Lang4d782732013-07-22 10:44:18 -04003252 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003253 {
Geoff Lang4d782732013-07-22 10:44:18 -04003254 SafeRelease(readTexture);
3255 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003256 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003257 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003258 }
3259
Geoff Lang125deab2013-08-09 13:34:16 -04003260 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3261 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3262
3263 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3264
3265 bool wholeBufferCopy = !scissorNeeded &&
3266 readRect.x == 0 && readRect.width == readSize.width &&
3267 readRect.y == 0 && readRect.height == readSize.height &&
3268 drawRect.x == 0 && drawRect.width == drawSize.width &&
3269 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003270
Geoff Langc1f51be2013-06-11 11:49:14 -04003271 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003272
Geoff Lang125deab2013-08-09 13:34:16 -04003273 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || readRect.height < 0;
3274
3275 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3276 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3277 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3278 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3279
3280 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3281 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3282 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3283
Geoff Langc1f51be2013-06-11 11:49:14 -04003284 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003285 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3286 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003287 {
Geoff Lang125deab2013-08-09 13:34:16 -04003288 UINT dstX = drawRect.x;
3289 UINT dstY = drawRect.y;
3290
Geoff Langc1f51be2013-06-11 11:49:14 -04003291 D3D11_BOX readBox;
3292 readBox.left = readRect.x;
3293 readBox.right = readRect.x + readRect.width;
3294 readBox.top = readRect.y;
3295 readBox.bottom = readRect.y + readRect.height;
3296 readBox.front = 0;
3297 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003298
Geoff Lang125deab2013-08-09 13:34:16 -04003299 if (scissorNeeded)
3300 {
3301 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3302 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3303
3304 if (drawRect.x < scissor->x)
3305 {
3306 dstX = scissor->x;
3307 readBox.left += (scissor->x - drawRect.x);
3308 }
3309 if (drawRect.y < scissor->y)
3310 {
3311 dstY = scissor->y;
3312 readBox.top += (scissor->y - drawRect.y);
3313 }
3314 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3315 {
3316 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3317 }
3318 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3319 {
3320 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3321 }
3322 }
3323
Geoff Langc1f51be2013-06-11 11:49:14 -04003324 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3325 // We also require complete framebuffer copies for depth-stencil blit.
3326 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003327
Geoff Lang125deab2013-08-09 13:34:16 -04003328 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003329 readTexture, readSubresource, pSrcBox);
3330 result = true;
3331 }
3332 else
3333 {
3334 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003335 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003336
Geoff Lang975af372013-06-12 11:19:22 -04003337 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003338 {
Geoff Lang975af372013-06-12 11:19:22 -04003339 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003340 drawTexture, drawSubresource, drawArea, drawSize,
3341 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003342 }
3343 else if (depthBlit)
3344 {
Geoff Lang125deab2013-08-09 13:34:16 -04003345 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3346 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003347 }
3348 else if (stencilBlit)
3349 {
3350 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003351 drawTexture, drawSubresource, drawArea, drawSize,
3352 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003353 }
3354 else
3355 {
Geoff Lang685806d2013-06-12 11:16:36 -04003356 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003357 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3358 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003359 }
3360 }
3361
3362 SafeRelease(readTexture);
3363 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003364
3365 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003366}
3367
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003368ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3369{
3370 D3D11_TEXTURE2D_DESC textureDesc;
3371 source->GetDesc(&textureDesc);
3372
3373 if (textureDesc.SampleDesc.Count > 1)
3374 {
3375 D3D11_TEXTURE2D_DESC resolveDesc;
3376 resolveDesc.Width = textureDesc.Width;
3377 resolveDesc.Height = textureDesc.Height;
3378 resolveDesc.MipLevels = 1;
3379 resolveDesc.ArraySize = 1;
3380 resolveDesc.Format = textureDesc.Format;
3381 resolveDesc.SampleDesc.Count = 1;
3382 resolveDesc.SampleDesc.Quality = 0;
3383 resolveDesc.Usage = textureDesc.Usage;
3384 resolveDesc.BindFlags = textureDesc.BindFlags;
3385 resolveDesc.CPUAccessFlags = 0;
3386 resolveDesc.MiscFlags = 0;
3387
3388 ID3D11Texture2D *resolveTexture = NULL;
3389 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3390 if (FAILED(result))
3391 {
3392 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3393 return NULL;
3394 }
3395
3396 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3397 return resolveTexture;
3398 }
3399 else
3400 {
3401 source->AddRef();
3402 return source;
3403 }
3404}
3405
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003406bool Renderer11::getLUID(LUID *adapterLuid) const
3407{
3408 adapterLuid->HighPart = 0;
3409 adapterLuid->LowPart = 0;
3410
3411 if (!mDxgiAdapter)
3412 {
3413 return false;
3414 }
3415
3416 DXGI_ADAPTER_DESC adapterDesc;
3417 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3418 {
3419 return false;
3420 }
3421
3422 *adapterLuid = adapterDesc.AdapterLuid;
3423 return true;
3424}
3425
Geoff Lang005df412013-10-16 14:12:50 -04003426GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003427{
3428 int clientVersion = getCurrentClientVersion();
3429 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3430}
3431
Geoff Lang61e49a52013-05-29 10:22:58 -04003432Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3433{
3434 MultisampleSupportInfo supportInfo = { 0 };
3435
3436 UINT formatSupport;
3437 HRESULT result;
3438
3439 result = mDevice->CheckFormatSupport(format, &formatSupport);
3440 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3441 {
3442 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3443 {
3444 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3445 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3446 {
3447 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3448 }
3449 else
3450 {
3451 supportInfo.qualityLevels[i - 1] = 0;
3452 }
3453 }
3454 }
3455
3456 return supportInfo;
3457}
3458
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003459}