blob: 02cadd9c8731a55fd676504d3d54f8abad971f10 [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
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000524void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
525{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000526 if (type == gl::SAMPLER_PIXEL)
527 {
528 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
529 {
530 ERR("Pixel shader sampler index %i is not valid.", index);
531 return;
532 }
533
534 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
535 {
536 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
537
538 if (!dxSamplerState)
539 {
540 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
541 "sampler state for pixel shaders at slot %i.", index);
542 }
543
544 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
545
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000546 mCurPixelSamplerStates[index] = samplerState;
547 }
548
549 mForceSetPixelSamplerStates[index] = false;
550 }
551 else if (type == gl::SAMPLER_VERTEX)
552 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000553 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000554 {
555 ERR("Vertex shader sampler index %i is not valid.", index);
556 return;
557 }
558
559 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
560 {
561 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
562
563 if (!dxSamplerState)
564 {
565 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
566 "sampler state for vertex shaders at slot %i.", index);
567 }
568
569 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
570
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000571 mCurVertexSamplerStates[index] = samplerState;
572 }
573
574 mForceSetVertexSamplerStates[index] = false;
575 }
576 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000577}
578
579void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
580{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000581 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000582 unsigned int serial = 0;
583 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000584
585 if (texture)
586 {
587 TextureStorageInterface *texStorage = texture->getNativeTexture();
588 if (texStorage)
589 {
590 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
591 textureSRV = storage11->getSRV();
592 }
593
594 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
595 // missing the shader resource view
596 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000597
598 serial = texture->getTextureSerial();
599 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000600 }
601
602 if (type == gl::SAMPLER_PIXEL)
603 {
604 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
605 {
606 ERR("Pixel shader sampler index %i is not valid.", index);
607 return;
608 }
609
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000610 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
611 {
612 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
613 }
614
615 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000616 }
617 else if (type == gl::SAMPLER_VERTEX)
618 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000619 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000620 {
621 ERR("Vertex shader sampler index %i is not valid.", index);
622 return;
623 }
624
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000625 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
626 {
627 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
628 }
629
630 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000631 }
632 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000633}
634
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000635bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
636{
637 // convert buffers to ID3D11Buffer*
638 ID3D11Buffer *vertexConstantBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = { NULL };
639 ID3D11Buffer *pixelConstantBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = { NULL };
640
641 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
642 {
643 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
644 if (uniformBuffer)
645 {
646 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Jamie Madill171ca0e2013-10-10 15:10:31 -0400647 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(true);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000648
649 if (!constantBuffer)
650 {
651 return false;
652 }
653
Geoff Langc6354ee2013-07-22 10:40:07 -0400654 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
655 {
656 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
657 1, &constantBuffer);
658 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
659 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000660 }
661 }
662
663 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
664 {
665 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
666 if (uniformBuffer)
667 {
668 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Jamie Madill171ca0e2013-10-10 15:10:31 -0400669 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(true);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000670
671 if (!constantBuffer)
672 {
673 return false;
674 }
675
Geoff Langc6354ee2013-07-22 10:40:07 -0400676 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
677 {
678 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
679 1, &constantBuffer);
680 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
681 }
682
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000683 pixelConstantBuffers[uniformBufferIndex] = constantBuffer;
684 }
685 }
686
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000687 return true;
688}
689
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000690void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000691{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000692 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000693 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000694 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
695 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000696 if (!dxRasterState)
697 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000698 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000699 "rasterizer state.");
700 }
701
702 mDeviceContext->RSSetState(dxRasterState);
703
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000704 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000705 }
706
707 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000708}
709
Geoff Lang2a64ee42013-05-31 11:22:40 -0400710void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000711 unsigned int sampleMask)
712{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000713 if (mForceSetBlendState ||
714 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400715 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000716 sampleMask != mCurSampleMask)
717 {
718 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
719 if (!dxBlendState)
720 {
721 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
722 "blend state.");
723 }
724
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000725 float blendColors[4] = {0.0f};
726 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
727 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
728 {
729 blendColors[0] = blendColor.red;
730 blendColors[1] = blendColor.green;
731 blendColors[2] = blendColor.blue;
732 blendColors[3] = blendColor.alpha;
733 }
734 else
735 {
736 blendColors[0] = blendColor.alpha;
737 blendColors[1] = blendColor.alpha;
738 blendColors[2] = blendColor.alpha;
739 blendColors[3] = blendColor.alpha;
740 }
741
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000742 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
743
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000744 mCurBlendState = blendState;
745 mCurBlendColor = blendColor;
746 mCurSampleMask = sampleMask;
747 }
748
749 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000750}
751
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000752void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000753 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000754{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000755 if (mForceSetDepthStencilState ||
756 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
757 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
758 {
759 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
760 stencilRef != stencilBackRef ||
761 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
762 {
763 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
764 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000765 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000766 }
767
768 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
769 if (!dxDepthStencilState)
770 {
771 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
772 "setting the default depth stencil state.");
773 }
774
775 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
776
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000777 mCurDepthStencilState = depthStencilState;
778 mCurStencilRef = stencilRef;
779 mCurStencilBackRef = stencilBackRef;
780 }
781
782 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000783}
784
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000785void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000786{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000787 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
788 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000789 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000790 if (enabled)
791 {
792 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000793 rect.left = std::max(0, scissor.x);
794 rect.top = std::max(0, scissor.y);
795 rect.right = scissor.x + std::max(0, scissor.width);
796 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000797
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000798 mDeviceContext->RSSetScissorRects(1, &rect);
799 }
800
801 if (enabled != mScissorEnabled)
802 {
803 mForceSetRasterState = true;
804 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000805
806 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000807 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000808 }
809
810 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000811}
812
daniel@transgaming.com12985182012-12-20 20:56:31 +0000813bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000814 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000815{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000816 gl::Rectangle actualViewport = viewport;
817 float actualZNear = gl::clamp01(zNear);
818 float actualZFar = gl::clamp01(zFar);
819 if (ignoreViewport)
820 {
821 actualViewport.x = 0;
822 actualViewport.y = 0;
823 actualViewport.width = mRenderTargetDesc.width;
824 actualViewport.height = mRenderTargetDesc.height;
825 actualZNear = 0.0f;
826 actualZFar = 1.0f;
827 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000828
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000829 // Get D3D viewport bounds, which depends on the feature level
830 const Range& viewportBounds = getViewportBounds();
831
832 // 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 +0000833 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000834 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
835 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
836 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
837 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
838 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
839 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000840 dxViewport.MinDepth = actualZNear;
841 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000842
843 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
844 {
845 return false; // Nothing to render
846 }
847
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000848 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
849 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000850
daniel@transgaming.com53670042012-11-28 20:55:51 +0000851 if (viewportChanged)
852 {
853 mDeviceContext->RSSetViewports(1, &dxViewport);
854
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000855 mCurViewport = actualViewport;
856 mCurNear = actualZNear;
857 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000858
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000859 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
860 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
861 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
862 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000863
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000864 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
865 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000866
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000867 mVertexConstants.depthRange[0] = actualZNear;
868 mVertexConstants.depthRange[1] = actualZFar;
869 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000870
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000871 mPixelConstants.depthRange[0] = actualZNear;
872 mPixelConstants.depthRange[1] = actualZFar;
873 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000874 }
875
876 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000877 return true;
878}
879
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000880bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
881{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000882 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000883
Geoff Lang57e713e2013-07-31 17:01:58 -0400884 GLsizei minCount = 0;
885
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000886 switch (mode)
887 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400888 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
889 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
890 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
891 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
892 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
893 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000894 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400895 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000896 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000897 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000898 }
899
Geoff Lang4c095862013-07-22 10:43:36 -0400900 if (primitiveTopology != mCurrentPrimitiveTopology)
901 {
902 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
903 mCurrentPrimitiveTopology = primitiveTopology;
904 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000905
Geoff Lang57e713e2013-07-31 17:01:58 -0400906 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000907}
908
909bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000910{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000911 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000912 // Also extract the render target dimensions and view
913 unsigned int renderTargetWidth = 0;
914 unsigned int renderTargetHeight = 0;
915 GLenum renderTargetFormat = 0;
916 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
917 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
918 bool missingColorRenderTarget = true;
919
920 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000921 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000922 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
923
924 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000925 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000926 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
927 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
928
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000929 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000930
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000931 if (!colorbuffer)
932 {
933 ERR("render target pointer unexpectedly null.");
934 return false;
935 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000936
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000937 // check for zero-sized default framebuffer, which is a special case.
938 // in this case we do not wish to modify any state and just silently return false.
939 // this will not report any gl error but will cause the calling method to return.
940 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
941 {
942 return false;
943 }
944
945 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
946
947 // Extract the render target dimensions and view
948 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
949 if (!renderTarget)
950 {
951 ERR("render target pointer unexpectedly null.");
952 return false;
953 }
954
955 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
956 if (!framebufferRTVs[colorAttachment])
957 {
958 ERR("render target view pointer unexpectedly null.");
959 return false;
960 }
961
962 if (missingColorRenderTarget)
963 {
964 renderTargetWidth = colorbuffer->getWidth();
965 renderTargetHeight = colorbuffer->getHeight();
966 renderTargetFormat = colorbuffer->getActualFormat();
967 missingColorRenderTarget = false;
968 }
Jamie Madillba597af2013-10-22 13:12:15 -0400969
970#ifdef _DEBUG
971 // Workaround for Debug SETSHADERRESOURCES_HAZARD D3D11 warnings
972 for (unsigned int vertexSerialIndex = 0; vertexSerialIndex < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; vertexSerialIndex++)
973 {
974 if (colorbuffer->getTextureSerial() != 0 && mCurVertexTextureSerials[vertexSerialIndex] == colorbuffer->getTextureSerial())
975 {
976 setTexture(gl::SAMPLER_VERTEX, vertexSerialIndex, NULL);
977 }
978 }
979
980 for (unsigned int pixelSerialIndex = 0; pixelSerialIndex < gl::MAX_TEXTURE_IMAGE_UNITS; pixelSerialIndex++)
981 {
982 if (colorbuffer->getTextureSerial() != 0 && mCurPixelTextureSerials[pixelSerialIndex] == colorbuffer->getTextureSerial())
983 {
984 setTexture(gl::SAMPLER_PIXEL, pixelSerialIndex, NULL);
985 }
986 }
987#endif
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000988 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000989 }
990
991 // Get the depth stencil render buffer and serials
992 gl::Renderbuffer *depthStencil = NULL;
993 unsigned int depthbufferSerial = 0;
994 unsigned int stencilbufferSerial = 0;
995 if (framebuffer->getDepthbufferType() != GL_NONE)
996 {
997 depthStencil = framebuffer->getDepthbuffer();
998 if (!depthStencil)
999 {
1000 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001001 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001002 return false;
1003 }
1004
1005 depthbufferSerial = depthStencil->getSerial();
1006 }
1007 else if (framebuffer->getStencilbufferType() != GL_NONE)
1008 {
1009 depthStencil = framebuffer->getStencilbuffer();
1010 if (!depthStencil)
1011 {
1012 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001013 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001014 return false;
1015 }
1016
1017 stencilbufferSerial = depthStencil->getSerial();
1018 }
1019
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001020 // Extract the depth stencil sizes and view
1021 unsigned int depthSize = 0;
1022 unsigned int stencilSize = 0;
1023 ID3D11DepthStencilView* framebufferDSV = NULL;
1024 if (depthStencil)
1025 {
1026 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
1027 if (!depthStencilRenderTarget)
1028 {
1029 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001030 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001031 return false;
1032 }
1033
1034 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
1035 if (!framebufferDSV)
1036 {
1037 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001038 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001039 return false;
1040 }
1041
1042 // If there is no render buffer, the width, height and format values come from
1043 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001044 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001045 {
1046 renderTargetWidth = depthStencil->getWidth();
1047 renderTargetHeight = depthStencil->getHeight();
1048 renderTargetFormat = depthStencil->getActualFormat();
1049 }
1050
1051 depthSize = depthStencil->getDepthSize();
1052 stencilSize = depthStencil->getStencilSize();
1053 }
1054
1055 // Apply the render target and depth stencil
1056 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001057 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001058 depthbufferSerial != mAppliedDepthbufferSerial ||
1059 stencilbufferSerial != mAppliedStencilbufferSerial)
1060 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001061 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001062
1063 mRenderTargetDesc.width = renderTargetWidth;
1064 mRenderTargetDesc.height = renderTargetHeight;
1065 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001066 mForceSetViewport = true;
1067 mForceSetScissor = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001068
1069 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1070 {
1071 mCurDepthSize = depthSize;
1072 mForceSetRasterState = true;
1073 }
1074
1075 mCurStencilSize = stencilSize;
1076
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001077 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1078 {
1079 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
1080 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001081 mAppliedDepthbufferSerial = depthbufferSerial;
1082 mAppliedStencilbufferSerial = stencilbufferSerial;
1083 mRenderTargetDescInitialized = true;
1084 mDepthStencilInitialized = true;
1085 }
1086
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001087 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001088}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001089
Jamie Madill57a89722013-07-02 11:57:03 -04001090GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001091 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001092{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001093 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001094 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001095 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001096 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001097 return err;
1098 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001099
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001100 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001101}
1102
daniel@transgaming.com31240482012-11-28 21:06:41 +00001103GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001104{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001105 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001106
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001107 if (err == GL_NO_ERROR)
1108 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001109 if (indexInfo->storage)
1110 {
1111 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1112 {
1113 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1114 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1115
Jamie Madill171ca0e2013-10-10 15:10:31 -04001116 mDeviceContext->IASetIndexBuffer(storage->getBuffer(false), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001117
1118 mAppliedIBSerial = 0;
1119 mAppliedStorageIBSerial = storage->getSerial();
1120 mAppliedIBOffset = indexInfo->startOffset;
1121 }
1122 }
1123 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001124 {
1125 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1126
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001127 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001128
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001129 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001130 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001131 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001132 }
1133 }
1134
1135 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001136}
1137
1138void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1139{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001140 if (mode == GL_LINE_LOOP)
1141 {
1142 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1143 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001144 else if (mode == GL_TRIANGLE_FAN)
1145 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001146 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001147 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001148 else if (instances > 0)
1149 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001150 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001151 }
1152 else
1153 {
1154 mDeviceContext->Draw(count, 0);
1155 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001156}
1157
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001158void 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 +00001159{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001160 if (mode == GL_LINE_LOOP)
1161 {
1162 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1163 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001164 else if (mode == GL_TRIANGLE_FAN)
1165 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001166 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1167 }
1168 else if (instances > 0)
1169 {
1170 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001171 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001172 else
1173 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001174 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001175 }
1176}
1177
1178void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1179{
1180 // Get the raw indices for an indexed draw
1181 if (type != GL_NONE && elementArrayBuffer)
1182 {
1183 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001184 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001185 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001186 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001187 }
1188
1189 if (!mLineLoopIB)
1190 {
1191 mLineLoopIB = new StreamingIndexBufferInterface(this);
1192 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1193 {
1194 delete mLineLoopIB;
1195 mLineLoopIB = NULL;
1196
1197 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001198 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001199 }
1200 }
1201
Geoff Lang57e713e2013-07-31 17:01:58 -04001202 // Checked by Renderer11::applyPrimitiveType
1203 ASSERT(count >= 0);
1204
1205 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001206 {
1207 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1208 return gl::error(GL_OUT_OF_MEMORY);
1209 }
1210
Geoff Lang57e713e2013-07-31 17:01:58 -04001211 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001212 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1213 {
1214 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001215 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001216 }
1217
1218 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001219 unsigned int offset;
1220 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001221 {
1222 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001223 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001224 }
1225
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001226 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001227 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001228
1229 switch (type)
1230 {
1231 case GL_NONE: // Non-indexed draw
1232 for (int i = 0; i < count; i++)
1233 {
1234 data[i] = i;
1235 }
1236 data[count] = 0;
1237 break;
1238 case GL_UNSIGNED_BYTE:
1239 for (int i = 0; i < count; i++)
1240 {
1241 data[i] = static_cast<const GLubyte*>(indices)[i];
1242 }
1243 data[count] = static_cast<const GLubyte*>(indices)[0];
1244 break;
1245 case GL_UNSIGNED_SHORT:
1246 for (int i = 0; i < count; i++)
1247 {
1248 data[i] = static_cast<const GLushort*>(indices)[i];
1249 }
1250 data[count] = static_cast<const GLushort*>(indices)[0];
1251 break;
1252 case GL_UNSIGNED_INT:
1253 for (int i = 0; i < count; i++)
1254 {
1255 data[i] = static_cast<const GLuint*>(indices)[i];
1256 }
1257 data[count] = static_cast<const GLuint*>(indices)[0];
1258 break;
1259 default: UNREACHABLE();
1260 }
1261
1262 if (!mLineLoopIB->unmapBuffer())
1263 {
1264 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001265 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001266 }
1267
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001268 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001269 {
1270 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1271
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001272 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001273 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001274 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001275 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001276 }
1277
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001278 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001279}
1280
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001281void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001282{
1283 // Get the raw indices for an indexed draw
1284 if (type != GL_NONE && elementArrayBuffer)
1285 {
1286 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001287 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001288 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001289 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001290 }
1291
1292 if (!mTriangleFanIB)
1293 {
1294 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1295 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1296 {
1297 delete mTriangleFanIB;
1298 mTriangleFanIB = NULL;
1299
1300 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001301 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001302 }
1303 }
1304
Geoff Lang57e713e2013-07-31 17:01:58 -04001305 // Checked by Renderer11::applyPrimitiveType
1306 ASSERT(count >= 3);
1307
Geoff Langeadfd572013-07-09 15:55:07 -04001308 const unsigned int numTris = count - 2;
1309
Geoff Lang57e713e2013-07-31 17:01:58 -04001310 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001311 {
1312 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1313 return gl::error(GL_OUT_OF_MEMORY);
1314 }
1315
1316 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001317 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1318 {
1319 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001320 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001321 }
1322
1323 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001324 unsigned int offset;
1325 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001326 {
1327 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001328 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001329 }
1330
1331 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001332 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001333
1334 switch (type)
1335 {
1336 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001337 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001338 {
1339 data[i*3 + 0] = 0;
1340 data[i*3 + 1] = i + 1;
1341 data[i*3 + 2] = i + 2;
1342 }
1343 break;
1344 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001345 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001346 {
1347 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1348 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1349 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1350 }
1351 break;
1352 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001353 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001354 {
1355 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1356 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1357 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1358 }
1359 break;
1360 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001361 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001362 {
1363 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1364 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1365 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1366 }
1367 break;
1368 default: UNREACHABLE();
1369 }
1370
1371 if (!mTriangleFanIB->unmapBuffer())
1372 {
1373 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001374 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001375 }
1376
1377 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1378 {
1379 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1380
1381 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1382 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001383 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001384 mAppliedIBOffset = indexBufferOffset;
1385 }
1386
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001387 if (instances > 0)
1388 {
1389 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1390 }
1391 else
1392 {
1393 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1394 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001395}
1396
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001397void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1398{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001399 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001400 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1401
1402 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001403 {
1404 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1405 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001406
daniel@transgaming.come4991412012-12-20 20:55:34 +00001407 ID3D11VertexShader *vertexShader = NULL;
1408 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001409
daniel@transgaming.come4991412012-12-20 20:55:34 +00001410 ID3D11PixelShader *pixelShader = NULL;
1411 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001412
daniel@transgaming.come4991412012-12-20 20:55:34 +00001413 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1414 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001415
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001416 programBinary->dirtyAllUniforms();
1417
1418 mAppliedProgramBinarySerial = programBinarySerial;
1419 }
1420
1421 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001422 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001423
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001424 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001425 {
1426 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001427 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001428 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1429 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001430 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1431 }
1432 else
1433 {
1434 mDeviceContext->GSSetShader(NULL, NULL, 0);
1435 }
1436
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001437 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001438 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001439}
1440
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001441void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001442{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001443 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1444 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001445
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001446 unsigned int totalRegisterCountVS = 0;
1447 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001448
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001449 bool vertexUniformsDirty = false;
1450 bool pixelUniformsDirty = false;
1451
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001452 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1453 {
1454 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001455
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001456 if (uniform->isReferencedByVertexShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001457 {
1458 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001459 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001460 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001461
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001462 if (uniform->isReferencedByFragmentShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001463 {
1464 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001465 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001466 }
1467 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001468
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001469 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1470 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1471
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001472 float (*mapVS)[4] = NULL;
1473 float (*mapPS)[4] = NULL;
1474
Shannon Woods5ab33c82013-06-26 15:31:09 -04001475 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1476 {
1477 D3D11_MAPPED_SUBRESOURCE map = {0};
1478 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1479 ASSERT(SUCCEEDED(result));
1480 mapVS = (float(*)[4])map.pData;
1481 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001482
Shannon Woods5ab33c82013-06-26 15:31:09 -04001483 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1484 {
1485 D3D11_MAPPED_SUBRESOURCE map = {0};
1486 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1487 ASSERT(SUCCEEDED(result));
1488 mapPS = (float(*)[4])map.pData;
1489 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001490
Jamie Madill5b085dc2013-08-30 13:21:11 -04001491 for (size_t uniformIndex = 0; uniformIndex < uniformArray->size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001492 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001493 gl::Uniform *uniform = (*uniformArray)[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001494
Nicolas Capense6050882013-07-08 10:43:10 -04001495 if (!gl::IsSampler(uniform->type))
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001496 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001497 unsigned int componentCount = (4 - uniform->registerElement);
1498
Jamie Madill71cc91f2013-09-18 12:51:22 -04001499 // 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 -04001500 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001501
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001502 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001503 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001504 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001505 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001506
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001507 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001508 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001509 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001510 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001511 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001512
1513 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001514 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001515
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001516 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001517 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001518 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001519 }
1520
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001521 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001522 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001523 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001524 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001525
1526 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1527 {
1528 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1529 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1530 }
1531
1532 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1533 {
1534 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1535 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1536 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001537
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001538 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001539 if (!mDriverConstantBufferVS)
1540 {
1541 D3D11_BUFFER_DESC constantBufferDescription = {0};
1542 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1543 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1544 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1545 constantBufferDescription.CPUAccessFlags = 0;
1546 constantBufferDescription.MiscFlags = 0;
1547 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001548
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001549 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001550 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001551
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001552 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1553 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001554
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001555 if (!mDriverConstantBufferPS)
1556 {
1557 D3D11_BUFFER_DESC constantBufferDescription = {0};
1558 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1559 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1560 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1561 constantBufferDescription.CPUAccessFlags = 0;
1562 constantBufferDescription.MiscFlags = 0;
1563 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001564
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001565 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001566 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001567
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001568 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1569 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001570
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001571 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1572 {
1573 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1574 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1575 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001576
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001577 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1578 {
1579 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1580 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1581 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001582
1583 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001584 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1585 {
1586 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1587 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1588 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001589}
1590
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001591void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001592{
Geoff Langda507fe2013-08-20 12:01:42 -04001593 mClear->clearFramebuffer(clearParams, frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001594}
1595
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001596void Renderer11::markAllStateDirty()
1597{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001598 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1599 {
1600 mAppliedRenderTargetSerials[rtIndex] = 0;
1601 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001602 mAppliedDepthbufferSerial = 0;
1603 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001604 mDepthStencilInitialized = false;
1605 mRenderTargetDescInitialized = false;
1606
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001607 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001608 {
1609 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001610 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001611 }
1612 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1613 {
1614 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001615 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001616 }
1617
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001618 mForceSetBlendState = true;
1619 mForceSetRasterState = true;
1620 mForceSetDepthStencilState = true;
1621 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001622 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001623
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001624 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001625 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001626 mAppliedIBOffset = 0;
1627
daniel@transgaming.come4991412012-12-20 20:55:34 +00001628 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001629 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1630 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001631
1632 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001633
1634 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1635 {
1636 mCurrentConstantBufferVS[i] = -1;
1637 mCurrentConstantBufferPS[i] = -1;
1638 }
1639
1640 mCurrentVertexConstantBuffer = NULL;
1641 mCurrentPixelConstantBuffer = NULL;
1642 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001643
1644 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001645}
1646
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001647void Renderer11::releaseDeviceResources()
1648{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001649 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001650 mInputLayoutCache.clear();
1651
Geoff Langea228632013-07-30 15:17:12 -04001652 SafeDelete(mVertexDataManager);
1653 SafeDelete(mIndexDataManager);
1654 SafeDelete(mLineLoopIB);
1655 SafeDelete(mTriangleFanIB);
1656 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001657 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001658 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001659
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001660 SafeRelease(mDriverConstantBufferVS);
1661 SafeRelease(mDriverConstantBufferPS);
1662 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001663}
1664
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001665void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001666{
1667 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001668 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001669}
1670
1671bool Renderer11::isDeviceLost()
1672{
1673 return mDeviceLost;
1674}
1675
1676// set notify to true to broadcast a message to all contexts of the device loss
1677bool Renderer11::testDeviceLost(bool notify)
1678{
1679 bool isLost = false;
1680
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001681 // GetRemovedReason is used to test if the device is removed
1682 HRESULT result = mDevice->GetDeviceRemovedReason();
1683 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001684
1685 if (isLost)
1686 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001687 // Log error if this is a new device lost event
1688 if (mDeviceLost == false)
1689 {
1690 ERR("The D3D11 device was removed: 0x%08X", result);
1691 }
1692
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001693 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001694 // we'll probably get this done again by notifyDeviceLost
1695 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001696 // Note that we don't want to clear the device loss status here
1697 // -- this needs to be done by resetDevice
1698 mDeviceLost = true;
1699 if (notify)
1700 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001701 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001702 }
1703 }
1704
1705 return isLost;
1706}
1707
1708bool Renderer11::testDeviceResettable()
1709{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001710 // determine if the device is resettable by creating a dummy device
1711 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001712
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001713 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001714 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001715 return false;
1716 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001717
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001718 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001719 {
1720 D3D_FEATURE_LEVEL_11_0,
1721 D3D_FEATURE_LEVEL_10_1,
1722 D3D_FEATURE_LEVEL_10_0,
1723 };
1724
1725 ID3D11Device* dummyDevice;
1726 D3D_FEATURE_LEVEL dummyFeatureLevel;
1727 ID3D11DeviceContext* dummyContext;
1728
1729 HRESULT result = D3D11CreateDevice(NULL,
1730 D3D_DRIVER_TYPE_HARDWARE,
1731 NULL,
1732 #if defined(_DEBUG)
1733 D3D11_CREATE_DEVICE_DEBUG,
1734 #else
1735 0,
1736 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001737 featureLevels,
1738 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001739 D3D11_SDK_VERSION,
1740 &dummyDevice,
1741 &dummyFeatureLevel,
1742 &dummyContext);
1743
1744 if (!mDevice || FAILED(result))
1745 {
1746 return false;
1747 }
1748
Geoff Langea228632013-07-30 15:17:12 -04001749 SafeRelease(dummyContext);
1750 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001751
1752 return true;
1753}
1754
1755void Renderer11::release()
1756{
1757 releaseDeviceResources();
1758
Geoff Langea228632013-07-30 15:17:12 -04001759 SafeRelease(mDxgiFactory);
1760 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001761
1762 if (mDeviceContext)
1763 {
1764 mDeviceContext->ClearState();
1765 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001766 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001767 }
1768
Geoff Langea228632013-07-30 15:17:12 -04001769 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001770
1771 if (mD3d11Module)
1772 {
1773 FreeLibrary(mD3d11Module);
1774 mD3d11Module = NULL;
1775 }
1776
1777 if (mDxgiModule)
1778 {
1779 FreeLibrary(mDxgiModule);
1780 mDxgiModule = NULL;
1781 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001782}
1783
1784bool Renderer11::resetDevice()
1785{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001786 // recreate everything
1787 release();
1788 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001789
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001790 if (result != EGL_SUCCESS)
1791 {
1792 ERR("Could not reinitialize D3D11 device: %08X", result);
1793 return false;
1794 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001795
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001796 mDeviceLost = false;
1797
1798 return true;
1799}
1800
1801DWORD Renderer11::getAdapterVendor() const
1802{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001803 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001804}
1805
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001806std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001807{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001808 std::ostringstream rendererString;
1809
1810 rendererString << mDescription;
1811 rendererString << " Direct3D11";
1812
1813 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1814 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1815
1816 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001817}
1818
1819GUID Renderer11::getAdapterIdentifier() const
1820{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001821 // Use the adapter LUID as our adapter ID
1822 // This number is local to a machine is only guaranteed to be unique between restarts
1823 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1824 GUID adapterId = {0};
1825 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1826 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001827}
1828
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001829bool Renderer11::getBGRATextureSupport() const
1830{
1831 return mBGRATextureSupport;
1832}
1833
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001834bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001835{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001836 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001837}
1838
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001839bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001840{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001841 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001842}
1843
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001844bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001845{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001846 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001847}
1848
1849bool Renderer11::getDepthTextureSupport() const
1850{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001851 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001852}
1853
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001854bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001855{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001856 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001857}
1858
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001859bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001860{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001861 return mFloat32FilterSupport;
1862}
1863
1864bool Renderer11::getFloat32TextureRenderingSupport() const
1865{
1866 return mFloat32RenderSupport;
1867}
1868
1869bool Renderer11::getFloat16TextureSupport() const
1870{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001871 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001872}
1873
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001874bool Renderer11::getFloat16TextureFilteringSupport() const
1875{
1876 return mFloat16FilterSupport;
1877}
1878
1879bool Renderer11::getFloat16TextureRenderingSupport() const
1880{
1881 return mFloat16RenderSupport;
1882}
1883
Geoff Langd42cf4e2013-06-05 16:09:17 -04001884bool Renderer11::getRGB565TextureSupport() const
1885{
1886 return false;
1887}
1888
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001889bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001890{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001891 return false;
1892}
1893
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001894bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001895{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001896 return false;
1897}
1898
Geoff Lang632192d2013-10-04 13:40:46 -04001899bool Renderer11::getRGTextureSupport() const
1900{
1901 return mRGTextureSupport;
1902}
1903
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001904bool Renderer11::getTextureFilterAnisotropySupport() const
1905{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001906 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001907}
1908
1909float Renderer11::getTextureMaxAnisotropy() const
1910{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001911 switch (mFeatureLevel)
1912 {
1913 case D3D_FEATURE_LEVEL_11_0:
1914 return D3D11_MAX_MAXANISOTROPY;
1915 case D3D_FEATURE_LEVEL_10_1:
1916 case D3D_FEATURE_LEVEL_10_0:
1917 return D3D10_MAX_MAXANISOTROPY;
1918 default: UNREACHABLE();
1919 return 0;
1920 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001921}
1922
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001923bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001924{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00001925 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001926}
1927
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001928Range Renderer11::getViewportBounds() const
1929{
1930 switch (mFeatureLevel)
1931 {
1932 case D3D_FEATURE_LEVEL_11_0:
1933 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1934 case D3D_FEATURE_LEVEL_10_1:
1935 case D3D_FEATURE_LEVEL_10_0:
1936 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1937 default: UNREACHABLE();
1938 return Range(0, 0);
1939 }
1940}
1941
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001942unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001943{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001944 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1945 switch (mFeatureLevel)
1946 {
1947 case D3D_FEATURE_LEVEL_11_0:
1948 case D3D_FEATURE_LEVEL_10_1:
1949 case D3D_FEATURE_LEVEL_10_0:
1950 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1951 default: UNREACHABLE();
1952 return 0;
1953 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001954}
1955
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001956unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1957{
1958 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1959}
1960
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001961unsigned int Renderer11::getReservedVertexUniformVectors() const
1962{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001963 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001964}
1965
1966unsigned int Renderer11::getReservedFragmentUniformVectors() const
1967{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001968 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001969}
1970
1971unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001972{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001973 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1974 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1975 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001976}
1977
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001978unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001979{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001980 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1981 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1982 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001983}
1984
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001985unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001986{
1987 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001988 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1989 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001990 switch (mFeatureLevel)
1991 {
1992 case D3D_FEATURE_LEVEL_11_0:
1993 return D3D11_VS_OUTPUT_REGISTER_COUNT;
1994 case D3D_FEATURE_LEVEL_10_1:
1995 case D3D_FEATURE_LEVEL_10_0:
1996 return D3D10_VS_OUTPUT_REGISTER_COUNT;
1997 default: UNREACHABLE();
1998 return 0;
1999 }
2000}
2001
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002002unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2003{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002004 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2005 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2006
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002007 switch (mFeatureLevel)
2008 {
2009 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002010 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002011 case D3D_FEATURE_LEVEL_10_1:
2012 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002013 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002014 default: UNREACHABLE();
2015 return 0;
2016 }
2017}
2018
2019unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2020{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002021 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2022 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2023
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002024 switch (mFeatureLevel)
2025 {
2026 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002027 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002028 case D3D_FEATURE_LEVEL_10_1:
2029 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002030 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002031 default: UNREACHABLE();
2032 return 0;
2033 }
2034}
2035
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002036unsigned int Renderer11::getReservedVertexUniformBuffers() const
2037{
2038 // we reserve one buffer for the application uniforms, and one for driver uniforms
2039 return 2;
2040}
2041
2042unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2043{
2044 // we reserve one buffer for the application uniforms, and one for driver uniforms
2045 return 2;
2046}
2047
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002048unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2049{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002050 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2051 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2052
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002053 switch (mFeatureLevel)
2054 {
2055 case D3D_FEATURE_LEVEL_11_0:
2056 return D3D11_SO_BUFFER_SLOT_COUNT;
2057 case D3D_FEATURE_LEVEL_10_1:
2058 case D3D_FEATURE_LEVEL_10_0:
2059 return D3D10_SO_BUFFER_SLOT_COUNT;
2060 default: UNREACHABLE();
2061 return 0;
2062 }
2063}
2064
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002065unsigned int Renderer11::getMaxUniformBufferSize() const
2066{
2067 // Each component is a 4-element vector of 4-byte units (floats)
2068 const unsigned int bytesPerComponent = 4 * sizeof(float);
2069
2070 switch (mFeatureLevel)
2071 {
2072 case D3D_FEATURE_LEVEL_11_0:
2073 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2074 case D3D_FEATURE_LEVEL_10_1:
2075 case D3D_FEATURE_LEVEL_10_0:
2076 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2077 default: UNREACHABLE();
2078 return 0;
2079 }
2080}
2081
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002082bool Renderer11::getNonPower2TextureSupport() const
2083{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002084 switch (mFeatureLevel)
2085 {
2086 case D3D_FEATURE_LEVEL_11_0:
2087 case D3D_FEATURE_LEVEL_10_1:
2088 case D3D_FEATURE_LEVEL_10_0:
2089 return true;
2090 default: UNREACHABLE();
2091 return false;
2092 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002093}
2094
2095bool Renderer11::getOcclusionQuerySupport() const
2096{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002097 switch (mFeatureLevel)
2098 {
2099 case D3D_FEATURE_LEVEL_11_0:
2100 case D3D_FEATURE_LEVEL_10_1:
2101 case D3D_FEATURE_LEVEL_10_0:
2102 return true;
2103 default: UNREACHABLE();
2104 return false;
2105 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002106}
2107
2108bool Renderer11::getInstancingSupport() const
2109{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002110 switch (mFeatureLevel)
2111 {
2112 case D3D_FEATURE_LEVEL_11_0:
2113 case D3D_FEATURE_LEVEL_10_1:
2114 case D3D_FEATURE_LEVEL_10_0:
2115 return true;
2116 default: UNREACHABLE();
2117 return false;
2118 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002119}
2120
2121bool Renderer11::getShareHandleSupport() const
2122{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002123 // We only currently support share handles with BGRA surfaces, because
2124 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002125 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002126 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002127}
2128
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002129bool Renderer11::getDerivativeInstructionSupport() const
2130{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002131 switch (mFeatureLevel)
2132 {
2133 case D3D_FEATURE_LEVEL_11_0:
2134 case D3D_FEATURE_LEVEL_10_1:
2135 case D3D_FEATURE_LEVEL_10_0:
2136 return true;
2137 default: UNREACHABLE();
2138 return false;
2139 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002140}
2141
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002142bool Renderer11::getPostSubBufferSupport() const
2143{
2144 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2145 return false;
2146}
2147
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002148int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002149{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002150 switch (mFeatureLevel)
2151 {
2152 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002153 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002154 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2155 default: UNREACHABLE(); return 0;
2156 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002157}
2158
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002159int Renderer11::getMinorShaderModel() const
2160{
2161 switch (mFeatureLevel)
2162 {
2163 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2164 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2165 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2166 default: UNREACHABLE(); return 0;
2167 }
2168}
2169
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002170float Renderer11::getMaxPointSize() const
2171{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002172 // choose a reasonable maximum. we enforce this in the shader.
2173 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2174 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002175}
2176
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002177int Renderer11::getMaxViewportDimension() const
2178{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002179 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2180 // In our case return the maximum texture size, which is the maximum render buffer size.
2181 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2182 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2183
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002184 switch (mFeatureLevel)
2185 {
2186 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002187 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002188 case D3D_FEATURE_LEVEL_10_1:
2189 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002190 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002191 default: UNREACHABLE();
2192 return 0;
2193 }
2194}
2195
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002196int Renderer11::getMaxTextureWidth() const
2197{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002198 switch (mFeatureLevel)
2199 {
2200 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2201 case D3D_FEATURE_LEVEL_10_1:
2202 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2203 default: UNREACHABLE(); return 0;
2204 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002205}
2206
2207int Renderer11::getMaxTextureHeight() const
2208{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002209 switch (mFeatureLevel)
2210 {
2211 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2212 case D3D_FEATURE_LEVEL_10_1:
2213 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2214 default: UNREACHABLE(); return 0;
2215 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002216}
2217
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002218int Renderer11::getMaxTextureDepth() const
2219{
2220 switch (mFeatureLevel)
2221 {
2222 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2223 case D3D_FEATURE_LEVEL_10_1:
2224 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2225 default: UNREACHABLE(); return 0;
2226 }
2227}
2228
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002229int Renderer11::getMaxTextureArrayLayers() const
2230{
2231 switch (mFeatureLevel)
2232 {
2233 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2234 case D3D_FEATURE_LEVEL_10_1:
2235 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2236 default: UNREACHABLE(); return 0;
2237 }
2238}
2239
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002240bool Renderer11::get32BitIndexSupport() const
2241{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002242 switch (mFeatureLevel)
2243 {
2244 case D3D_FEATURE_LEVEL_11_0:
2245 case D3D_FEATURE_LEVEL_10_1:
2246 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2247 default: UNREACHABLE(); return false;
2248 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002249}
2250
2251int Renderer11::getMinSwapInterval() const
2252{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002253 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002254}
2255
2256int Renderer11::getMaxSwapInterval() const
2257{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002258 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002259}
2260
2261int Renderer11::getMaxSupportedSamples() const
2262{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002263 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002264}
2265
Geoff Lang005df412013-10-16 14:12:50 -04002266GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002267{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002268 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002269 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2270 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2271}
2272
Geoff Lang005df412013-10-16 14:12:50 -04002273GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002274{
2275 unsigned int numCounts = 0;
2276
2277 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002278 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2279 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002280 {
2281 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2282 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2283
2284 if (iter != mMultisampleSupportMap.end())
2285 {
2286 const MultisampleSupportInfo& info = iter->second;
2287 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2288 {
2289 if (info.qualityLevels[i] > 0)
2290 {
2291 numCounts++;
2292 }
2293 }
2294 }
2295 }
2296
2297 return numCounts;
2298}
2299
Geoff Lang005df412013-10-16 14:12:50 -04002300void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002301{
2302 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002303 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2304 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2305 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002306 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002307 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002308
2309 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2310 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2311
2312 if (iter != mMultisampleSupportMap.end())
2313 {
2314 const MultisampleSupportInfo& info = iter->second;
2315 int bufPos = 0;
2316 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2317 {
2318 if (info.qualityLevels[i] > 0)
2319 {
2320 params[bufPos++] = i + 1;
2321 }
2322 }
2323 }
2324}
2325
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002326int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2327{
2328 if (requested == 0)
2329 {
2330 return 0;
2331 }
2332
2333 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2334 if (iter != mMultisampleSupportMap.end())
2335 {
2336 const MultisampleSupportInfo& info = iter->second;
2337 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2338 {
2339 if (info.qualityLevels[i] > 0)
2340 {
2341 return i + 1;
2342 }
2343 }
2344 }
2345
2346 return -1;
2347}
2348
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002349unsigned int Renderer11::getMaxRenderTargets() const
2350{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002351 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2352 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2353
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002354 switch (mFeatureLevel)
2355 {
2356 case D3D_FEATURE_LEVEL_11_0:
2357 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2358 case D3D_FEATURE_LEVEL_10_1:
2359 case D3D_FEATURE_LEVEL_10_0:
2360 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2361 default:
2362 UNREACHABLE();
2363 return 1;
2364 }
2365}
2366
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002367bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002368{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002369 if (source && dest)
2370 {
2371 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2372 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2373
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002374 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002375 return true;
2376 }
2377
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002378 return false;
2379}
2380
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002381bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002382{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002383 if (source && dest)
2384 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002385 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2386 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002387
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002388 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002389 return true;
2390 }
2391
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002392 return false;
2393}
2394
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002395bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2396{
2397 if (source && dest)
2398 {
2399 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2400 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2401
2402 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2403 return true;
2404 }
2405
2406 return false;
2407}
2408
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002409bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2410{
2411 if (source && dest)
2412 {
2413 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2414 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2415
2416 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2417 return true;
2418 }
2419
2420 return false;
2421}
2422
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002423bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002424 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002425{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002426 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002427 if (!colorbuffer)
2428 {
2429 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002430 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002431 }
2432
2433 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2434 if (!sourceRenderTarget)
2435 {
2436 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002437 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002438 }
2439
2440 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2441 if (!source)
2442 {
2443 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002444 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002445 }
2446
2447 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2448 if (!storage11)
2449 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002450 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002451 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002452 }
2453
2454 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2455 if (!destRenderTarget)
2456 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002457 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002458 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002459 }
2460
2461 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2462 if (!dest)
2463 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002464 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002465 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002466 }
2467
Geoff Langb86b9792013-06-04 16:32:05 -04002468 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2469 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002470
Geoff Langb86b9792013-06-04 16:32:05 -04002471 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2472 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002473
Geoff Langb86b9792013-06-04 16:32:05 -04002474 // Use nearest filtering because source and destination are the same size for the direct
2475 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002476 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002477 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002478
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002479 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002480}
2481
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002482bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002483 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002484{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002485 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002486 if (!colorbuffer)
2487 {
2488 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002489 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002490 }
2491
2492 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2493 if (!sourceRenderTarget)
2494 {
2495 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002496 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002497 }
2498
2499 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2500 if (!source)
2501 {
2502 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002503 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002504 }
2505
2506 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2507 if (!storage11)
2508 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002509 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002510 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002511 }
2512
Nicolas Capensb13f8662013-06-04 13:30:19 -04002513 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002514 if (!destRenderTarget)
2515 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002516 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002517 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002518 }
2519
2520 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2521 if (!dest)
2522 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002523 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002524 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002525 }
2526
Geoff Langb86b9792013-06-04 16:32:05 -04002527 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2528 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002529
Geoff Langb86b9792013-06-04 16:32:05 -04002530 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2531 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002532
Geoff Langb86b9792013-06-04 16:32:05 -04002533 // Use nearest filtering because source and destination are the same size for the direct
2534 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002535 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002536 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002537
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002538 return ret;
2539}
2540
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002541bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2542 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2543{
2544 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2545 if (!colorbuffer)
2546 {
2547 ERR("Failed to retrieve the color buffer from the frame buffer.");
2548 return gl::error(GL_OUT_OF_MEMORY, false);
2549 }
2550
2551 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2552 if (!sourceRenderTarget)
2553 {
2554 ERR("Failed to retrieve the render target from the frame buffer.");
2555 return gl::error(GL_OUT_OF_MEMORY, false);
2556 }
2557
2558 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2559 if (!source)
2560 {
2561 ERR("Failed to retrieve the render target view from the render target.");
2562 return gl::error(GL_OUT_OF_MEMORY, false);
2563 }
2564
2565 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2566 if (!storage11)
2567 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002568 ERR("Failed to retrieve the texture storage from the destination.");
2569 return gl::error(GL_OUT_OF_MEMORY, false);
2570 }
2571
2572 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2573 if (!destRenderTarget)
2574 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002575 ERR("Failed to retrieve the render target from the destination storage.");
2576 return gl::error(GL_OUT_OF_MEMORY, false);
2577 }
2578
2579 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2580 if (!dest)
2581 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002582 ERR("Failed to retrieve the render target view from the destination render target.");
2583 return gl::error(GL_OUT_OF_MEMORY, false);
2584 }
2585
Geoff Langb86b9792013-06-04 16:32:05 -04002586 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2587 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002588
Geoff Langb86b9792013-06-04 16:32:05 -04002589 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2590 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002591
Geoff Langb86b9792013-06-04 16:32:05 -04002592 // Use nearest filtering because source and destination are the same size for the direct
2593 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002594 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002595 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002596
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002597 return ret;
2598}
2599
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002600bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2601 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2602{
2603 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2604 if (!colorbuffer)
2605 {
2606 ERR("Failed to retrieve the color buffer from the frame buffer.");
2607 return gl::error(GL_OUT_OF_MEMORY, false);
2608 }
2609
2610 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2611 if (!sourceRenderTarget)
2612 {
2613 ERR("Failed to retrieve the render target from the frame buffer.");
2614 return gl::error(GL_OUT_OF_MEMORY, false);
2615 }
2616
2617 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2618 if (!source)
2619 {
2620 ERR("Failed to retrieve the render target view from the render target.");
2621 return gl::error(GL_OUT_OF_MEMORY, false);
2622 }
2623
2624 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2625 if (!storage11)
2626 {
Geoff Langea228632013-07-30 15:17:12 -04002627 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002628 ERR("Failed to retrieve the texture storage from the destination.");
2629 return gl::error(GL_OUT_OF_MEMORY, false);
2630 }
2631
2632 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2633 if (!destRenderTarget)
2634 {
Geoff Langea228632013-07-30 15:17:12 -04002635 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002636 ERR("Failed to retrieve the render target from the destination storage.");
2637 return gl::error(GL_OUT_OF_MEMORY, false);
2638 }
2639
2640 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2641 if (!dest)
2642 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002643 ERR("Failed to retrieve the render target view from the destination render target.");
2644 return gl::error(GL_OUT_OF_MEMORY, false);
2645 }
2646
Geoff Langb86b9792013-06-04 16:32:05 -04002647 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2648 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002649
Geoff Langb86b9792013-06-04 16:32:05 -04002650 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2651 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002652
Geoff Langb86b9792013-06-04 16:32:05 -04002653 // Use nearest filtering because source and destination are the same size for the direct
2654 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002655 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002656 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002657
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002658 return ret;
2659}
2660
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002661void Renderer11::unapplyRenderTargets()
2662{
2663 setOneTimeRenderTarget(NULL);
2664}
2665
2666void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2667{
2668 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2669
2670 rtvArray[0] = renderTargetView;
2671
2672 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2673
2674 // Do not preserve the serial for this one-time-use render target
2675 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2676 {
2677 mAppliedRenderTargetSerials[rtIndex] = 0;
2678 }
2679}
2680
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002681RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2682{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002683 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002684 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002685
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002686 if (depth)
2687 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002688 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002689 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002690 swapChain11->getDepthStencilTexture(),
2691 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002692 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002693 }
2694 else
2695 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002696 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002697 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002698 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002699 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002700 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002701 }
2702 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002703}
2704
Geoff Langa2d97f12013-06-11 11:44:02 -04002705RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002706{
Geoff Langa2d97f12013-06-11 11:44:02 -04002707 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002708 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002709}
2710
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002711ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002712{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002713 ShaderExecutable11 *executable = NULL;
2714
2715 switch (type)
2716 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002717 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002718 {
2719 ID3D11VertexShader *vshader = NULL;
2720 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2721 ASSERT(SUCCEEDED(result));
2722
2723 if (vshader)
2724 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002725 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002726 }
2727 }
2728 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002729 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002730 {
2731 ID3D11PixelShader *pshader = NULL;
2732 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2733 ASSERT(SUCCEEDED(result));
2734
2735 if (pshader)
2736 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002737 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002738 }
2739 }
2740 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002741 case rx::SHADER_GEOMETRY:
2742 {
2743 ID3D11GeometryShader *gshader = NULL;
2744 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2745 ASSERT(SUCCEEDED(result));
2746
2747 if (gshader)
2748 {
2749 executable = new ShaderExecutable11(function, length, gshader);
2750 }
2751 }
2752 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002753 default:
2754 UNREACHABLE();
2755 break;
2756 }
2757
2758 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002759}
2760
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002761ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002762{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002763 const char *profile = NULL;
2764
2765 switch (type)
2766 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002767 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002768 profile = "vs_4_0";
2769 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002770 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002771 profile = "ps_4_0";
2772 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002773 case rx::SHADER_GEOMETRY:
2774 profile = "gs_4_0";
2775 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002776 default:
2777 UNREACHABLE();
2778 return NULL;
2779 }
2780
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00002781 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002782 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002783 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002784 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002785 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002786
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002787 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002788 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002789
2790 return executable;
2791}
2792
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002793VertexBuffer *Renderer11::createVertexBuffer()
2794{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002795 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002796}
2797
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002798IndexBuffer *Renderer11::createIndexBuffer()
2799{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002800 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002801}
2802
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002803BufferStorage *Renderer11::createBufferStorage()
2804{
2805 return new BufferStorage11(this);
2806}
2807
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002808QueryImpl *Renderer11::createQuery(GLenum type)
2809{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002810 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002811}
2812
2813FenceImpl *Renderer11::createFence()
2814{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002815 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002816}
2817
Geoff Lang005df412013-10-16 14:12:50 -04002818bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002819{
Jamie Madill4461f092013-10-10 15:10:39 -04002820 int clientVersion = getCurrentClientVersion();
2821
2822 // We only support buffer to texture copies in ES3
2823 if (clientVersion <= 2)
2824 {
2825 return false;
2826 }
2827
2828 // sRGB formats do not work with D3D11 buffer SRVs
2829 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2830 {
2831 return false;
2832 }
2833
2834 // We cannot support direct copies to non-color-renderable formats
2835 if (!gl::IsColorRenderingSupported(internalFormat, this))
2836 {
2837 return false;
2838 }
2839
2840 // We skip all 3-channel formats since sometimes format support is missing
2841 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2842 {
2843 return false;
2844 }
2845
2846 // We don't support formats which we can't represent without conversion
2847 if (getNativeTextureFormat(internalFormat) != internalFormat)
2848 {
2849 return false;
2850 }
2851
2852 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002853}
2854
Jamie Madilla21eea32013-09-18 14:36:25 -04002855bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2856 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2857{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002858 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002859 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2860}
2861
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002862bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002863{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002864 ASSERT(colorbuffer != NULL);
2865
2866 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2867 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002868 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002869 *subresourceIndex = renderTarget->getSubresourceIndex();
2870
2871 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2872 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002873 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002874 ID3D11Resource *textureResource = NULL;
2875 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002876
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002877 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002878 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002879 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002880 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002881
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002882 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002883 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002884 return true;
2885 }
2886 else
2887 {
2888 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2889 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002890 }
2891 }
2892 }
2893 }
2894
2895 return false;
2896}
2897
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002898bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002899 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002900{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002901 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002902 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002903 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002904
2905 if (!readBuffer)
2906 {
2907 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2908 return gl::error(GL_OUT_OF_MEMORY, false);
2909 }
2910
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002911 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002912
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002913 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002914 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002915 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2916 {
2917 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
2918
2919 if (!drawBuffer)
2920 {
2921 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2922 return gl::error(GL_OUT_OF_MEMORY, false);
2923 }
2924
2925 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2926
Geoff Lang125deab2013-08-09 13:34:16 -04002927 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002928 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002929 {
2930 return false;
2931 }
2932 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002933 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002934 }
2935
Geoff Lang685806d2013-06-12 11:16:36 -04002936 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002937 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002938 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
2939 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
2940
2941 if (!readBuffer)
2942 {
2943 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2944 return gl::error(GL_OUT_OF_MEMORY, false);
2945 }
2946
2947 if (!drawBuffer)
2948 {
2949 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2950 return gl::error(GL_OUT_OF_MEMORY, false);
2951 }
2952
2953 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2954 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2955
Geoff Lang125deab2013-08-09 13:34:16 -04002956 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002957 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002958 {
2959 return false;
2960 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002961 }
2962
2963 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002964}
2965
2966void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2967 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2968{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002969 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002970 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002971
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002972 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2973
2974 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002975 {
2976 gl::Rectangle area;
2977 area.x = x;
2978 area.y = y;
2979 area.width = width;
2980 area.height = height;
2981
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002982 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
2983 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002984
Geoff Langea228632013-07-30 15:17:12 -04002985 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002986 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002987}
2988
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002989Image *Renderer11::createImage()
2990{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002991 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002992}
2993
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002994void Renderer11::generateMipmap(Image *dest, Image *src)
2995{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002996 Image11 *dest11 = Image11::makeImage11(dest);
2997 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04002998 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002999}
3000
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003001TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3002{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003003 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3004 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003005}
3006
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003007TextureStorage *Renderer11::createTextureStorage2D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003008{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003009 return new TextureStorage11_2D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003010}
3011
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003012TextureStorage *Renderer11::createTextureStorageCube(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, int size)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003013{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003014 return new TextureStorage11_Cube(this, baseLevel, maxLevel, internalformat, renderTarget, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003015}
3016
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003017TextureStorage *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 +00003018{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003019 return new TextureStorage11_3D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003020}
3021
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003022TextureStorage *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 +00003023{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003024 return new TextureStorage11_2DArray(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003025}
3026
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003027void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3028 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3029 GLint packAlignment, void *pixels)
3030{
3031 D3D11_TEXTURE2D_DESC textureDesc;
3032 texture->GetDesc(&textureDesc);
3033
3034 D3D11_TEXTURE2D_DESC stagingDesc;
3035 stagingDesc.Width = area.width;
3036 stagingDesc.Height = area.height;
3037 stagingDesc.MipLevels = 1;
3038 stagingDesc.ArraySize = 1;
3039 stagingDesc.Format = textureDesc.Format;
3040 stagingDesc.SampleDesc.Count = 1;
3041 stagingDesc.SampleDesc.Quality = 0;
3042 stagingDesc.Usage = D3D11_USAGE_STAGING;
3043 stagingDesc.BindFlags = 0;
3044 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3045 stagingDesc.MiscFlags = 0;
3046
3047 ID3D11Texture2D* stagingTex = NULL;
3048 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3049 if (FAILED(result))
3050 {
3051 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3052 return;
3053 }
3054
3055 ID3D11Texture2D* srcTex = NULL;
3056 if (textureDesc.SampleDesc.Count > 1)
3057 {
3058 D3D11_TEXTURE2D_DESC resolveDesc;
3059 resolveDesc.Width = textureDesc.Width;
3060 resolveDesc.Height = textureDesc.Height;
3061 resolveDesc.MipLevels = 1;
3062 resolveDesc.ArraySize = 1;
3063 resolveDesc.Format = textureDesc.Format;
3064 resolveDesc.SampleDesc.Count = 1;
3065 resolveDesc.SampleDesc.Quality = 0;
3066 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3067 resolveDesc.BindFlags = 0;
3068 resolveDesc.CPUAccessFlags = 0;
3069 resolveDesc.MiscFlags = 0;
3070
3071 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3072 if (FAILED(result))
3073 {
3074 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003075 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003076 return;
3077 }
3078
3079 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3080 subResource = 0;
3081 }
3082 else
3083 {
3084 srcTex = texture;
3085 srcTex->AddRef();
3086 }
3087
3088 D3D11_BOX srcBox;
3089 srcBox.left = area.x;
3090 srcBox.right = area.x + area.width;
3091 srcBox.top = area.y;
3092 srcBox.bottom = area.y + area.height;
3093 srcBox.front = 0;
3094 srcBox.back = 1;
3095
3096 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3097
Geoff Langea228632013-07-30 15:17:12 -04003098 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003099
3100 D3D11_MAPPED_SUBRESOURCE mapping;
3101 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3102
3103 unsigned char *source;
3104 int inputPitch;
3105 if (packReverseRowOrder)
3106 {
3107 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3108 inputPitch = -static_cast<int>(mapping.RowPitch);
3109 }
3110 else
3111 {
3112 source = static_cast<unsigned char*>(mapping.pData);
3113 inputPitch = static_cast<int>(mapping.RowPitch);
3114 }
3115
Geoff Lang697ad3e2013-06-04 10:11:28 -04003116 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003117
Geoff Lang005df412013-10-16 14:12:50 -04003118 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003119 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3120 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3121
3122 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3123
3124 if (sourceFormat == format && sourceType == type)
3125 {
3126 // Direct copy possible
3127 unsigned char *dest = static_cast<unsigned char*>(pixels);
3128 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003129 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003130 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003131 }
3132 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003133 else
3134 {
Geoff Lang005df412013-10-16 14:12:50 -04003135 GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003136 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3137
3138 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type, getCurrentClientVersion());
3139 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003140 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003141 // Fast copy is possible through some special function
3142 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003143 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003144 for (int x = 0; x < area.width; x++)
3145 {
3146 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3147 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3148
3149 fastCopyFunc(src, dest);
3150 }
3151 }
3152 }
3153 else
3154 {
Jamie Madilld6cb2442013-07-10 15:13:38 -04003155 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003156 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3157
3158 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3159 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3160 sizeof(temp) >= sizeof(gl::ColorUI) &&
3161 sizeof(temp) >= sizeof(gl::ColorI));
3162
3163 for (int y = 0; y < area.height; y++)
3164 {
3165 for (int x = 0; x < area.width; x++)
3166 {
3167 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3168 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3169
3170 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3171 // will not allow the copy otherwise.
3172 readFunc(src, temp);
3173 writeFunc(temp, dest);
3174 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003175 }
3176 }
3177 }
3178
3179 mDeviceContext->Unmap(stagingTex, 0);
3180
Geoff Langea228632013-07-30 15:17:12 -04003181 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003182}
3183
Geoff Lang758d5b22013-06-11 11:42:50 -04003184bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003185 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3186 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003187{
Geoff Lang975af372013-06-12 11:19:22 -04003188 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3189 // it should never be the case that both color and depth/stencil need to be blitted at
3190 // at the same time.
3191 ASSERT(colorBlit != (depthBlit || stencilBlit));
3192
Geoff Langc1f51be2013-06-11 11:49:14 -04003193 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003194
Geoff Lang4d782732013-07-22 10:44:18 -04003195 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3196 if (!drawRenderTarget)
3197 {
3198 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3199 return gl::error(GL_OUT_OF_MEMORY, false);
3200 }
3201
3202 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3203 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3204 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3205 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3206
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003207 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3208 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003209 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003210 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003211 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003212 }
3213
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003214 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003215 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003216 unsigned int readSubresource = 0;
3217 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003218 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003219 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3220 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003221
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003222 if (unresolvedTexture)
3223 {
3224 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3225 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003226
Geoff Langea228632013-07-30 15:17:12 -04003227 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003228
3229 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3230 if (FAILED(result))
3231 {
Geoff Langea228632013-07-30 15:17:12 -04003232 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003233 return gl::error(GL_OUT_OF_MEMORY, false);
3234 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003235 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003236 }
3237 else
3238 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003239 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003240 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003241 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003242 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003243 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003244 }
3245
Geoff Lang4d782732013-07-22 10:44:18 -04003246 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003247 {
Geoff Lang4d782732013-07-22 10:44:18 -04003248 SafeRelease(readTexture);
3249 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003250 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003251 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003252 }
3253
Geoff Lang125deab2013-08-09 13:34:16 -04003254 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3255 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3256
3257 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3258
3259 bool wholeBufferCopy = !scissorNeeded &&
3260 readRect.x == 0 && readRect.width == readSize.width &&
3261 readRect.y == 0 && readRect.height == readSize.height &&
3262 drawRect.x == 0 && drawRect.width == drawSize.width &&
3263 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003264
Geoff Langc1f51be2013-06-11 11:49:14 -04003265 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003266
Geoff Lang125deab2013-08-09 13:34:16 -04003267 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || readRect.height < 0;
3268
3269 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3270 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3271 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3272 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3273
3274 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3275 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3276 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3277
Geoff Langc1f51be2013-06-11 11:49:14 -04003278 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003279 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3280 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003281 {
Geoff Lang125deab2013-08-09 13:34:16 -04003282 UINT dstX = drawRect.x;
3283 UINT dstY = drawRect.y;
3284
Geoff Langc1f51be2013-06-11 11:49:14 -04003285 D3D11_BOX readBox;
3286 readBox.left = readRect.x;
3287 readBox.right = readRect.x + readRect.width;
3288 readBox.top = readRect.y;
3289 readBox.bottom = readRect.y + readRect.height;
3290 readBox.front = 0;
3291 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003292
Geoff Lang125deab2013-08-09 13:34:16 -04003293 if (scissorNeeded)
3294 {
3295 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3296 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3297
3298 if (drawRect.x < scissor->x)
3299 {
3300 dstX = scissor->x;
3301 readBox.left += (scissor->x - drawRect.x);
3302 }
3303 if (drawRect.y < scissor->y)
3304 {
3305 dstY = scissor->y;
3306 readBox.top += (scissor->y - drawRect.y);
3307 }
3308 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3309 {
3310 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3311 }
3312 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3313 {
3314 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3315 }
3316 }
3317
Geoff Langc1f51be2013-06-11 11:49:14 -04003318 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3319 // We also require complete framebuffer copies for depth-stencil blit.
3320 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003321
Geoff Lang125deab2013-08-09 13:34:16 -04003322 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003323 readTexture, readSubresource, pSrcBox);
3324 result = true;
3325 }
3326 else
3327 {
3328 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003329 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003330
Geoff Lang975af372013-06-12 11:19:22 -04003331 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003332 {
Geoff Lang975af372013-06-12 11:19:22 -04003333 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003334 drawTexture, drawSubresource, drawArea, drawSize,
3335 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003336 }
3337 else if (depthBlit)
3338 {
Geoff Lang125deab2013-08-09 13:34:16 -04003339 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3340 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003341 }
3342 else if (stencilBlit)
3343 {
3344 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003345 drawTexture, drawSubresource, drawArea, drawSize,
3346 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003347 }
3348 else
3349 {
Geoff Lang685806d2013-06-12 11:16:36 -04003350 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003351 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3352 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003353 }
3354 }
3355
3356 SafeRelease(readTexture);
3357 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003358
3359 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003360}
3361
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003362ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3363{
3364 D3D11_TEXTURE2D_DESC textureDesc;
3365 source->GetDesc(&textureDesc);
3366
3367 if (textureDesc.SampleDesc.Count > 1)
3368 {
3369 D3D11_TEXTURE2D_DESC resolveDesc;
3370 resolveDesc.Width = textureDesc.Width;
3371 resolveDesc.Height = textureDesc.Height;
3372 resolveDesc.MipLevels = 1;
3373 resolveDesc.ArraySize = 1;
3374 resolveDesc.Format = textureDesc.Format;
3375 resolveDesc.SampleDesc.Count = 1;
3376 resolveDesc.SampleDesc.Quality = 0;
3377 resolveDesc.Usage = textureDesc.Usage;
3378 resolveDesc.BindFlags = textureDesc.BindFlags;
3379 resolveDesc.CPUAccessFlags = 0;
3380 resolveDesc.MiscFlags = 0;
3381
3382 ID3D11Texture2D *resolveTexture = NULL;
3383 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3384 if (FAILED(result))
3385 {
3386 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3387 return NULL;
3388 }
3389
3390 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3391 return resolveTexture;
3392 }
3393 else
3394 {
3395 source->AddRef();
3396 return source;
3397 }
3398}
3399
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003400bool Renderer11::getLUID(LUID *adapterLuid) const
3401{
3402 adapterLuid->HighPart = 0;
3403 adapterLuid->LowPart = 0;
3404
3405 if (!mDxgiAdapter)
3406 {
3407 return false;
3408 }
3409
3410 DXGI_ADAPTER_DESC adapterDesc;
3411 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3412 {
3413 return false;
3414 }
3415
3416 *adapterLuid = adapterDesc.AdapterLuid;
3417 return true;
3418}
3419
Geoff Lang005df412013-10-16 14:12:50 -04003420GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003421{
3422 int clientVersion = getCurrentClientVersion();
3423 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3424}
3425
Geoff Lang61e49a52013-05-29 10:22:58 -04003426Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3427{
3428 MultisampleSupportInfo supportInfo = { 0 };
3429
3430 UINT formatSupport;
3431 HRESULT result;
3432
3433 result = mDevice->CheckFormatSupport(format, &formatSupport);
3434 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3435 {
3436 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3437 {
3438 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3439 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3440 {
3441 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3442 }
3443 else
3444 {
3445 supportInfo.qualityLevels[i - 1] = 0;
3446 }
3447 }
3448 }
3449
3450 return supportInfo;
3451}
3452
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003453}