blob: 1f180d2695ec96d338341a2e1da4325b15451324 [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());
Geoff Lang644bbf22013-09-17 17:02:43 -0400591 textureSRV = storage11->getSRV(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
592 texture->getSwizzleAlpha());
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000593 }
594
595 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
596 // missing the shader resource view
597 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000598
599 serial = texture->getTextureSerial();
600 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000601 }
602
603 if (type == gl::SAMPLER_PIXEL)
604 {
605 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
606 {
607 ERR("Pixel shader sampler index %i is not valid.", index);
608 return;
609 }
610
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000611 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
612 {
613 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
614 }
615
616 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000617 }
618 else if (type == gl::SAMPLER_VERTEX)
619 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000620 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000621 {
622 ERR("Vertex shader sampler index %i is not valid.", index);
623 return;
624 }
625
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000626 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
627 {
628 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
629 }
630
631 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000632 }
633 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000634}
635
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000636bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
637{
638 // convert buffers to ID3D11Buffer*
639 ID3D11Buffer *vertexConstantBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = { NULL };
640 ID3D11Buffer *pixelConstantBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = { NULL };
641
642 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
643 {
644 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
645 if (uniformBuffer)
646 {
647 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Jamie Madill171ca0e2013-10-10 15:10:31 -0400648 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(true);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000649
650 if (!constantBuffer)
651 {
652 return false;
653 }
654
Geoff Langc6354ee2013-07-22 10:40:07 -0400655 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
656 {
657 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
658 1, &constantBuffer);
659 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
660 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000661 }
662 }
663
664 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
665 {
666 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
667 if (uniformBuffer)
668 {
669 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Jamie Madill171ca0e2013-10-10 15:10:31 -0400670 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(true);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000671
672 if (!constantBuffer)
673 {
674 return false;
675 }
676
Geoff Langc6354ee2013-07-22 10:40:07 -0400677 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
678 {
679 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
680 1, &constantBuffer);
681 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
682 }
683
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000684 pixelConstantBuffers[uniformBufferIndex] = constantBuffer;
685 }
686 }
687
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000688 return true;
689}
690
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000691void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000692{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000693 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000694 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000695 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
696 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000697 if (!dxRasterState)
698 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000699 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000700 "rasterizer state.");
701 }
702
703 mDeviceContext->RSSetState(dxRasterState);
704
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000705 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000706 }
707
708 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000709}
710
Geoff Lang2a64ee42013-05-31 11:22:40 -0400711void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000712 unsigned int sampleMask)
713{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000714 if (mForceSetBlendState ||
715 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400716 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000717 sampleMask != mCurSampleMask)
718 {
719 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
720 if (!dxBlendState)
721 {
722 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
723 "blend state.");
724 }
725
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000726 float blendColors[4] = {0.0f};
727 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
728 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
729 {
730 blendColors[0] = blendColor.red;
731 blendColors[1] = blendColor.green;
732 blendColors[2] = blendColor.blue;
733 blendColors[3] = blendColor.alpha;
734 }
735 else
736 {
737 blendColors[0] = blendColor.alpha;
738 blendColors[1] = blendColor.alpha;
739 blendColors[2] = blendColor.alpha;
740 blendColors[3] = blendColor.alpha;
741 }
742
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000743 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
744
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000745 mCurBlendState = blendState;
746 mCurBlendColor = blendColor;
747 mCurSampleMask = sampleMask;
748 }
749
750 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000751}
752
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000753void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000754 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000755{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000756 if (mForceSetDepthStencilState ||
757 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
758 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
759 {
760 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
761 stencilRef != stencilBackRef ||
762 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
763 {
764 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
765 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000766 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000767 }
768
769 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
770 if (!dxDepthStencilState)
771 {
772 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
773 "setting the default depth stencil state.");
774 }
775
776 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
777
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000778 mCurDepthStencilState = depthStencilState;
779 mCurStencilRef = stencilRef;
780 mCurStencilBackRef = stencilBackRef;
781 }
782
783 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000784}
785
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000786void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000787{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000788 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
789 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000790 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000791 if (enabled)
792 {
793 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000794 rect.left = std::max(0, scissor.x);
795 rect.top = std::max(0, scissor.y);
796 rect.right = scissor.x + std::max(0, scissor.width);
797 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000798
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000799 mDeviceContext->RSSetScissorRects(1, &rect);
800 }
801
802 if (enabled != mScissorEnabled)
803 {
804 mForceSetRasterState = true;
805 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000806
807 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000808 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000809 }
810
811 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000812}
813
daniel@transgaming.com12985182012-12-20 20:56:31 +0000814bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000815 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000816{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000817 gl::Rectangle actualViewport = viewport;
818 float actualZNear = gl::clamp01(zNear);
819 float actualZFar = gl::clamp01(zFar);
820 if (ignoreViewport)
821 {
822 actualViewport.x = 0;
823 actualViewport.y = 0;
824 actualViewport.width = mRenderTargetDesc.width;
825 actualViewport.height = mRenderTargetDesc.height;
826 actualZNear = 0.0f;
827 actualZFar = 1.0f;
828 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000829
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000830 // Get D3D viewport bounds, which depends on the feature level
831 const Range& viewportBounds = getViewportBounds();
832
833 // 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 +0000834 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000835 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
836 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
837 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
838 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
839 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
840 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000841 dxViewport.MinDepth = actualZNear;
842 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000843
844 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
845 {
846 return false; // Nothing to render
847 }
848
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000849 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
850 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000851
daniel@transgaming.com53670042012-11-28 20:55:51 +0000852 if (viewportChanged)
853 {
854 mDeviceContext->RSSetViewports(1, &dxViewport);
855
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000856 mCurViewport = actualViewport;
857 mCurNear = actualZNear;
858 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000859
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000860 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
861 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
862 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
863 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000864
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000865 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
866 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000867
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000868 mVertexConstants.depthRange[0] = actualZNear;
869 mVertexConstants.depthRange[1] = actualZFar;
870 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000871
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000872 mPixelConstants.depthRange[0] = actualZNear;
873 mPixelConstants.depthRange[1] = actualZFar;
874 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000875 }
876
877 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000878 return true;
879}
880
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000881bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
882{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000883 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000884
Geoff Lang57e713e2013-07-31 17:01:58 -0400885 GLsizei minCount = 0;
886
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000887 switch (mode)
888 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400889 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
890 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
891 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
892 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
893 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
894 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000895 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400896 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000897 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000898 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000899 }
900
Geoff Lang4c095862013-07-22 10:43:36 -0400901 if (primitiveTopology != mCurrentPrimitiveTopology)
902 {
903 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
904 mCurrentPrimitiveTopology = primitiveTopology;
905 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000906
Geoff Lang57e713e2013-07-31 17:01:58 -0400907 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000908}
909
910bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000911{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000912 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000913 // Also extract the render target dimensions and view
914 unsigned int renderTargetWidth = 0;
915 unsigned int renderTargetHeight = 0;
916 GLenum renderTargetFormat = 0;
917 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
918 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
919 bool missingColorRenderTarget = true;
920
921 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000922 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000923 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
924
925 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000926 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000927 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
928 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
929
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000930 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000931
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000932 if (!colorbuffer)
933 {
934 ERR("render target pointer unexpectedly null.");
935 return false;
936 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000937
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000938 // check for zero-sized default framebuffer, which is a special case.
939 // in this case we do not wish to modify any state and just silently return false.
940 // this will not report any gl error but will cause the calling method to return.
941 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
942 {
943 return false;
944 }
945
946 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
947
948 // Extract the render target dimensions and view
949 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
950 if (!renderTarget)
951 {
952 ERR("render target pointer unexpectedly null.");
953 return false;
954 }
955
956 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
957 if (!framebufferRTVs[colorAttachment])
958 {
959 ERR("render target view pointer unexpectedly null.");
960 return false;
961 }
962
963 if (missingColorRenderTarget)
964 {
965 renderTargetWidth = colorbuffer->getWidth();
966 renderTargetHeight = colorbuffer->getHeight();
967 renderTargetFormat = colorbuffer->getActualFormat();
968 missingColorRenderTarget = false;
969 }
Jamie Madillba597af2013-10-22 13:12:15 -0400970
971#ifdef _DEBUG
972 // Workaround for Debug SETSHADERRESOURCES_HAZARD D3D11 warnings
973 for (unsigned int vertexSerialIndex = 0; vertexSerialIndex < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; vertexSerialIndex++)
974 {
975 if (colorbuffer->getTextureSerial() != 0 && mCurVertexTextureSerials[vertexSerialIndex] == colorbuffer->getTextureSerial())
976 {
977 setTexture(gl::SAMPLER_VERTEX, vertexSerialIndex, NULL);
978 }
979 }
980
981 for (unsigned int pixelSerialIndex = 0; pixelSerialIndex < gl::MAX_TEXTURE_IMAGE_UNITS; pixelSerialIndex++)
982 {
983 if (colorbuffer->getTextureSerial() != 0 && mCurPixelTextureSerials[pixelSerialIndex] == colorbuffer->getTextureSerial())
984 {
985 setTexture(gl::SAMPLER_PIXEL, pixelSerialIndex, NULL);
986 }
987 }
988#endif
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000989 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000990 }
991
992 // Get the depth stencil render buffer and serials
993 gl::Renderbuffer *depthStencil = NULL;
994 unsigned int depthbufferSerial = 0;
995 unsigned int stencilbufferSerial = 0;
996 if (framebuffer->getDepthbufferType() != GL_NONE)
997 {
998 depthStencil = framebuffer->getDepthbuffer();
999 if (!depthStencil)
1000 {
1001 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001002 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001003 return false;
1004 }
1005
1006 depthbufferSerial = depthStencil->getSerial();
1007 }
1008 else if (framebuffer->getStencilbufferType() != GL_NONE)
1009 {
1010 depthStencil = framebuffer->getStencilbuffer();
1011 if (!depthStencil)
1012 {
1013 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001014 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001015 return false;
1016 }
1017
1018 stencilbufferSerial = depthStencil->getSerial();
1019 }
1020
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001021 // Extract the depth stencil sizes and view
1022 unsigned int depthSize = 0;
1023 unsigned int stencilSize = 0;
1024 ID3D11DepthStencilView* framebufferDSV = NULL;
1025 if (depthStencil)
1026 {
1027 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
1028 if (!depthStencilRenderTarget)
1029 {
1030 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001031 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001032 return false;
1033 }
1034
1035 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
1036 if (!framebufferDSV)
1037 {
1038 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001039 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001040 return false;
1041 }
1042
1043 // If there is no render buffer, the width, height and format values come from
1044 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001045 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001046 {
1047 renderTargetWidth = depthStencil->getWidth();
1048 renderTargetHeight = depthStencil->getHeight();
1049 renderTargetFormat = depthStencil->getActualFormat();
1050 }
1051
1052 depthSize = depthStencil->getDepthSize();
1053 stencilSize = depthStencil->getStencilSize();
1054 }
1055
1056 // Apply the render target and depth stencil
1057 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001058 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001059 depthbufferSerial != mAppliedDepthbufferSerial ||
1060 stencilbufferSerial != mAppliedStencilbufferSerial)
1061 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001062 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001063
1064 mRenderTargetDesc.width = renderTargetWidth;
1065 mRenderTargetDesc.height = renderTargetHeight;
1066 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001067 mForceSetViewport = true;
1068 mForceSetScissor = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001069
1070 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1071 {
1072 mCurDepthSize = depthSize;
1073 mForceSetRasterState = true;
1074 }
1075
1076 mCurStencilSize = stencilSize;
1077
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001078 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1079 {
1080 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
1081 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001082 mAppliedDepthbufferSerial = depthbufferSerial;
1083 mAppliedStencilbufferSerial = stencilbufferSerial;
1084 mRenderTargetDescInitialized = true;
1085 mDepthStencilInitialized = true;
1086 }
1087
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001088 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001089}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001090
Jamie Madill57a89722013-07-02 11:57:03 -04001091GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001092 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001093{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001094 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001095 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001096 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001097 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001098 return err;
1099 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001100
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001101 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001102}
1103
daniel@transgaming.com31240482012-11-28 21:06:41 +00001104GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001105{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001106 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001107
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001108 if (err == GL_NO_ERROR)
1109 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001110 if (indexInfo->storage)
1111 {
1112 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1113 {
1114 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1115 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1116
Jamie Madill171ca0e2013-10-10 15:10:31 -04001117 mDeviceContext->IASetIndexBuffer(storage->getBuffer(false), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001118
1119 mAppliedIBSerial = 0;
1120 mAppliedStorageIBSerial = storage->getSerial();
1121 mAppliedIBOffset = indexInfo->startOffset;
1122 }
1123 }
1124 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001125 {
1126 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1127
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001128 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001129
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001130 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001131 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001132 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001133 }
1134 }
1135
1136 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001137}
1138
1139void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1140{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001141 if (mode == GL_LINE_LOOP)
1142 {
1143 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1144 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001145 else if (mode == GL_TRIANGLE_FAN)
1146 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001147 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001148 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001149 else if (instances > 0)
1150 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001151 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001152 }
1153 else
1154 {
1155 mDeviceContext->Draw(count, 0);
1156 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001157}
1158
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001159void 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 +00001160{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001161 if (mode == GL_LINE_LOOP)
1162 {
1163 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1164 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001165 else if (mode == GL_TRIANGLE_FAN)
1166 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001167 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1168 }
1169 else if (instances > 0)
1170 {
1171 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001172 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001173 else
1174 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001175 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001176 }
1177}
1178
1179void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1180{
1181 // Get the raw indices for an indexed draw
1182 if (type != GL_NONE && elementArrayBuffer)
1183 {
1184 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001185 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001186 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001187 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001188 }
1189
1190 if (!mLineLoopIB)
1191 {
1192 mLineLoopIB = new StreamingIndexBufferInterface(this);
1193 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1194 {
1195 delete mLineLoopIB;
1196 mLineLoopIB = NULL;
1197
1198 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001199 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001200 }
1201 }
1202
Geoff Lang57e713e2013-07-31 17:01:58 -04001203 // Checked by Renderer11::applyPrimitiveType
1204 ASSERT(count >= 0);
1205
1206 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001207 {
1208 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1209 return gl::error(GL_OUT_OF_MEMORY);
1210 }
1211
Geoff Lang57e713e2013-07-31 17:01:58 -04001212 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001213 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1214 {
1215 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001216 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001217 }
1218
1219 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001220 unsigned int offset;
1221 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001222 {
1223 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001224 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001225 }
1226
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001227 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001228 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001229
1230 switch (type)
1231 {
1232 case GL_NONE: // Non-indexed draw
1233 for (int i = 0; i < count; i++)
1234 {
1235 data[i] = i;
1236 }
1237 data[count] = 0;
1238 break;
1239 case GL_UNSIGNED_BYTE:
1240 for (int i = 0; i < count; i++)
1241 {
1242 data[i] = static_cast<const GLubyte*>(indices)[i];
1243 }
1244 data[count] = static_cast<const GLubyte*>(indices)[0];
1245 break;
1246 case GL_UNSIGNED_SHORT:
1247 for (int i = 0; i < count; i++)
1248 {
1249 data[i] = static_cast<const GLushort*>(indices)[i];
1250 }
1251 data[count] = static_cast<const GLushort*>(indices)[0];
1252 break;
1253 case GL_UNSIGNED_INT:
1254 for (int i = 0; i < count; i++)
1255 {
1256 data[i] = static_cast<const GLuint*>(indices)[i];
1257 }
1258 data[count] = static_cast<const GLuint*>(indices)[0];
1259 break;
1260 default: UNREACHABLE();
1261 }
1262
1263 if (!mLineLoopIB->unmapBuffer())
1264 {
1265 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001266 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001267 }
1268
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001269 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001270 {
1271 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1272
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001273 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001274 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001275 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001276 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001277 }
1278
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001279 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001280}
1281
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001282void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001283{
1284 // Get the raw indices for an indexed draw
1285 if (type != GL_NONE && elementArrayBuffer)
1286 {
1287 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001288 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001289 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001290 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001291 }
1292
1293 if (!mTriangleFanIB)
1294 {
1295 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1296 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1297 {
1298 delete mTriangleFanIB;
1299 mTriangleFanIB = NULL;
1300
1301 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001302 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001303 }
1304 }
1305
Geoff Lang57e713e2013-07-31 17:01:58 -04001306 // Checked by Renderer11::applyPrimitiveType
1307 ASSERT(count >= 3);
1308
Geoff Langeadfd572013-07-09 15:55:07 -04001309 const unsigned int numTris = count - 2;
1310
Geoff Lang57e713e2013-07-31 17:01:58 -04001311 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001312 {
1313 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1314 return gl::error(GL_OUT_OF_MEMORY);
1315 }
1316
1317 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001318 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1319 {
1320 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001321 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001322 }
1323
1324 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001325 unsigned int offset;
1326 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001327 {
1328 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001329 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001330 }
1331
1332 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001333 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001334
1335 switch (type)
1336 {
1337 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001338 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001339 {
1340 data[i*3 + 0] = 0;
1341 data[i*3 + 1] = i + 1;
1342 data[i*3 + 2] = i + 2;
1343 }
1344 break;
1345 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001346 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001347 {
1348 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1349 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1350 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1351 }
1352 break;
1353 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001354 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001355 {
1356 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1357 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1358 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1359 }
1360 break;
1361 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001362 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001363 {
1364 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1365 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1366 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1367 }
1368 break;
1369 default: UNREACHABLE();
1370 }
1371
1372 if (!mTriangleFanIB->unmapBuffer())
1373 {
1374 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001375 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001376 }
1377
1378 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1379 {
1380 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1381
1382 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1383 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001384 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001385 mAppliedIBOffset = indexBufferOffset;
1386 }
1387
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001388 if (instances > 0)
1389 {
1390 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1391 }
1392 else
1393 {
1394 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1395 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001396}
1397
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001398void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1399{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001400 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001401 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1402
1403 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001404 {
1405 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1406 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001407
daniel@transgaming.come4991412012-12-20 20:55:34 +00001408 ID3D11VertexShader *vertexShader = NULL;
1409 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001410
daniel@transgaming.come4991412012-12-20 20:55:34 +00001411 ID3D11PixelShader *pixelShader = NULL;
1412 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001413
daniel@transgaming.come4991412012-12-20 20:55:34 +00001414 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1415 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001416
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001417 programBinary->dirtyAllUniforms();
1418
1419 mAppliedProgramBinarySerial = programBinarySerial;
1420 }
1421
1422 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001423 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001424
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001425 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001426 {
1427 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001428 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001429 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1430 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001431 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1432 }
1433 else
1434 {
1435 mDeviceContext->GSSetShader(NULL, NULL, 0);
1436 }
1437
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001438 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001439 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001440}
1441
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001442void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001443{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001444 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1445 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001446
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001447 unsigned int totalRegisterCountVS = 0;
1448 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001449
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001450 bool vertexUniformsDirty = false;
1451 bool pixelUniformsDirty = false;
1452
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001453 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1454 {
1455 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001456
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001457 if (uniform->isReferencedByVertexShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001458 {
1459 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001460 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001461 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001462
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001463 if (uniform->isReferencedByFragmentShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001464 {
1465 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001466 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001467 }
1468 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001469
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001470 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1471 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1472
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001473 float (*mapVS)[4] = NULL;
1474 float (*mapPS)[4] = NULL;
1475
Shannon Woods5ab33c82013-06-26 15:31:09 -04001476 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1477 {
1478 D3D11_MAPPED_SUBRESOURCE map = {0};
1479 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1480 ASSERT(SUCCEEDED(result));
1481 mapVS = (float(*)[4])map.pData;
1482 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001483
Shannon Woods5ab33c82013-06-26 15:31:09 -04001484 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1485 {
1486 D3D11_MAPPED_SUBRESOURCE map = {0};
1487 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1488 ASSERT(SUCCEEDED(result));
1489 mapPS = (float(*)[4])map.pData;
1490 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001491
Jamie Madill5b085dc2013-08-30 13:21:11 -04001492 for (size_t uniformIndex = 0; uniformIndex < uniformArray->size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001493 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001494 gl::Uniform *uniform = (*uniformArray)[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001495
Nicolas Capense6050882013-07-08 10:43:10 -04001496 if (!gl::IsSampler(uniform->type))
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001497 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001498 unsigned int componentCount = (4 - uniform->registerElement);
1499
Jamie Madill71cc91f2013-09-18 12:51:22 -04001500 // 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 -04001501 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001502
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001503 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001504 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001505 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001506 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001507
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001508 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001509 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001510 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001511 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001512 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001513
1514 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001515 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001516
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001517 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001518 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001519 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001520 }
1521
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001522 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001523 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001524 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001525 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001526
1527 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1528 {
1529 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1530 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1531 }
1532
1533 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1534 {
1535 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1536 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1537 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001538
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001539 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001540 if (!mDriverConstantBufferVS)
1541 {
1542 D3D11_BUFFER_DESC constantBufferDescription = {0};
1543 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1544 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1545 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1546 constantBufferDescription.CPUAccessFlags = 0;
1547 constantBufferDescription.MiscFlags = 0;
1548 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001549
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001550 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001551 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001552
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001553 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1554 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001555
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001556 if (!mDriverConstantBufferPS)
1557 {
1558 D3D11_BUFFER_DESC constantBufferDescription = {0};
1559 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1560 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1561 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1562 constantBufferDescription.CPUAccessFlags = 0;
1563 constantBufferDescription.MiscFlags = 0;
1564 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001565
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001566 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001567 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001568
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001569 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1570 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001571
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001572 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1573 {
1574 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1575 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1576 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001577
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001578 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1579 {
1580 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1581 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1582 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001583
1584 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001585 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1586 {
1587 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1588 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1589 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001590}
1591
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001592void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001593{
Geoff Langda507fe2013-08-20 12:01:42 -04001594 mClear->clearFramebuffer(clearParams, frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001595}
1596
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001597void Renderer11::markAllStateDirty()
1598{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001599 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1600 {
1601 mAppliedRenderTargetSerials[rtIndex] = 0;
1602 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001603 mAppliedDepthbufferSerial = 0;
1604 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001605 mDepthStencilInitialized = false;
1606 mRenderTargetDescInitialized = false;
1607
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001608 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001609 {
1610 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001611 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001612 }
1613 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1614 {
1615 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001616 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001617 }
1618
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001619 mForceSetBlendState = true;
1620 mForceSetRasterState = true;
1621 mForceSetDepthStencilState = true;
1622 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001623 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001624
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001625 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001626 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001627 mAppliedIBOffset = 0;
1628
daniel@transgaming.come4991412012-12-20 20:55:34 +00001629 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001630 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1631 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001632
1633 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001634
1635 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1636 {
1637 mCurrentConstantBufferVS[i] = -1;
1638 mCurrentConstantBufferPS[i] = -1;
1639 }
1640
1641 mCurrentVertexConstantBuffer = NULL;
1642 mCurrentPixelConstantBuffer = NULL;
1643 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001644
1645 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001646}
1647
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001648void Renderer11::releaseDeviceResources()
1649{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001650 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001651 mInputLayoutCache.clear();
1652
Geoff Langea228632013-07-30 15:17:12 -04001653 SafeDelete(mVertexDataManager);
1654 SafeDelete(mIndexDataManager);
1655 SafeDelete(mLineLoopIB);
1656 SafeDelete(mTriangleFanIB);
1657 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001658 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001659 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001660
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001661 SafeRelease(mDriverConstantBufferVS);
1662 SafeRelease(mDriverConstantBufferPS);
1663 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001664}
1665
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001666void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001667{
1668 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001669 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001670}
1671
1672bool Renderer11::isDeviceLost()
1673{
1674 return mDeviceLost;
1675}
1676
1677// set notify to true to broadcast a message to all contexts of the device loss
1678bool Renderer11::testDeviceLost(bool notify)
1679{
1680 bool isLost = false;
1681
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001682 // GetRemovedReason is used to test if the device is removed
1683 HRESULT result = mDevice->GetDeviceRemovedReason();
1684 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001685
1686 if (isLost)
1687 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001688 // Log error if this is a new device lost event
1689 if (mDeviceLost == false)
1690 {
1691 ERR("The D3D11 device was removed: 0x%08X", result);
1692 }
1693
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001694 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001695 // we'll probably get this done again by notifyDeviceLost
1696 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001697 // Note that we don't want to clear the device loss status here
1698 // -- this needs to be done by resetDevice
1699 mDeviceLost = true;
1700 if (notify)
1701 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001702 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001703 }
1704 }
1705
1706 return isLost;
1707}
1708
1709bool Renderer11::testDeviceResettable()
1710{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001711 // determine if the device is resettable by creating a dummy device
1712 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001713
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001714 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001715 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001716 return false;
1717 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001718
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001719 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001720 {
1721 D3D_FEATURE_LEVEL_11_0,
1722 D3D_FEATURE_LEVEL_10_1,
1723 D3D_FEATURE_LEVEL_10_0,
1724 };
1725
1726 ID3D11Device* dummyDevice;
1727 D3D_FEATURE_LEVEL dummyFeatureLevel;
1728 ID3D11DeviceContext* dummyContext;
1729
1730 HRESULT result = D3D11CreateDevice(NULL,
1731 D3D_DRIVER_TYPE_HARDWARE,
1732 NULL,
1733 #if defined(_DEBUG)
1734 D3D11_CREATE_DEVICE_DEBUG,
1735 #else
1736 0,
1737 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001738 featureLevels,
1739 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001740 D3D11_SDK_VERSION,
1741 &dummyDevice,
1742 &dummyFeatureLevel,
1743 &dummyContext);
1744
1745 if (!mDevice || FAILED(result))
1746 {
1747 return false;
1748 }
1749
Geoff Langea228632013-07-30 15:17:12 -04001750 SafeRelease(dummyContext);
1751 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001752
1753 return true;
1754}
1755
1756void Renderer11::release()
1757{
1758 releaseDeviceResources();
1759
Geoff Langea228632013-07-30 15:17:12 -04001760 SafeRelease(mDxgiFactory);
1761 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001762
1763 if (mDeviceContext)
1764 {
1765 mDeviceContext->ClearState();
1766 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001767 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001768 }
1769
Geoff Langea228632013-07-30 15:17:12 -04001770 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001771
1772 if (mD3d11Module)
1773 {
1774 FreeLibrary(mD3d11Module);
1775 mD3d11Module = NULL;
1776 }
1777
1778 if (mDxgiModule)
1779 {
1780 FreeLibrary(mDxgiModule);
1781 mDxgiModule = NULL;
1782 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001783}
1784
1785bool Renderer11::resetDevice()
1786{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001787 // recreate everything
1788 release();
1789 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001790
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001791 if (result != EGL_SUCCESS)
1792 {
1793 ERR("Could not reinitialize D3D11 device: %08X", result);
1794 return false;
1795 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001796
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001797 mDeviceLost = false;
1798
1799 return true;
1800}
1801
1802DWORD Renderer11::getAdapterVendor() const
1803{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001804 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001805}
1806
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001807std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001808{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001809 std::ostringstream rendererString;
1810
1811 rendererString << mDescription;
1812 rendererString << " Direct3D11";
1813
1814 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1815 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1816
1817 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001818}
1819
1820GUID Renderer11::getAdapterIdentifier() const
1821{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001822 // Use the adapter LUID as our adapter ID
1823 // This number is local to a machine is only guaranteed to be unique between restarts
1824 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1825 GUID adapterId = {0};
1826 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1827 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001828}
1829
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001830bool Renderer11::getBGRATextureSupport() const
1831{
1832 return mBGRATextureSupport;
1833}
1834
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001835bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001836{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001837 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001838}
1839
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001840bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001841{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001842 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001843}
1844
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001845bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001846{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001847 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001848}
1849
1850bool Renderer11::getDepthTextureSupport() const
1851{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001852 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001853}
1854
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001855bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001856{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001857 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001858}
1859
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001860bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001861{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001862 return mFloat32FilterSupport;
1863}
1864
1865bool Renderer11::getFloat32TextureRenderingSupport() const
1866{
1867 return mFloat32RenderSupport;
1868}
1869
1870bool Renderer11::getFloat16TextureSupport() const
1871{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001872 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001873}
1874
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001875bool Renderer11::getFloat16TextureFilteringSupport() const
1876{
1877 return mFloat16FilterSupport;
1878}
1879
1880bool Renderer11::getFloat16TextureRenderingSupport() const
1881{
1882 return mFloat16RenderSupport;
1883}
1884
Geoff Langd42cf4e2013-06-05 16:09:17 -04001885bool Renderer11::getRGB565TextureSupport() const
1886{
1887 return false;
1888}
1889
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001890bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001891{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001892 return false;
1893}
1894
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001895bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001896{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001897 return false;
1898}
1899
Geoff Lang632192d2013-10-04 13:40:46 -04001900bool Renderer11::getRGTextureSupport() const
1901{
1902 return mRGTextureSupport;
1903}
1904
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001905bool Renderer11::getTextureFilterAnisotropySupport() const
1906{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001907 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001908}
1909
1910float Renderer11::getTextureMaxAnisotropy() const
1911{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001912 switch (mFeatureLevel)
1913 {
1914 case D3D_FEATURE_LEVEL_11_0:
1915 return D3D11_MAX_MAXANISOTROPY;
1916 case D3D_FEATURE_LEVEL_10_1:
1917 case D3D_FEATURE_LEVEL_10_0:
1918 return D3D10_MAX_MAXANISOTROPY;
1919 default: UNREACHABLE();
1920 return 0;
1921 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001922}
1923
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001924bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001925{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00001926 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001927}
1928
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001929Range Renderer11::getViewportBounds() const
1930{
1931 switch (mFeatureLevel)
1932 {
1933 case D3D_FEATURE_LEVEL_11_0:
1934 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1935 case D3D_FEATURE_LEVEL_10_1:
1936 case D3D_FEATURE_LEVEL_10_0:
1937 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1938 default: UNREACHABLE();
1939 return Range(0, 0);
1940 }
1941}
1942
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001943unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001944{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001945 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1946 switch (mFeatureLevel)
1947 {
1948 case D3D_FEATURE_LEVEL_11_0:
1949 case D3D_FEATURE_LEVEL_10_1:
1950 case D3D_FEATURE_LEVEL_10_0:
1951 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1952 default: UNREACHABLE();
1953 return 0;
1954 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001955}
1956
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001957unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1958{
1959 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1960}
1961
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001962unsigned int Renderer11::getReservedVertexUniformVectors() const
1963{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001964 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001965}
1966
1967unsigned int Renderer11::getReservedFragmentUniformVectors() const
1968{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001969 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001970}
1971
1972unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001973{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001974 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1975 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1976 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001977}
1978
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001979unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001980{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001981 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1982 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1983 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001984}
1985
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001986unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001987{
1988 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001989 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1990 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001991 switch (mFeatureLevel)
1992 {
1993 case D3D_FEATURE_LEVEL_11_0:
1994 return D3D11_VS_OUTPUT_REGISTER_COUNT;
1995 case D3D_FEATURE_LEVEL_10_1:
1996 case D3D_FEATURE_LEVEL_10_0:
1997 return D3D10_VS_OUTPUT_REGISTER_COUNT;
1998 default: UNREACHABLE();
1999 return 0;
2000 }
2001}
2002
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002003unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2004{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002005 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2006 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2007
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002008 switch (mFeatureLevel)
2009 {
2010 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002011 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002012 case D3D_FEATURE_LEVEL_10_1:
2013 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002014 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002015 default: UNREACHABLE();
2016 return 0;
2017 }
2018}
2019
2020unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2021{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002022 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2023 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2024
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002025 switch (mFeatureLevel)
2026 {
2027 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002028 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002029 case D3D_FEATURE_LEVEL_10_1:
2030 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002031 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002032 default: UNREACHABLE();
2033 return 0;
2034 }
2035}
2036
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002037unsigned int Renderer11::getReservedVertexUniformBuffers() const
2038{
2039 // we reserve one buffer for the application uniforms, and one for driver uniforms
2040 return 2;
2041}
2042
2043unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2044{
2045 // we reserve one buffer for the application uniforms, and one for driver uniforms
2046 return 2;
2047}
2048
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002049unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2050{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002051 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2052 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2053
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002054 switch (mFeatureLevel)
2055 {
2056 case D3D_FEATURE_LEVEL_11_0:
2057 return D3D11_SO_BUFFER_SLOT_COUNT;
2058 case D3D_FEATURE_LEVEL_10_1:
2059 case D3D_FEATURE_LEVEL_10_0:
2060 return D3D10_SO_BUFFER_SLOT_COUNT;
2061 default: UNREACHABLE();
2062 return 0;
2063 }
2064}
2065
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002066unsigned int Renderer11::getMaxUniformBufferSize() const
2067{
2068 // Each component is a 4-element vector of 4-byte units (floats)
2069 const unsigned int bytesPerComponent = 4 * sizeof(float);
2070
2071 switch (mFeatureLevel)
2072 {
2073 case D3D_FEATURE_LEVEL_11_0:
2074 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2075 case D3D_FEATURE_LEVEL_10_1:
2076 case D3D_FEATURE_LEVEL_10_0:
2077 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2078 default: UNREACHABLE();
2079 return 0;
2080 }
2081}
2082
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002083bool Renderer11::getNonPower2TextureSupport() const
2084{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002085 switch (mFeatureLevel)
2086 {
2087 case D3D_FEATURE_LEVEL_11_0:
2088 case D3D_FEATURE_LEVEL_10_1:
2089 case D3D_FEATURE_LEVEL_10_0:
2090 return true;
2091 default: UNREACHABLE();
2092 return false;
2093 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002094}
2095
2096bool Renderer11::getOcclusionQuerySupport() const
2097{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002098 switch (mFeatureLevel)
2099 {
2100 case D3D_FEATURE_LEVEL_11_0:
2101 case D3D_FEATURE_LEVEL_10_1:
2102 case D3D_FEATURE_LEVEL_10_0:
2103 return true;
2104 default: UNREACHABLE();
2105 return false;
2106 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002107}
2108
2109bool Renderer11::getInstancingSupport() const
2110{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002111 switch (mFeatureLevel)
2112 {
2113 case D3D_FEATURE_LEVEL_11_0:
2114 case D3D_FEATURE_LEVEL_10_1:
2115 case D3D_FEATURE_LEVEL_10_0:
2116 return true;
2117 default: UNREACHABLE();
2118 return false;
2119 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002120}
2121
2122bool Renderer11::getShareHandleSupport() const
2123{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002124 // We only currently support share handles with BGRA surfaces, because
2125 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002126 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002127 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002128}
2129
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002130bool Renderer11::getDerivativeInstructionSupport() const
2131{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002132 switch (mFeatureLevel)
2133 {
2134 case D3D_FEATURE_LEVEL_11_0:
2135 case D3D_FEATURE_LEVEL_10_1:
2136 case D3D_FEATURE_LEVEL_10_0:
2137 return true;
2138 default: UNREACHABLE();
2139 return false;
2140 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002141}
2142
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002143bool Renderer11::getPostSubBufferSupport() const
2144{
2145 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2146 return false;
2147}
2148
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002149int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002150{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002151 switch (mFeatureLevel)
2152 {
2153 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002154 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002155 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2156 default: UNREACHABLE(); return 0;
2157 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002158}
2159
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002160int Renderer11::getMinorShaderModel() const
2161{
2162 switch (mFeatureLevel)
2163 {
2164 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2165 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2166 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2167 default: UNREACHABLE(); return 0;
2168 }
2169}
2170
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002171float Renderer11::getMaxPointSize() const
2172{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002173 // choose a reasonable maximum. we enforce this in the shader.
2174 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2175 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002176}
2177
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002178int Renderer11::getMaxViewportDimension() const
2179{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002180 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2181 // In our case return the maximum texture size, which is the maximum render buffer size.
2182 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2183 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2184
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002185 switch (mFeatureLevel)
2186 {
2187 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002188 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002189 case D3D_FEATURE_LEVEL_10_1:
2190 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002191 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002192 default: UNREACHABLE();
2193 return 0;
2194 }
2195}
2196
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002197int Renderer11::getMaxTextureWidth() const
2198{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002199 switch (mFeatureLevel)
2200 {
2201 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2202 case D3D_FEATURE_LEVEL_10_1:
2203 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2204 default: UNREACHABLE(); return 0;
2205 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002206}
2207
2208int Renderer11::getMaxTextureHeight() const
2209{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002210 switch (mFeatureLevel)
2211 {
2212 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2213 case D3D_FEATURE_LEVEL_10_1:
2214 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2215 default: UNREACHABLE(); return 0;
2216 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002217}
2218
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002219int Renderer11::getMaxTextureDepth() const
2220{
2221 switch (mFeatureLevel)
2222 {
2223 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2224 case D3D_FEATURE_LEVEL_10_1:
2225 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2226 default: UNREACHABLE(); return 0;
2227 }
2228}
2229
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002230int Renderer11::getMaxTextureArrayLayers() const
2231{
2232 switch (mFeatureLevel)
2233 {
2234 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2235 case D3D_FEATURE_LEVEL_10_1:
2236 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2237 default: UNREACHABLE(); return 0;
2238 }
2239}
2240
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002241bool Renderer11::get32BitIndexSupport() const
2242{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002243 switch (mFeatureLevel)
2244 {
2245 case D3D_FEATURE_LEVEL_11_0:
2246 case D3D_FEATURE_LEVEL_10_1:
2247 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2248 default: UNREACHABLE(); return false;
2249 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002250}
2251
2252int Renderer11::getMinSwapInterval() const
2253{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002254 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002255}
2256
2257int Renderer11::getMaxSwapInterval() const
2258{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002259 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002260}
2261
2262int Renderer11::getMaxSupportedSamples() const
2263{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002264 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002265}
2266
Geoff Lang005df412013-10-16 14:12:50 -04002267GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002268{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002269 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002270 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2271 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2272}
2273
Geoff Lang005df412013-10-16 14:12:50 -04002274GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002275{
2276 unsigned int numCounts = 0;
2277
2278 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002279 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2280 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002281 {
2282 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2283 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2284
2285 if (iter != mMultisampleSupportMap.end())
2286 {
2287 const MultisampleSupportInfo& info = iter->second;
2288 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2289 {
2290 if (info.qualityLevels[i] > 0)
2291 {
2292 numCounts++;
2293 }
2294 }
2295 }
2296 }
2297
2298 return numCounts;
2299}
2300
Geoff Lang005df412013-10-16 14:12:50 -04002301void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002302{
2303 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002304 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2305 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2306 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002307 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002308 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002309
2310 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2311 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2312
2313 if (iter != mMultisampleSupportMap.end())
2314 {
2315 const MultisampleSupportInfo& info = iter->second;
2316 int bufPos = 0;
2317 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2318 {
2319 if (info.qualityLevels[i] > 0)
2320 {
2321 params[bufPos++] = i + 1;
2322 }
2323 }
2324 }
2325}
2326
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002327int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2328{
2329 if (requested == 0)
2330 {
2331 return 0;
2332 }
2333
2334 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2335 if (iter != mMultisampleSupportMap.end())
2336 {
2337 const MultisampleSupportInfo& info = iter->second;
2338 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2339 {
2340 if (info.qualityLevels[i] > 0)
2341 {
2342 return i + 1;
2343 }
2344 }
2345 }
2346
2347 return -1;
2348}
2349
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002350unsigned int Renderer11::getMaxRenderTargets() const
2351{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002352 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2353 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2354
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002355 switch (mFeatureLevel)
2356 {
2357 case D3D_FEATURE_LEVEL_11_0:
2358 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2359 case D3D_FEATURE_LEVEL_10_1:
2360 case D3D_FEATURE_LEVEL_10_0:
2361 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2362 default:
2363 UNREACHABLE();
2364 return 1;
2365 }
2366}
2367
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002368bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002369{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002370 if (source && dest)
2371 {
2372 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2373 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2374
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002375 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002376 return true;
2377 }
2378
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002379 return false;
2380}
2381
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002382bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002383{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002384 if (source && dest)
2385 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002386 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2387 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002388
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002389 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002390 return true;
2391 }
2392
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002393 return false;
2394}
2395
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002396bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2397{
2398 if (source && dest)
2399 {
2400 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2401 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2402
2403 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2404 return true;
2405 }
2406
2407 return false;
2408}
2409
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002410bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2411{
2412 if (source && dest)
2413 {
2414 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2415 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2416
2417 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2418 return true;
2419 }
2420
2421 return false;
2422}
2423
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002424bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002425 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002426{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002427 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002428 if (!colorbuffer)
2429 {
2430 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002431 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002432 }
2433
2434 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2435 if (!sourceRenderTarget)
2436 {
2437 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002438 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002439 }
2440
2441 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2442 if (!source)
2443 {
2444 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002445 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002446 }
2447
2448 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2449 if (!storage11)
2450 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002451 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002452 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002453 }
2454
2455 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2456 if (!destRenderTarget)
2457 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002458 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002459 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002460 }
2461
2462 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2463 if (!dest)
2464 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002465 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002466 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002467 }
2468
Geoff Langb86b9792013-06-04 16:32:05 -04002469 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2470 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002471
Geoff Langb86b9792013-06-04 16:32:05 -04002472 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2473 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002474
Geoff Langb86b9792013-06-04 16:32:05 -04002475 // Use nearest filtering because source and destination are the same size for the direct
2476 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002477 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002478 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002479
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002480 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002481}
2482
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002483bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002484 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002485{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002486 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002487 if (!colorbuffer)
2488 {
2489 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002490 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002491 }
2492
2493 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2494 if (!sourceRenderTarget)
2495 {
2496 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002497 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002498 }
2499
2500 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2501 if (!source)
2502 {
2503 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002504 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002505 }
2506
2507 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2508 if (!storage11)
2509 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002510 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002511 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002512 }
2513
Nicolas Capensb13f8662013-06-04 13:30:19 -04002514 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002515 if (!destRenderTarget)
2516 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002517 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002518 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002519 }
2520
2521 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2522 if (!dest)
2523 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002524 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002525 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002526 }
2527
Geoff Langb86b9792013-06-04 16:32:05 -04002528 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2529 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002530
Geoff Langb86b9792013-06-04 16:32:05 -04002531 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2532 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002533
Geoff Langb86b9792013-06-04 16:32:05 -04002534 // Use nearest filtering because source and destination are the same size for the direct
2535 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002536 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002537 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002538
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002539 return ret;
2540}
2541
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002542bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2543 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2544{
2545 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2546 if (!colorbuffer)
2547 {
2548 ERR("Failed to retrieve the color buffer from the frame buffer.");
2549 return gl::error(GL_OUT_OF_MEMORY, false);
2550 }
2551
2552 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2553 if (!sourceRenderTarget)
2554 {
2555 ERR("Failed to retrieve the render target from the frame buffer.");
2556 return gl::error(GL_OUT_OF_MEMORY, false);
2557 }
2558
2559 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2560 if (!source)
2561 {
2562 ERR("Failed to retrieve the render target view from the render target.");
2563 return gl::error(GL_OUT_OF_MEMORY, false);
2564 }
2565
2566 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2567 if (!storage11)
2568 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002569 ERR("Failed to retrieve the texture storage from the destination.");
2570 return gl::error(GL_OUT_OF_MEMORY, false);
2571 }
2572
2573 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2574 if (!destRenderTarget)
2575 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002576 ERR("Failed to retrieve the render target from the destination storage.");
2577 return gl::error(GL_OUT_OF_MEMORY, false);
2578 }
2579
2580 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2581 if (!dest)
2582 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002583 ERR("Failed to retrieve the render target view from the destination render target.");
2584 return gl::error(GL_OUT_OF_MEMORY, false);
2585 }
2586
Geoff Langb86b9792013-06-04 16:32:05 -04002587 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2588 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002589
Geoff Langb86b9792013-06-04 16:32:05 -04002590 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2591 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002592
Geoff Langb86b9792013-06-04 16:32:05 -04002593 // Use nearest filtering because source and destination are the same size for the direct
2594 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002595 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002596 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002597
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002598 return ret;
2599}
2600
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002601bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2602 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2603{
2604 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2605 if (!colorbuffer)
2606 {
2607 ERR("Failed to retrieve the color buffer from the frame buffer.");
2608 return gl::error(GL_OUT_OF_MEMORY, false);
2609 }
2610
2611 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2612 if (!sourceRenderTarget)
2613 {
2614 ERR("Failed to retrieve the render target from the frame buffer.");
2615 return gl::error(GL_OUT_OF_MEMORY, false);
2616 }
2617
2618 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2619 if (!source)
2620 {
2621 ERR("Failed to retrieve the render target view from the render target.");
2622 return gl::error(GL_OUT_OF_MEMORY, false);
2623 }
2624
2625 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2626 if (!storage11)
2627 {
Geoff Langea228632013-07-30 15:17:12 -04002628 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002629 ERR("Failed to retrieve the texture storage from the destination.");
2630 return gl::error(GL_OUT_OF_MEMORY, false);
2631 }
2632
2633 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2634 if (!destRenderTarget)
2635 {
Geoff Langea228632013-07-30 15:17:12 -04002636 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002637 ERR("Failed to retrieve the render target from the destination storage.");
2638 return gl::error(GL_OUT_OF_MEMORY, false);
2639 }
2640
2641 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2642 if (!dest)
2643 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002644 ERR("Failed to retrieve the render target view from the destination render target.");
2645 return gl::error(GL_OUT_OF_MEMORY, false);
2646 }
2647
Geoff Langb86b9792013-06-04 16:32:05 -04002648 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2649 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002650
Geoff Langb86b9792013-06-04 16:32:05 -04002651 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2652 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002653
Geoff Langb86b9792013-06-04 16:32:05 -04002654 // Use nearest filtering because source and destination are the same size for the direct
2655 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002656 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002657 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002658
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002659 return ret;
2660}
2661
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002662void Renderer11::unapplyRenderTargets()
2663{
2664 setOneTimeRenderTarget(NULL);
2665}
2666
2667void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2668{
2669 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2670
2671 rtvArray[0] = renderTargetView;
2672
2673 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2674
2675 // Do not preserve the serial for this one-time-use render target
2676 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2677 {
2678 mAppliedRenderTargetSerials[rtIndex] = 0;
2679 }
2680}
2681
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002682RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2683{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002684 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002685 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002686
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002687 if (depth)
2688 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002689 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002690 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002691 swapChain11->getDepthStencilTexture(),
2692 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002693 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002694 }
2695 else
2696 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002697 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002698 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002699 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002700 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002701 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002702 }
2703 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002704}
2705
Geoff Langa2d97f12013-06-11 11:44:02 -04002706RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002707{
Geoff Langa2d97f12013-06-11 11:44:02 -04002708 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002709 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002710}
2711
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002712ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002713{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002714 ShaderExecutable11 *executable = NULL;
2715
2716 switch (type)
2717 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002718 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002719 {
2720 ID3D11VertexShader *vshader = NULL;
2721 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2722 ASSERT(SUCCEEDED(result));
2723
2724 if (vshader)
2725 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002726 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002727 }
2728 }
2729 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002730 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002731 {
2732 ID3D11PixelShader *pshader = NULL;
2733 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2734 ASSERT(SUCCEEDED(result));
2735
2736 if (pshader)
2737 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002738 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002739 }
2740 }
2741 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002742 case rx::SHADER_GEOMETRY:
2743 {
2744 ID3D11GeometryShader *gshader = NULL;
2745 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2746 ASSERT(SUCCEEDED(result));
2747
2748 if (gshader)
2749 {
2750 executable = new ShaderExecutable11(function, length, gshader);
2751 }
2752 }
2753 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002754 default:
2755 UNREACHABLE();
2756 break;
2757 }
2758
2759 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002760}
2761
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002762ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002763{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002764 const char *profile = NULL;
2765
2766 switch (type)
2767 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002768 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002769 profile = "vs_4_0";
2770 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002771 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002772 profile = "ps_4_0";
2773 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002774 case rx::SHADER_GEOMETRY:
2775 profile = "gs_4_0";
2776 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002777 default:
2778 UNREACHABLE();
2779 return NULL;
2780 }
2781
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00002782 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002783 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002784 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002785 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002786 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002787
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002788 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002789 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002790
2791 return executable;
2792}
2793
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002794VertexBuffer *Renderer11::createVertexBuffer()
2795{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002796 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002797}
2798
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002799IndexBuffer *Renderer11::createIndexBuffer()
2800{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002801 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002802}
2803
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002804BufferStorage *Renderer11::createBufferStorage()
2805{
2806 return new BufferStorage11(this);
2807}
2808
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002809QueryImpl *Renderer11::createQuery(GLenum type)
2810{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002811 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002812}
2813
2814FenceImpl *Renderer11::createFence()
2815{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002816 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002817}
2818
Geoff Lang005df412013-10-16 14:12:50 -04002819bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002820{
Jamie Madill4461f092013-10-10 15:10:39 -04002821 int clientVersion = getCurrentClientVersion();
2822
2823 // We only support buffer to texture copies in ES3
2824 if (clientVersion <= 2)
2825 {
2826 return false;
2827 }
2828
2829 // sRGB formats do not work with D3D11 buffer SRVs
2830 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2831 {
2832 return false;
2833 }
2834
2835 // We cannot support direct copies to non-color-renderable formats
2836 if (!gl::IsColorRenderingSupported(internalFormat, this))
2837 {
2838 return false;
2839 }
2840
2841 // We skip all 3-channel formats since sometimes format support is missing
2842 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2843 {
2844 return false;
2845 }
2846
2847 // We don't support formats which we can't represent without conversion
2848 if (getNativeTextureFormat(internalFormat) != internalFormat)
2849 {
2850 return false;
2851 }
2852
2853 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002854}
2855
Jamie Madilla21eea32013-09-18 14:36:25 -04002856bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2857 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2858{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002859 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002860 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2861}
2862
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002863bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002864{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002865 ASSERT(colorbuffer != NULL);
2866
2867 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2868 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002869 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002870 *subresourceIndex = renderTarget->getSubresourceIndex();
2871
2872 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2873 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002874 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002875 ID3D11Resource *textureResource = NULL;
2876 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002877
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002878 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002879 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002880 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002881 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002882
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002883 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002884 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002885 return true;
2886 }
2887 else
2888 {
2889 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2890 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002891 }
2892 }
2893 }
2894 }
2895
2896 return false;
2897}
2898
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002899bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002900 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002901{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002902 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002903 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002904 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002905
2906 if (!readBuffer)
2907 {
2908 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2909 return gl::error(GL_OUT_OF_MEMORY, false);
2910 }
2911
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002912 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002913
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002914 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002915 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002916 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2917 {
2918 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
2919
2920 if (!drawBuffer)
2921 {
2922 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2923 return gl::error(GL_OUT_OF_MEMORY, false);
2924 }
2925
2926 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2927
Geoff Lang125deab2013-08-09 13:34:16 -04002928 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002929 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002930 {
2931 return false;
2932 }
2933 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002934 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002935 }
2936
Geoff Lang685806d2013-06-12 11:16:36 -04002937 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002938 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002939 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
2940 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
2941
2942 if (!readBuffer)
2943 {
2944 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2945 return gl::error(GL_OUT_OF_MEMORY, false);
2946 }
2947
2948 if (!drawBuffer)
2949 {
2950 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2951 return gl::error(GL_OUT_OF_MEMORY, false);
2952 }
2953
2954 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2955 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2956
Geoff Lang125deab2013-08-09 13:34:16 -04002957 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002958 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002959 {
2960 return false;
2961 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002962 }
2963
2964 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002965}
2966
2967void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2968 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2969{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002970 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002971 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002972
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002973 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2974
2975 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002976 {
2977 gl::Rectangle area;
2978 area.x = x;
2979 area.y = y;
2980 area.width = width;
2981 area.height = height;
2982
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002983 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
2984 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002985
Geoff Langea228632013-07-30 15:17:12 -04002986 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002987 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002988}
2989
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002990Image *Renderer11::createImage()
2991{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002992 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002993}
2994
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002995void Renderer11::generateMipmap(Image *dest, Image *src)
2996{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002997 Image11 *dest11 = Image11::makeImage11(dest);
2998 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04002999 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003000}
3001
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003002TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3003{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003004 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3005 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003006}
3007
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003008TextureStorage *Renderer11::createTextureStorage2D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003009{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003010 return new TextureStorage11_2D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003011}
3012
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003013TextureStorage *Renderer11::createTextureStorageCube(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, int size)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003014{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003015 return new TextureStorage11_Cube(this, baseLevel, maxLevel, internalformat, renderTarget, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003016}
3017
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003018TextureStorage *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 +00003019{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003020 return new TextureStorage11_3D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003021}
3022
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003023TextureStorage *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 +00003024{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003025 return new TextureStorage11_2DArray(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003026}
3027
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003028void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3029 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3030 GLint packAlignment, void *pixels)
3031{
3032 D3D11_TEXTURE2D_DESC textureDesc;
3033 texture->GetDesc(&textureDesc);
3034
3035 D3D11_TEXTURE2D_DESC stagingDesc;
3036 stagingDesc.Width = area.width;
3037 stagingDesc.Height = area.height;
3038 stagingDesc.MipLevels = 1;
3039 stagingDesc.ArraySize = 1;
3040 stagingDesc.Format = textureDesc.Format;
3041 stagingDesc.SampleDesc.Count = 1;
3042 stagingDesc.SampleDesc.Quality = 0;
3043 stagingDesc.Usage = D3D11_USAGE_STAGING;
3044 stagingDesc.BindFlags = 0;
3045 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3046 stagingDesc.MiscFlags = 0;
3047
3048 ID3D11Texture2D* stagingTex = NULL;
3049 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3050 if (FAILED(result))
3051 {
3052 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3053 return;
3054 }
3055
3056 ID3D11Texture2D* srcTex = NULL;
3057 if (textureDesc.SampleDesc.Count > 1)
3058 {
3059 D3D11_TEXTURE2D_DESC resolveDesc;
3060 resolveDesc.Width = textureDesc.Width;
3061 resolveDesc.Height = textureDesc.Height;
3062 resolveDesc.MipLevels = 1;
3063 resolveDesc.ArraySize = 1;
3064 resolveDesc.Format = textureDesc.Format;
3065 resolveDesc.SampleDesc.Count = 1;
3066 resolveDesc.SampleDesc.Quality = 0;
3067 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3068 resolveDesc.BindFlags = 0;
3069 resolveDesc.CPUAccessFlags = 0;
3070 resolveDesc.MiscFlags = 0;
3071
3072 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3073 if (FAILED(result))
3074 {
3075 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003076 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003077 return;
3078 }
3079
3080 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3081 subResource = 0;
3082 }
3083 else
3084 {
3085 srcTex = texture;
3086 srcTex->AddRef();
3087 }
3088
3089 D3D11_BOX srcBox;
3090 srcBox.left = area.x;
3091 srcBox.right = area.x + area.width;
3092 srcBox.top = area.y;
3093 srcBox.bottom = area.y + area.height;
3094 srcBox.front = 0;
3095 srcBox.back = 1;
3096
3097 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3098
Geoff Langea228632013-07-30 15:17:12 -04003099 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003100
3101 D3D11_MAPPED_SUBRESOURCE mapping;
3102 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3103
3104 unsigned char *source;
3105 int inputPitch;
3106 if (packReverseRowOrder)
3107 {
3108 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3109 inputPitch = -static_cast<int>(mapping.RowPitch);
3110 }
3111 else
3112 {
3113 source = static_cast<unsigned char*>(mapping.pData);
3114 inputPitch = static_cast<int>(mapping.RowPitch);
3115 }
3116
Geoff Lang697ad3e2013-06-04 10:11:28 -04003117 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003118
Geoff Lang005df412013-10-16 14:12:50 -04003119 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003120 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3121 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3122
3123 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3124
3125 if (sourceFormat == format && sourceType == type)
3126 {
3127 // Direct copy possible
3128 unsigned char *dest = static_cast<unsigned char*>(pixels);
3129 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003130 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003131 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003132 }
3133 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003134 else
3135 {
Geoff Lang005df412013-10-16 14:12:50 -04003136 GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003137 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3138
3139 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type, getCurrentClientVersion());
3140 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003141 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003142 // Fast copy is possible through some special function
3143 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003144 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003145 for (int x = 0; x < area.width; x++)
3146 {
3147 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3148 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3149
3150 fastCopyFunc(src, dest);
3151 }
3152 }
3153 }
3154 else
3155 {
Jamie Madilld6cb2442013-07-10 15:13:38 -04003156 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003157 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3158
3159 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3160 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3161 sizeof(temp) >= sizeof(gl::ColorUI) &&
3162 sizeof(temp) >= sizeof(gl::ColorI));
3163
3164 for (int y = 0; y < area.height; y++)
3165 {
3166 for (int x = 0; x < area.width; x++)
3167 {
3168 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3169 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3170
3171 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3172 // will not allow the copy otherwise.
3173 readFunc(src, temp);
3174 writeFunc(temp, dest);
3175 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003176 }
3177 }
3178 }
3179
3180 mDeviceContext->Unmap(stagingTex, 0);
3181
Geoff Langea228632013-07-30 15:17:12 -04003182 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003183}
3184
Geoff Lang758d5b22013-06-11 11:42:50 -04003185bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003186 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3187 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003188{
Geoff Lang975af372013-06-12 11:19:22 -04003189 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3190 // it should never be the case that both color and depth/stencil need to be blitted at
3191 // at the same time.
3192 ASSERT(colorBlit != (depthBlit || stencilBlit));
3193
Geoff Langc1f51be2013-06-11 11:49:14 -04003194 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003195
Geoff Lang4d782732013-07-22 10:44:18 -04003196 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3197 if (!drawRenderTarget)
3198 {
3199 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3200 return gl::error(GL_OUT_OF_MEMORY, false);
3201 }
3202
3203 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3204 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3205 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3206 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3207
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003208 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3209 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003210 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003211 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003212 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003213 }
3214
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003215 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003216 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003217 unsigned int readSubresource = 0;
3218 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003219 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003220 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3221 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003222
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003223 if (unresolvedTexture)
3224 {
3225 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3226 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003227
Geoff Langea228632013-07-30 15:17:12 -04003228 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003229
3230 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3231 if (FAILED(result))
3232 {
Geoff Langea228632013-07-30 15:17:12 -04003233 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003234 return gl::error(GL_OUT_OF_MEMORY, false);
3235 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003236 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003237 }
3238 else
3239 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003240 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003241 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003242 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003243 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003244 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003245 }
3246
Geoff Lang4d782732013-07-22 10:44:18 -04003247 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003248 {
Geoff Lang4d782732013-07-22 10:44:18 -04003249 SafeRelease(readTexture);
3250 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003251 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003252 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003253 }
3254
Geoff Lang125deab2013-08-09 13:34:16 -04003255 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3256 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3257
3258 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3259
3260 bool wholeBufferCopy = !scissorNeeded &&
3261 readRect.x == 0 && readRect.width == readSize.width &&
3262 readRect.y == 0 && readRect.height == readSize.height &&
3263 drawRect.x == 0 && drawRect.width == drawSize.width &&
3264 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003265
Geoff Langc1f51be2013-06-11 11:49:14 -04003266 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003267
Geoff Lang125deab2013-08-09 13:34:16 -04003268 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || readRect.height < 0;
3269
3270 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3271 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3272 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3273 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3274
3275 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3276 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3277 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3278
Geoff Langc1f51be2013-06-11 11:49:14 -04003279 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003280 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3281 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003282 {
Geoff Lang125deab2013-08-09 13:34:16 -04003283 UINT dstX = drawRect.x;
3284 UINT dstY = drawRect.y;
3285
Geoff Langc1f51be2013-06-11 11:49:14 -04003286 D3D11_BOX readBox;
3287 readBox.left = readRect.x;
3288 readBox.right = readRect.x + readRect.width;
3289 readBox.top = readRect.y;
3290 readBox.bottom = readRect.y + readRect.height;
3291 readBox.front = 0;
3292 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003293
Geoff Lang125deab2013-08-09 13:34:16 -04003294 if (scissorNeeded)
3295 {
3296 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3297 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3298
3299 if (drawRect.x < scissor->x)
3300 {
3301 dstX = scissor->x;
3302 readBox.left += (scissor->x - drawRect.x);
3303 }
3304 if (drawRect.y < scissor->y)
3305 {
3306 dstY = scissor->y;
3307 readBox.top += (scissor->y - drawRect.y);
3308 }
3309 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3310 {
3311 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3312 }
3313 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3314 {
3315 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3316 }
3317 }
3318
Geoff Langc1f51be2013-06-11 11:49:14 -04003319 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3320 // We also require complete framebuffer copies for depth-stencil blit.
3321 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003322
Geoff Lang125deab2013-08-09 13:34:16 -04003323 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003324 readTexture, readSubresource, pSrcBox);
3325 result = true;
3326 }
3327 else
3328 {
3329 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003330 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003331
Geoff Lang975af372013-06-12 11:19:22 -04003332 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003333 {
Geoff Lang975af372013-06-12 11:19:22 -04003334 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003335 drawTexture, drawSubresource, drawArea, drawSize,
3336 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003337 }
3338 else if (depthBlit)
3339 {
Geoff Lang125deab2013-08-09 13:34:16 -04003340 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3341 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003342 }
3343 else if (stencilBlit)
3344 {
3345 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003346 drawTexture, drawSubresource, drawArea, drawSize,
3347 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003348 }
3349 else
3350 {
Geoff Lang685806d2013-06-12 11:16:36 -04003351 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003352 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3353 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003354 }
3355 }
3356
3357 SafeRelease(readTexture);
3358 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003359
3360 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003361}
3362
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003363ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3364{
3365 D3D11_TEXTURE2D_DESC textureDesc;
3366 source->GetDesc(&textureDesc);
3367
3368 if (textureDesc.SampleDesc.Count > 1)
3369 {
3370 D3D11_TEXTURE2D_DESC resolveDesc;
3371 resolveDesc.Width = textureDesc.Width;
3372 resolveDesc.Height = textureDesc.Height;
3373 resolveDesc.MipLevels = 1;
3374 resolveDesc.ArraySize = 1;
3375 resolveDesc.Format = textureDesc.Format;
3376 resolveDesc.SampleDesc.Count = 1;
3377 resolveDesc.SampleDesc.Quality = 0;
3378 resolveDesc.Usage = textureDesc.Usage;
3379 resolveDesc.BindFlags = textureDesc.BindFlags;
3380 resolveDesc.CPUAccessFlags = 0;
3381 resolveDesc.MiscFlags = 0;
3382
3383 ID3D11Texture2D *resolveTexture = NULL;
3384 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3385 if (FAILED(result))
3386 {
3387 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3388 return NULL;
3389 }
3390
3391 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3392 return resolveTexture;
3393 }
3394 else
3395 {
3396 source->AddRef();
3397 return source;
3398 }
3399}
3400
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003401bool Renderer11::getLUID(LUID *adapterLuid) const
3402{
3403 adapterLuid->HighPart = 0;
3404 adapterLuid->LowPart = 0;
3405
3406 if (!mDxgiAdapter)
3407 {
3408 return false;
3409 }
3410
3411 DXGI_ADAPTER_DESC adapterDesc;
3412 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3413 {
3414 return false;
3415 }
3416
3417 *adapterLuid = adapterDesc.AdapterLuid;
3418 return true;
3419}
3420
Geoff Lang005df412013-10-16 14:12:50 -04003421GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003422{
3423 int clientVersion = getCurrentClientVersion();
3424 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3425}
3426
Geoff Lang61e49a52013-05-29 10:22:58 -04003427Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3428{
3429 MultisampleSupportInfo supportInfo = { 0 };
3430
3431 UINT formatSupport;
3432 HRESULT result;
3433
3434 result = mDevice->CheckFormatSupport(format, &formatSupport);
3435 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3436 {
3437 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3438 {
3439 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3440 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3441 {
3442 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3443 }
3444 else
3445 {
3446 supportInfo.qualityLevels[i - 1] = 0;
3447 }
3448 }
3449 }
3450
3451 return supportInfo;
3452}
3453
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003454}