blob: 32c55fcda765deb96501c0fbff6d0cfccfd46a1c [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002//
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00003// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// Renderer11.cpp: Implements a back-end specific class for the D3D11 renderer.
9
daniel@transgaming.com5503fd02012-11-28 19:38:57 +000010#include "libGLESv2/main.h"
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +000011#include "common/utilities.h"
daniel@transgaming.com18adad02012-11-28 21:04:03 +000012#include "libGLESv2/Buffer.h"
daniel@transgaming.com53670042012-11-28 20:55:51 +000013#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com80fc3322012-11-28 21:02:13 +000014#include "libGLESv2/Framebuffer.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000015#include "libGLESv2/RenderBuffer.h"
Geoff Langd47e0fc2013-08-29 11:40:43 -040016#include "libGLESv2/renderer/d3d11/Renderer11.h"
17#include "libGLESv2/renderer/d3d11/RenderTarget11.h"
18#include "libGLESv2/renderer/d3d11/renderer11_utils.h"
19#include "libGLESv2/renderer/d3d11/formatutils11.h"
20#include "libGLESv2/renderer/d3d11/ShaderExecutable11.h"
21#include "libGLESv2/renderer/d3d11/SwapChain11.h"
22#include "libGLESv2/renderer/d3d11/Image11.h"
23#include "libGLESv2/renderer/d3d11/VertexBuffer11.h"
24#include "libGLESv2/renderer/d3d11/IndexBuffer11.h"
25#include "libGLESv2/renderer/d3d11/BufferStorage11.h"
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000026#include "libGLESv2/renderer/VertexDataManager.h"
27#include "libGLESv2/renderer/IndexDataManager.h"
Geoff Langd47e0fc2013-08-29 11:40:43 -040028#include "libGLESv2/renderer/d3d11/TextureStorage11.h"
29#include "libGLESv2/renderer/d3d11/Query11.h"
30#include "libGLESv2/renderer/d3d11/Fence11.h"
31#include "libGLESv2/renderer/d3d11/Blit11.h"
32#include "libGLESv2/renderer/d3d11/Clear11.h"
33#include "libGLESv2/renderer/d3d11/PixelTransfer11.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000034#include "libEGL/Display.h"
35
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000036#ifdef _DEBUG
37// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
38// and conformance tests. to enable all warnings, remove this define.
39#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
40#endif
41
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000042namespace rx
43{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000044static const DXGI_FORMAT RenderTargetFormats[] =
45 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000046 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000047 DXGI_FORMAT_R8G8B8A8_UNORM
48 };
49
50static const DXGI_FORMAT DepthStencilFormats[] =
51 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000052 DXGI_FORMAT_UNKNOWN,
53 DXGI_FORMAT_D24_UNORM_S8_UINT,
54 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000055 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000056
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000057enum
58{
59 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
60};
61
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000062Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
63{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000064 mVertexDataManager = NULL;
65 mIndexDataManager = NULL;
66
daniel@transgaming.comc5114302012-12-20 21:11:36 +000067 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000068 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000069
Geoff Langb86b9792013-06-04 16:32:05 -040070 mBlit = NULL;
Jamie Madilla21eea32013-09-18 14:36:25 -040071 mPixelTransfer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000072
Geoff Langda507fe2013-08-20 12:01:42 -040073 mClear = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000074
daniel@transgaming.combdf787f2013-02-01 03:20:36 +000075 mSyncQuery = NULL;
76
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000077 mD3d11Module = NULL;
78 mDxgiModule = NULL;
79
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000080 mDeviceLost = false;
81
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +000082 mMaxSupportedSamples = 0;
83
daniel@transgaming.com25072f62012-11-28 19:31:32 +000084 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000085 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000086 mDxgiAdapter = NULL;
87 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +000088
89 mDriverConstantBufferVS = NULL;
90 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +000091
92 mBGRATextureSupport = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +000093
94 mIsGeometryShaderActive = false;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000095}
96
97Renderer11::~Renderer11()
98{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +000099 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000100}
101
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000102Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
103{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000104 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000105 return static_cast<rx::Renderer11*>(renderer);
106}
107
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000108#ifndef __d3d11_1_h__
109#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
110#endif
111
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000112EGLint Renderer11::initialize()
113{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000114 if (!initializeCompiler())
115 {
116 return EGL_NOT_INITIALIZED;
117 }
118
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000119 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
120 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000121
122 if (mD3d11Module == NULL || mDxgiModule == NULL)
123 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000124 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000125 return EGL_NOT_INITIALIZED;
126 }
127
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000128 // create the D3D11 device
129 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000130 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000131
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000132 if (D3D11CreateDevice == NULL)
133 {
134 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
135 return EGL_NOT_INITIALIZED;
136 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000137
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000138 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000139 {
140 D3D_FEATURE_LEVEL_11_0,
141 D3D_FEATURE_LEVEL_10_1,
142 D3D_FEATURE_LEVEL_10_0,
143 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000144
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000145 HRESULT result = S_OK;
146
147#ifdef _DEBUG
148 result = D3D11CreateDevice(NULL,
149 D3D_DRIVER_TYPE_HARDWARE,
150 NULL,
151 D3D11_CREATE_DEVICE_DEBUG,
152 featureLevels,
153 ArraySize(featureLevels),
154 D3D11_SDK_VERSION,
155 &mDevice,
156 &mFeatureLevel,
157 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000158
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000159 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000160 {
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000161 ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n");
162 }
163
164 if (!mDevice || FAILED(result))
165#endif
166 {
167 result = D3D11CreateDevice(NULL,
168 D3D_DRIVER_TYPE_HARDWARE,
169 NULL,
170 0,
171 featureLevels,
172 ArraySize(featureLevels),
173 D3D11_SDK_VERSION,
174 &mDevice,
175 &mFeatureLevel,
176 &mDeviceContext);
177
178 if (!mDevice || FAILED(result))
179 {
180 ERR("Could not create D3D11 device - aborting!\n");
181 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
182 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000183 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000184
185 IDXGIDevice *dxgiDevice = NULL;
186 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
187
188 if (FAILED(result))
189 {
190 ERR("Could not query DXGI device - aborting!\n");
191 return EGL_NOT_INITIALIZED;
192 }
193
194 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
195
196 if (FAILED(result))
197 {
198 ERR("Could not retrieve DXGI adapter - aborting!\n");
199 return EGL_NOT_INITIALIZED;
200 }
201
Geoff Langea228632013-07-30 15:17:12 -0400202 SafeRelease(dxgiDevice);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000203
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000204 mDxgiAdapter->GetDesc(&mAdapterDescription);
205 memset(mDescription, 0, sizeof(mDescription));
206 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
207
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000208 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
209
210 if (!mDxgiFactory || FAILED(result))
211 {
212 ERR("Could not create DXGI factory - aborting!\n");
213 return EGL_NOT_INITIALIZED;
214 }
215
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000216 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
217#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
218 ID3D11InfoQueue *infoQueue;
219 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
220
221 if (SUCCEEDED(result))
222 {
223 D3D11_MESSAGE_ID hideMessages[] =
224 {
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000225 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000226 };
227
228 D3D11_INFO_QUEUE_FILTER filter = {0};
229 filter.DenyList.NumIDs = ArraySize(hideMessages);
230 filter.DenyList.pIDList = hideMessages;
231
232 infoQueue->AddStorageFilterEntries(&filter);
Geoff Langea228632013-07-30 15:17:12 -0400233 SafeRelease(infoQueue);
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000234 }
235#endif
236
Geoff Lang61e49a52013-05-29 10:22:58 -0400237 mMaxSupportedSamples = 0;
238
239 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
240 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000241 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400242 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
243 mMultisampleSupportMap.insert(std::make_pair(*i, support));
244 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000245 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000246
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000247 initializeDevice();
248
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000249 // BGRA texture support is optional in feature levels 10 and 10_1
250 UINT formatSupport;
251 result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport);
252 if (FAILED(result))
253 {
254 ERR("Error checking BGRA format support: 0x%08X", result);
255 }
256 else
257 {
258 const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET);
259 mBGRATextureSupport = (formatSupport & flags) == flags;
260 }
261
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000262 // Check floating point texture support
263 static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE;
264 static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET;
265 static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE;
266
267 DXGI_FORMAT float16Formats[] =
268 {
269 DXGI_FORMAT_R16_FLOAT,
270 DXGI_FORMAT_R16G16_FLOAT,
271 DXGI_FORMAT_R16G16B16A16_FLOAT,
272 };
273
274 DXGI_FORMAT float32Formats[] =
275 {
276 DXGI_FORMAT_R32_FLOAT,
277 DXGI_FORMAT_R32G32_FLOAT,
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000278 DXGI_FORMAT_R32G32B32A32_FLOAT,
279 };
280
281 mFloat16TextureSupport = true;
282 mFloat16FilterSupport = true;
283 mFloat16RenderSupport = true;
284 for (unsigned int i = 0; i < ArraySize(float16Formats); i++)
285 {
286 if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport)))
287 {
288 mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
289 mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
290 mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
291 }
292 else
293 {
294 mFloat16TextureSupport = false;
295 mFloat16RenderSupport = false;
296 mFloat16FilterSupport = false;
297 }
298 }
299
300 mFloat32TextureSupport = true;
301 mFloat32FilterSupport = true;
302 mFloat32RenderSupport = true;
303 for (unsigned int i = 0; i < ArraySize(float32Formats); i++)
304 {
305 if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport)))
306 {
307 mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
308 mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
309 mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
310 }
311 else
312 {
313 mFloat32TextureSupport = false;
314 mFloat32FilterSupport = false;
315 mFloat32RenderSupport = false;
316 }
317 }
318
Geoff Lang632192d2013-10-04 13:40:46 -0400319 DXGI_FORMAT rgTextureFormats[] =
320 {
321 DXGI_FORMAT_R8_UNORM,
322 DXGI_FORMAT_R8G8_UNORM,
323 DXGI_FORMAT_R16_FLOAT,
324 DXGI_FORMAT_R16G16_FLOAT,
325 DXGI_FORMAT_R32_FLOAT,
326 DXGI_FORMAT_R32G32_FLOAT,
327 };
328
329 mRGTextureSupport = true;
330 for (unsigned int i = 0; i < ArraySize(rgTextureFormats); i++)
331 {
332 if (SUCCEEDED(mDevice->CheckFormatSupport(rgTextureFormats[i], &formatSupport)))
333 {
334 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
335 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
336 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
337 }
338 else
339 {
340 mRGTextureSupport = false;
341 }
342 }
343
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +0000344 // Check compressed texture support
345 const unsigned int requiredCompressedTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D;
346
347 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &formatSupport)))
348 {
349 mDXT1TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
350 }
351 else
352 {
353 mDXT1TextureSupport = false;
354 }
355
356 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport)))
357 {
358 mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
359 }
360 else
361 {
362 mDXT3TextureSupport = false;
363 }
364
365 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC5_UNORM, &formatSupport)))
366 {
367 mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
368 }
369 else
370 {
371 mDXT5TextureSupport = false;
372 }
373
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +0000374 // Check depth texture support
375 DXGI_FORMAT depthTextureFormats[] =
376 {
377 DXGI_FORMAT_D16_UNORM,
378 DXGI_FORMAT_D24_UNORM_S8_UINT,
379 };
380
381 static const unsigned int requiredDepthTextureFlags = D3D11_FORMAT_SUPPORT_DEPTH_STENCIL |
382 D3D11_FORMAT_SUPPORT_TEXTURE2D;
383
384 mDepthTextureSupport = true;
385 for (unsigned int i = 0; i < ArraySize(depthTextureFormats); i++)
386 {
387 if (SUCCEEDED(mDevice->CheckFormatSupport(depthTextureFormats[i], &formatSupport)))
388 {
389 mDepthTextureSupport = mDepthTextureSupport && ((formatSupport & requiredDepthTextureFlags) == requiredDepthTextureFlags);
390 }
391 else
392 {
393 mDepthTextureSupport = false;
394 }
395 }
396
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000397 return EGL_SUCCESS;
398}
399
400// do any one-time device initialization
401// NOTE: this is also needed after a device lost/reset
402// to reset the scene status and ensure the default states are reset.
403void Renderer11::initializeDevice()
404{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000405 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000406 mInputLayoutCache.initialize(mDevice, mDeviceContext);
407
408 ASSERT(!mVertexDataManager && !mIndexDataManager);
409 mVertexDataManager = new VertexDataManager(this);
410 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000411
Geoff Langb86b9792013-06-04 16:32:05 -0400412 ASSERT(!mBlit);
413 mBlit = new Blit11(this);
414
Geoff Langda507fe2013-08-20 12:01:42 -0400415 ASSERT(!mClear);
416 mClear = new Clear11(this);
417
Jamie Madilla21eea32013-09-18 14:36:25 -0400418 ASSERT(!mPixelTransfer);
419 mPixelTransfer = new PixelTransfer11(this);
420
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000421 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000422}
423
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000424int Renderer11::generateConfigs(ConfigDesc **configDescList)
425{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000426 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
427 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000428 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
429 int numConfigs = 0;
430
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000431 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000432 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000433 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000434 {
435 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
436
437 UINT formatSupport = 0;
438 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000439
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000440 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
441 {
442 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
443
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000444 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000445
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000446 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
447 {
448 UINT formatSupport = 0;
449 result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
450 depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
451 }
452
453 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000454 {
Jamie Madilld6cb2442013-07-10 15:13:38 -0400455 // FIXME: parse types from context version
456 ASSERT(d3d11_gl::GetInternalFormat(renderTargetFormat, 2) == d3d11_gl::GetInternalFormat(renderTargetFormat, 3));
457 ASSERT(d3d11_gl::GetInternalFormat(depthStencilFormat, 2) == d3d11_gl::GetInternalFormat(depthStencilFormat, 3));
458
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000459 ConfigDesc newConfig;
Jamie Madilld6cb2442013-07-10 15:13:38 -0400460 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat, getCurrentClientVersion());
461 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat, getCurrentClientVersion());
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000462 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
463 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000464 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000465
466 (*configDescList)[numConfigs++] = newConfig;
467 }
468 }
469 }
470 }
471
472 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000473}
474
475void Renderer11::deleteConfigs(ConfigDesc *configDescList)
476{
477 delete [] (configDescList);
478}
479
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000480void Renderer11::sync(bool block)
481{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000482 if (block)
483 {
484 HRESULT result;
485
486 if (!mSyncQuery)
487 {
488 D3D11_QUERY_DESC queryDesc;
489 queryDesc.Query = D3D11_QUERY_EVENT;
490 queryDesc.MiscFlags = 0;
491
492 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
493 ASSERT(SUCCEEDED(result));
494 }
495
496 mDeviceContext->End(mSyncQuery);
497 mDeviceContext->Flush();
498
499 do
500 {
501 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
502
503 // Keep polling, but allow other threads to do something useful first
504 Sleep(0);
505
506 if (testDeviceLost(true))
507 {
508 return;
509 }
510 }
511 while (result == S_FALSE);
512 }
513 else
514 {
515 mDeviceContext->Flush();
516 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000517}
518
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000519SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
520{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000521 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000522}
523
Geoff Lange2e0ce02013-09-17 17:05:08 -0400524void Renderer11::generateSwizzle(gl::Texture *texture)
525{
Geoff Lang42477a42013-09-17 17:07:02 -0400526 if (texture)
527 {
528 TextureStorageInterface *texStorage = texture->getNativeTexture();
529 if (texStorage)
530 {
531 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
532
533 storage11->generateSwizzles(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
534 texture->getSwizzleAlpha());
535 }
536 }
Geoff Lange2e0ce02013-09-17 17:05:08 -0400537}
538
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000539void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
540{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000541 if (type == gl::SAMPLER_PIXEL)
542 {
543 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
544 {
545 ERR("Pixel shader sampler index %i is not valid.", index);
546 return;
547 }
548
549 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
550 {
551 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
552
553 if (!dxSamplerState)
554 {
555 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
556 "sampler state for pixel shaders at slot %i.", index);
557 }
558
559 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
560
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000561 mCurPixelSamplerStates[index] = samplerState;
562 }
563
564 mForceSetPixelSamplerStates[index] = false;
565 }
566 else if (type == gl::SAMPLER_VERTEX)
567 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000568 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000569 {
570 ERR("Vertex shader sampler index %i is not valid.", index);
571 return;
572 }
573
574 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
575 {
576 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
577
578 if (!dxSamplerState)
579 {
580 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
581 "sampler state for vertex shaders at slot %i.", index);
582 }
583
584 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
585
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000586 mCurVertexSamplerStates[index] = samplerState;
587 }
588
589 mForceSetVertexSamplerStates[index] = false;
590 }
591 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000592}
593
594void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
595{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000596 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000597 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000598
599 if (texture)
600 {
601 TextureStorageInterface *texStorage = texture->getNativeTexture();
602 if (texStorage)
603 {
604 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
Geoff Lang644bbf22013-09-17 17:02:43 -0400605 textureSRV = storage11->getSRV(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
606 texture->getSwizzleAlpha());
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000607 }
608
609 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
610 // missing the shader resource view
611 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000612
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000613 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000614 }
615
616 if (type == gl::SAMPLER_PIXEL)
617 {
618 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
619 {
620 ERR("Pixel shader sampler index %i is not valid.", index);
621 return;
622 }
623
Geoff Lang91382e52014-01-07 16:16:30 -0500624 if (forceSetTexture || mCurPixelSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000625 {
626 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
627 }
628
Geoff Lang91382e52014-01-07 16:16:30 -0500629 mCurPixelSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000630 }
631 else if (type == gl::SAMPLER_VERTEX)
632 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000633 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000634 {
635 ERR("Vertex shader sampler index %i is not valid.", index);
636 return;
637 }
638
Geoff Lang91382e52014-01-07 16:16:30 -0500639 if (forceSetTexture || mCurVertexSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000640 {
641 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
642 }
643
Geoff Lang91382e52014-01-07 16:16:30 -0500644 mCurVertexSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000645 }
646 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000647}
648
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000649bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
650{
651 // convert buffers to ID3D11Buffer*
652 ID3D11Buffer *vertexConstantBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = { NULL };
653 ID3D11Buffer *pixelConstantBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = { NULL };
654
655 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
656 {
657 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
658 if (uniformBuffer)
659 {
660 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500661 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000662
663 if (!constantBuffer)
664 {
665 return false;
666 }
667
Geoff Langc6354ee2013-07-22 10:40:07 -0400668 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
669 {
670 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
671 1, &constantBuffer);
672 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
673 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000674 }
675 }
676
677 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
678 {
679 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
680 if (uniformBuffer)
681 {
682 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500683 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000684
685 if (!constantBuffer)
686 {
687 return false;
688 }
689
Geoff Langc6354ee2013-07-22 10:40:07 -0400690 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
691 {
692 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
693 1, &constantBuffer);
694 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
695 }
696
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000697 pixelConstantBuffers[uniformBufferIndex] = constantBuffer;
698 }
699 }
700
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000701 return true;
702}
703
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000704void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000705{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000706 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000707 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000708 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
709 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000710 if (!dxRasterState)
711 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000712 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000713 "rasterizer state.");
714 }
715
716 mDeviceContext->RSSetState(dxRasterState);
717
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000718 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000719 }
720
721 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000722}
723
Geoff Langc142e9d2013-09-30 15:19:47 -0400724void Renderer11::setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000725 unsigned int sampleMask)
726{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000727 if (mForceSetBlendState ||
728 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400729 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000730 sampleMask != mCurSampleMask)
731 {
Geoff Langc142e9d2013-09-30 15:19:47 -0400732 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(framebuffer, blendState);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000733 if (!dxBlendState)
734 {
735 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
736 "blend state.");
737 }
738
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000739 float blendColors[4] = {0.0f};
740 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
741 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
742 {
743 blendColors[0] = blendColor.red;
744 blendColors[1] = blendColor.green;
745 blendColors[2] = blendColor.blue;
746 blendColors[3] = blendColor.alpha;
747 }
748 else
749 {
750 blendColors[0] = blendColor.alpha;
751 blendColors[1] = blendColor.alpha;
752 blendColors[2] = blendColor.alpha;
753 blendColors[3] = blendColor.alpha;
754 }
755
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000756 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
757
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000758 mCurBlendState = blendState;
759 mCurBlendColor = blendColor;
760 mCurSampleMask = sampleMask;
761 }
762
763 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000764}
765
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000766void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000767 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000768{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000769 if (mForceSetDepthStencilState ||
770 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
771 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
772 {
773 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
774 stencilRef != stencilBackRef ||
775 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
776 {
777 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
778 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000779 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000780 }
781
782 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
783 if (!dxDepthStencilState)
784 {
785 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
786 "setting the default depth stencil state.");
787 }
788
Jamie Madillec91cd32014-01-21 16:38:12 -0500789 // Max D3D11 stencil reference value is 0xFF, corresponding to the max 8 bits in a stencil buffer
790 // GL specifies we should clamp the ref value to the nearest bit depth when doing stencil ops
791 META_ASSERT(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF);
792 META_ASSERT(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF);
793 UINT stencilRef = std::min(stencilRef, 0xFFu);
794
795 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, stencilRef);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000796
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000797 mCurDepthStencilState = depthStencilState;
798 mCurStencilRef = stencilRef;
799 mCurStencilBackRef = stencilBackRef;
800 }
801
802 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000803}
804
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000805void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000806{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000807 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
808 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000809 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000810 if (enabled)
811 {
812 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000813 rect.left = std::max(0, scissor.x);
814 rect.top = std::max(0, scissor.y);
815 rect.right = scissor.x + std::max(0, scissor.width);
816 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000817
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000818 mDeviceContext->RSSetScissorRects(1, &rect);
819 }
820
821 if (enabled != mScissorEnabled)
822 {
823 mForceSetRasterState = true;
824 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000825
826 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000827 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000828 }
829
830 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000831}
832
daniel@transgaming.com12985182012-12-20 20:56:31 +0000833bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000834 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000835{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000836 gl::Rectangle actualViewport = viewport;
837 float actualZNear = gl::clamp01(zNear);
838 float actualZFar = gl::clamp01(zFar);
839 if (ignoreViewport)
840 {
841 actualViewport.x = 0;
842 actualViewport.y = 0;
843 actualViewport.width = mRenderTargetDesc.width;
844 actualViewport.height = mRenderTargetDesc.height;
845 actualZNear = 0.0f;
846 actualZFar = 1.0f;
847 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000848
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000849 // Get D3D viewport bounds, which depends on the feature level
850 const Range& viewportBounds = getViewportBounds();
851
852 // 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 +0000853 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000854 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
855 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
856 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
857 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
858 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
859 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000860 dxViewport.MinDepth = actualZNear;
861 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000862
863 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
864 {
865 return false; // Nothing to render
866 }
867
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000868 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
869 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000870
daniel@transgaming.com53670042012-11-28 20:55:51 +0000871 if (viewportChanged)
872 {
873 mDeviceContext->RSSetViewports(1, &dxViewport);
874
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000875 mCurViewport = actualViewport;
876 mCurNear = actualZNear;
877 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000878
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000879 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
880 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
881 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
882 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000883
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000884 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
885 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000886
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000887 mVertexConstants.depthRange[0] = actualZNear;
888 mVertexConstants.depthRange[1] = actualZFar;
889 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000890
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000891 mPixelConstants.depthRange[0] = actualZNear;
892 mPixelConstants.depthRange[1] = actualZFar;
893 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000894 }
895
896 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000897 return true;
898}
899
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000900bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
901{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000902 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000903
Geoff Lang57e713e2013-07-31 17:01:58 -0400904 GLsizei minCount = 0;
905
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000906 switch (mode)
907 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400908 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
909 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
910 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
911 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
912 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
913 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000914 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400915 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000916 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000917 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000918 }
919
Geoff Lang4c095862013-07-22 10:43:36 -0400920 if (primitiveTopology != mCurrentPrimitiveTopology)
921 {
922 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
923 mCurrentPrimitiveTopology = primitiveTopology;
924 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000925
Geoff Lang57e713e2013-07-31 17:01:58 -0400926 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000927}
928
929bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000930{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000931 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000932 // Also extract the render target dimensions and view
933 unsigned int renderTargetWidth = 0;
934 unsigned int renderTargetHeight = 0;
935 GLenum renderTargetFormat = 0;
936 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
937 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
938 bool missingColorRenderTarget = true;
939
940 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000941 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000942 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
943
944 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000945 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000946 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
947 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
948
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000949 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000950
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000951 if (!colorbuffer)
952 {
953 ERR("render target pointer unexpectedly null.");
954 return false;
955 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000956
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000957 // check for zero-sized default framebuffer, which is a special case.
958 // in this case we do not wish to modify any state and just silently return false.
959 // this will not report any gl error but will cause the calling method to return.
960 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
961 {
962 return false;
963 }
964
965 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
966
967 // Extract the render target dimensions and view
968 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
969 if (!renderTarget)
970 {
971 ERR("render target pointer unexpectedly null.");
972 return false;
973 }
974
975 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
976 if (!framebufferRTVs[colorAttachment])
977 {
978 ERR("render target view pointer unexpectedly null.");
979 return false;
980 }
981
982 if (missingColorRenderTarget)
983 {
984 renderTargetWidth = colorbuffer->getWidth();
985 renderTargetHeight = colorbuffer->getHeight();
986 renderTargetFormat = colorbuffer->getActualFormat();
987 missingColorRenderTarget = false;
988 }
Jamie Madillba597af2013-10-22 13:12:15 -0400989
Geoff Lang91382e52014-01-07 16:16:30 -0500990 // TODO: Detect if this color buffer is already bound as a texture and unbind it first to prevent
991 // D3D11 warnings.
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000992 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000993 }
994
995 // Get the depth stencil render buffer and serials
996 gl::Renderbuffer *depthStencil = NULL;
997 unsigned int depthbufferSerial = 0;
998 unsigned int stencilbufferSerial = 0;
999 if (framebuffer->getDepthbufferType() != GL_NONE)
1000 {
1001 depthStencil = framebuffer->getDepthbuffer();
1002 if (!depthStencil)
1003 {
1004 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001005 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001006 return false;
1007 }
1008
1009 depthbufferSerial = depthStencil->getSerial();
1010 }
1011 else if (framebuffer->getStencilbufferType() != GL_NONE)
1012 {
1013 depthStencil = framebuffer->getStencilbuffer();
1014 if (!depthStencil)
1015 {
1016 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001017 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001018 return false;
1019 }
1020
1021 stencilbufferSerial = depthStencil->getSerial();
1022 }
1023
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001024 // Extract the depth stencil sizes and view
1025 unsigned int depthSize = 0;
1026 unsigned int stencilSize = 0;
1027 ID3D11DepthStencilView* framebufferDSV = NULL;
1028 if (depthStencil)
1029 {
1030 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
1031 if (!depthStencilRenderTarget)
1032 {
1033 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001034 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001035 return false;
1036 }
1037
1038 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
1039 if (!framebufferDSV)
1040 {
1041 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001042 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001043 return false;
1044 }
1045
1046 // If there is no render buffer, the width, height and format values come from
1047 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001048 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001049 {
1050 renderTargetWidth = depthStencil->getWidth();
1051 renderTargetHeight = depthStencil->getHeight();
1052 renderTargetFormat = depthStencil->getActualFormat();
1053 }
1054
1055 depthSize = depthStencil->getDepthSize();
1056 stencilSize = depthStencil->getStencilSize();
1057 }
1058
1059 // Apply the render target and depth stencil
1060 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001061 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001062 depthbufferSerial != mAppliedDepthbufferSerial ||
1063 stencilbufferSerial != mAppliedStencilbufferSerial)
1064 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001065 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001066
1067 mRenderTargetDesc.width = renderTargetWidth;
1068 mRenderTargetDesc.height = renderTargetHeight;
1069 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001070 mForceSetViewport = true;
1071 mForceSetScissor = true;
Geoff Langc142e9d2013-09-30 15:19:47 -04001072 mForceSetBlendState = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001073
1074 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1075 {
1076 mCurDepthSize = depthSize;
1077 mForceSetRasterState = true;
1078 }
1079
1080 mCurStencilSize = stencilSize;
1081
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001082 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1083 {
1084 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
1085 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001086 mAppliedDepthbufferSerial = depthbufferSerial;
1087 mAppliedStencilbufferSerial = stencilbufferSerial;
1088 mRenderTargetDescInitialized = true;
1089 mDepthStencilInitialized = true;
1090 }
1091
Geoff Lang42477a42013-09-17 17:07:02 -04001092 invalidateFramebufferSwizzles(framebuffer);
1093
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001094 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001095}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001096
Jamie Madill57a89722013-07-02 11:57:03 -04001097GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001098 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001099{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001100 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001101 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001102 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001103 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001104 return err;
1105 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001106
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001107 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001108}
1109
daniel@transgaming.com31240482012-11-28 21:06:41 +00001110GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001111{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001112 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001113
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001114 if (err == GL_NO_ERROR)
1115 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001116 if (indexInfo->storage)
1117 {
1118 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1119 {
1120 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1121 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1122
Geoff Lang9c53f1e2014-01-08 13:41:47 -05001123 mDeviceContext->IASetIndexBuffer(storage->getBuffer(BUFFER_USAGE_INDEX), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001124
1125 mAppliedIBSerial = 0;
1126 mAppliedStorageIBSerial = storage->getSerial();
1127 mAppliedIBOffset = indexInfo->startOffset;
1128 }
1129 }
1130 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001131 {
1132 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1133
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001134 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001135
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001136 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001137 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001138 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001139 }
1140 }
1141
1142 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001143}
1144
1145void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1146{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001147 if (mode == GL_LINE_LOOP)
1148 {
1149 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1150 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001151 else if (mode == GL_TRIANGLE_FAN)
1152 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001153 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001154 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001155 else if (instances > 0)
1156 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001157 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001158 }
1159 else
1160 {
1161 mDeviceContext->Draw(count, 0);
1162 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001163}
1164
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001165void 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 +00001166{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001167 if (mode == GL_LINE_LOOP)
1168 {
1169 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1170 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001171 else if (mode == GL_TRIANGLE_FAN)
1172 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001173 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1174 }
1175 else if (instances > 0)
1176 {
1177 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001178 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001179 else
1180 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001181 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001182 }
1183}
1184
1185void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1186{
1187 // Get the raw indices for an indexed draw
1188 if (type != GL_NONE && elementArrayBuffer)
1189 {
1190 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001191 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001192 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001193 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001194 }
1195
1196 if (!mLineLoopIB)
1197 {
1198 mLineLoopIB = new StreamingIndexBufferInterface(this);
1199 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1200 {
1201 delete mLineLoopIB;
1202 mLineLoopIB = NULL;
1203
1204 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001205 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001206 }
1207 }
1208
Geoff Lang57e713e2013-07-31 17:01:58 -04001209 // Checked by Renderer11::applyPrimitiveType
1210 ASSERT(count >= 0);
1211
1212 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001213 {
1214 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1215 return gl::error(GL_OUT_OF_MEMORY);
1216 }
1217
Geoff Lang57e713e2013-07-31 17:01:58 -04001218 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001219 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1220 {
1221 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001222 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001223 }
1224
1225 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001226 unsigned int offset;
1227 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001228 {
1229 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001230 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001231 }
1232
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001233 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001234 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001235
1236 switch (type)
1237 {
1238 case GL_NONE: // Non-indexed draw
1239 for (int i = 0; i < count; i++)
1240 {
1241 data[i] = i;
1242 }
1243 data[count] = 0;
1244 break;
1245 case GL_UNSIGNED_BYTE:
1246 for (int i = 0; i < count; i++)
1247 {
1248 data[i] = static_cast<const GLubyte*>(indices)[i];
1249 }
1250 data[count] = static_cast<const GLubyte*>(indices)[0];
1251 break;
1252 case GL_UNSIGNED_SHORT:
1253 for (int i = 0; i < count; i++)
1254 {
1255 data[i] = static_cast<const GLushort*>(indices)[i];
1256 }
1257 data[count] = static_cast<const GLushort*>(indices)[0];
1258 break;
1259 case GL_UNSIGNED_INT:
1260 for (int i = 0; i < count; i++)
1261 {
1262 data[i] = static_cast<const GLuint*>(indices)[i];
1263 }
1264 data[count] = static_cast<const GLuint*>(indices)[0];
1265 break;
1266 default: UNREACHABLE();
1267 }
1268
1269 if (!mLineLoopIB->unmapBuffer())
1270 {
1271 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001272 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001273 }
1274
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001275 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001276 {
1277 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1278
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001279 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001280 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001281 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001282 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001283 }
1284
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001285 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001286}
1287
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001288void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001289{
1290 // Get the raw indices for an indexed draw
1291 if (type != GL_NONE && elementArrayBuffer)
1292 {
1293 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001294 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001295 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001296 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001297 }
1298
1299 if (!mTriangleFanIB)
1300 {
1301 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1302 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1303 {
1304 delete mTriangleFanIB;
1305 mTriangleFanIB = NULL;
1306
1307 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001308 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001309 }
1310 }
1311
Geoff Lang57e713e2013-07-31 17:01:58 -04001312 // Checked by Renderer11::applyPrimitiveType
1313 ASSERT(count >= 3);
1314
Geoff Langeadfd572013-07-09 15:55:07 -04001315 const unsigned int numTris = count - 2;
1316
Geoff Lang57e713e2013-07-31 17:01:58 -04001317 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001318 {
1319 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1320 return gl::error(GL_OUT_OF_MEMORY);
1321 }
1322
1323 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001324 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1325 {
1326 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001327 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001328 }
1329
1330 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001331 unsigned int offset;
1332 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001333 {
1334 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001335 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001336 }
1337
1338 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001339 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001340
1341 switch (type)
1342 {
1343 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001344 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001345 {
1346 data[i*3 + 0] = 0;
1347 data[i*3 + 1] = i + 1;
1348 data[i*3 + 2] = i + 2;
1349 }
1350 break;
1351 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001352 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001353 {
1354 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1355 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1356 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1357 }
1358 break;
1359 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001360 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001361 {
1362 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1363 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1364 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1365 }
1366 break;
1367 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001368 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001369 {
1370 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1371 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1372 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1373 }
1374 break;
1375 default: UNREACHABLE();
1376 }
1377
1378 if (!mTriangleFanIB->unmapBuffer())
1379 {
1380 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001381 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001382 }
1383
1384 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1385 {
1386 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1387
1388 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1389 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001390 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001391 mAppliedIBOffset = indexBufferOffset;
1392 }
1393
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001394 if (instances > 0)
1395 {
1396 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1397 }
1398 else
1399 {
1400 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1401 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001402}
1403
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001404void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1405{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001406 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001407 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1408
1409 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001410 {
1411 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1412 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001413
daniel@transgaming.come4991412012-12-20 20:55:34 +00001414 ID3D11VertexShader *vertexShader = NULL;
1415 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001416
daniel@transgaming.come4991412012-12-20 20:55:34 +00001417 ID3D11PixelShader *pixelShader = NULL;
1418 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001419
daniel@transgaming.come4991412012-12-20 20:55:34 +00001420 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1421 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001422
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001423 programBinary->dirtyAllUniforms();
1424
1425 mAppliedProgramBinarySerial = programBinarySerial;
1426 }
1427
1428 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001429 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001430
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001431 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001432 {
1433 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001434 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001435 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1436 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001437 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1438 }
1439 else
1440 {
1441 mDeviceContext->GSSetShader(NULL, NULL, 0);
1442 }
1443
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001444 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001445 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001446}
1447
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001448void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001449{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001450 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1451 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001452
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001453 unsigned int totalRegisterCountVS = 0;
1454 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001455
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001456 bool vertexUniformsDirty = false;
1457 bool pixelUniformsDirty = false;
1458
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001459 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1460 {
1461 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001462
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001463 if (uniform->isReferencedByVertexShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001464 {
1465 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001466 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001467 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001468
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001469 if (uniform->isReferencedByFragmentShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001470 {
1471 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001472 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001473 }
1474 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001475
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001476 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1477 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1478
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001479 float (*mapVS)[4] = NULL;
1480 float (*mapPS)[4] = NULL;
1481
Shannon Woods5ab33c82013-06-26 15:31:09 -04001482 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1483 {
1484 D3D11_MAPPED_SUBRESOURCE map = {0};
1485 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1486 ASSERT(SUCCEEDED(result));
1487 mapVS = (float(*)[4])map.pData;
1488 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001489
Shannon Woods5ab33c82013-06-26 15:31:09 -04001490 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1491 {
1492 D3D11_MAPPED_SUBRESOURCE map = {0};
1493 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1494 ASSERT(SUCCEEDED(result));
1495 mapPS = (float(*)[4])map.pData;
1496 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001497
Jamie Madill5b085dc2013-08-30 13:21:11 -04001498 for (size_t uniformIndex = 0; uniformIndex < uniformArray->size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001499 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001500 gl::Uniform *uniform = (*uniformArray)[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001501
Nicolas Capense6050882013-07-08 10:43:10 -04001502 if (!gl::IsSampler(uniform->type))
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001503 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001504 unsigned int componentCount = (4 - uniform->registerElement);
1505
Jamie Madill71cc91f2013-09-18 12:51:22 -04001506 // 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 -04001507 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001508
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001509 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001510 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001511 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001512 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001513
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001514 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001515 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001516 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001517 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001518 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001519
1520 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001521 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001522
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001523 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001524 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001525 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001526 }
1527
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001528 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001529 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001530 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001531 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001532
1533 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1534 {
1535 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1536 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1537 }
1538
1539 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1540 {
1541 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1542 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1543 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001544
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001545 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001546 if (!mDriverConstantBufferVS)
1547 {
1548 D3D11_BUFFER_DESC constantBufferDescription = {0};
1549 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1550 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1551 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1552 constantBufferDescription.CPUAccessFlags = 0;
1553 constantBufferDescription.MiscFlags = 0;
1554 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001555
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001556 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001557 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001558
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001559 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1560 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001561
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001562 if (!mDriverConstantBufferPS)
1563 {
1564 D3D11_BUFFER_DESC constantBufferDescription = {0};
1565 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1566 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1567 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1568 constantBufferDescription.CPUAccessFlags = 0;
1569 constantBufferDescription.MiscFlags = 0;
1570 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001571
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001572 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001573 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001574
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001575 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1576 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001577
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001578 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1579 {
1580 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1581 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1582 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001583
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001584 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1585 {
1586 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1587 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1588 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001589
1590 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001591 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1592 {
1593 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1594 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1595 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001596}
1597
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001598void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001599{
Geoff Langda507fe2013-08-20 12:01:42 -04001600 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001601 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001602}
1603
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001604void Renderer11::markAllStateDirty()
1605{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001606 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1607 {
1608 mAppliedRenderTargetSerials[rtIndex] = 0;
1609 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001610 mAppliedDepthbufferSerial = 0;
1611 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001612 mDepthStencilInitialized = false;
1613 mRenderTargetDescInitialized = false;
1614
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001615 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001616 {
1617 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001618 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001619 }
1620 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1621 {
1622 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001623 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001624 }
1625
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001626 mForceSetBlendState = true;
1627 mForceSetRasterState = true;
1628 mForceSetDepthStencilState = true;
1629 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001630 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001631
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001632 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001633 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001634 mAppliedIBOffset = 0;
1635
daniel@transgaming.come4991412012-12-20 20:55:34 +00001636 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001637 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1638 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001639
1640 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001641
1642 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1643 {
1644 mCurrentConstantBufferVS[i] = -1;
1645 mCurrentConstantBufferPS[i] = -1;
1646 }
1647
1648 mCurrentVertexConstantBuffer = NULL;
1649 mCurrentPixelConstantBuffer = NULL;
1650 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001651
1652 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001653}
1654
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001655void Renderer11::releaseDeviceResources()
1656{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001657 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001658 mInputLayoutCache.clear();
1659
Geoff Langea228632013-07-30 15:17:12 -04001660 SafeDelete(mVertexDataManager);
1661 SafeDelete(mIndexDataManager);
1662 SafeDelete(mLineLoopIB);
1663 SafeDelete(mTriangleFanIB);
1664 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001665 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001666 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001667
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001668 SafeRelease(mDriverConstantBufferVS);
1669 SafeRelease(mDriverConstantBufferPS);
1670 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001671}
1672
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001673void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001674{
1675 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001676 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001677}
1678
1679bool Renderer11::isDeviceLost()
1680{
1681 return mDeviceLost;
1682}
1683
1684// set notify to true to broadcast a message to all contexts of the device loss
1685bool Renderer11::testDeviceLost(bool notify)
1686{
1687 bool isLost = false;
1688
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001689 // GetRemovedReason is used to test if the device is removed
1690 HRESULT result = mDevice->GetDeviceRemovedReason();
1691 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001692
1693 if (isLost)
1694 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001695 // Log error if this is a new device lost event
1696 if (mDeviceLost == false)
1697 {
1698 ERR("The D3D11 device was removed: 0x%08X", result);
1699 }
1700
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001701 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001702 // we'll probably get this done again by notifyDeviceLost
1703 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001704 // Note that we don't want to clear the device loss status here
1705 // -- this needs to be done by resetDevice
1706 mDeviceLost = true;
1707 if (notify)
1708 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001709 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001710 }
1711 }
1712
1713 return isLost;
1714}
1715
1716bool Renderer11::testDeviceResettable()
1717{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001718 // determine if the device is resettable by creating a dummy device
1719 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001720
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001721 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001722 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001723 return false;
1724 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001725
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001726 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001727 {
1728 D3D_FEATURE_LEVEL_11_0,
1729 D3D_FEATURE_LEVEL_10_1,
1730 D3D_FEATURE_LEVEL_10_0,
1731 };
1732
1733 ID3D11Device* dummyDevice;
1734 D3D_FEATURE_LEVEL dummyFeatureLevel;
1735 ID3D11DeviceContext* dummyContext;
1736
1737 HRESULT result = D3D11CreateDevice(NULL,
1738 D3D_DRIVER_TYPE_HARDWARE,
1739 NULL,
1740 #if defined(_DEBUG)
1741 D3D11_CREATE_DEVICE_DEBUG,
1742 #else
1743 0,
1744 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001745 featureLevels,
1746 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001747 D3D11_SDK_VERSION,
1748 &dummyDevice,
1749 &dummyFeatureLevel,
1750 &dummyContext);
1751
1752 if (!mDevice || FAILED(result))
1753 {
1754 return false;
1755 }
1756
Geoff Langea228632013-07-30 15:17:12 -04001757 SafeRelease(dummyContext);
1758 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001759
1760 return true;
1761}
1762
1763void Renderer11::release()
1764{
1765 releaseDeviceResources();
1766
Geoff Langea228632013-07-30 15:17:12 -04001767 SafeRelease(mDxgiFactory);
1768 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001769
1770 if (mDeviceContext)
1771 {
1772 mDeviceContext->ClearState();
1773 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001774 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001775 }
1776
Geoff Langea228632013-07-30 15:17:12 -04001777 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001778
1779 if (mD3d11Module)
1780 {
1781 FreeLibrary(mD3d11Module);
1782 mD3d11Module = NULL;
1783 }
1784
1785 if (mDxgiModule)
1786 {
1787 FreeLibrary(mDxgiModule);
1788 mDxgiModule = NULL;
1789 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001790}
1791
1792bool Renderer11::resetDevice()
1793{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001794 // recreate everything
1795 release();
1796 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001797
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001798 if (result != EGL_SUCCESS)
1799 {
1800 ERR("Could not reinitialize D3D11 device: %08X", result);
1801 return false;
1802 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001803
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001804 mDeviceLost = false;
1805
1806 return true;
1807}
1808
1809DWORD Renderer11::getAdapterVendor() const
1810{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001811 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001812}
1813
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001814std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001815{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001816 std::ostringstream rendererString;
1817
1818 rendererString << mDescription;
1819 rendererString << " Direct3D11";
1820
1821 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1822 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1823
1824 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001825}
1826
1827GUID Renderer11::getAdapterIdentifier() const
1828{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001829 // Use the adapter LUID as our adapter ID
1830 // This number is local to a machine is only guaranteed to be unique between restarts
1831 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1832 GUID adapterId = {0};
1833 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1834 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001835}
1836
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001837bool Renderer11::getBGRATextureSupport() const
1838{
1839 return mBGRATextureSupport;
1840}
1841
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001842bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001843{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001844 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001845}
1846
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001847bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001848{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001849 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001850}
1851
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001852bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001853{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001854 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001855}
1856
1857bool Renderer11::getDepthTextureSupport() const
1858{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001859 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001860}
1861
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001862bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001863{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001864 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001865}
1866
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001867bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001868{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001869 return mFloat32FilterSupport;
1870}
1871
1872bool Renderer11::getFloat32TextureRenderingSupport() const
1873{
1874 return mFloat32RenderSupport;
1875}
1876
1877bool Renderer11::getFloat16TextureSupport() const
1878{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001879 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001880}
1881
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001882bool Renderer11::getFloat16TextureFilteringSupport() const
1883{
1884 return mFloat16FilterSupport;
1885}
1886
1887bool Renderer11::getFloat16TextureRenderingSupport() const
1888{
1889 return mFloat16RenderSupport;
1890}
1891
Geoff Langd42cf4e2013-06-05 16:09:17 -04001892bool Renderer11::getRGB565TextureSupport() const
1893{
1894 return false;
1895}
1896
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001897bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001898{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001899 return false;
1900}
1901
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001902bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001903{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001904 return false;
1905}
1906
Geoff Lang632192d2013-10-04 13:40:46 -04001907bool Renderer11::getRGTextureSupport() const
1908{
1909 return mRGTextureSupport;
1910}
1911
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001912bool Renderer11::getTextureFilterAnisotropySupport() const
1913{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001914 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001915}
1916
1917float Renderer11::getTextureMaxAnisotropy() const
1918{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001919 switch (mFeatureLevel)
1920 {
1921 case D3D_FEATURE_LEVEL_11_0:
1922 return D3D11_MAX_MAXANISOTROPY;
1923 case D3D_FEATURE_LEVEL_10_1:
1924 case D3D_FEATURE_LEVEL_10_0:
1925 return D3D10_MAX_MAXANISOTROPY;
1926 default: UNREACHABLE();
1927 return 0;
1928 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001929}
1930
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001931bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001932{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00001933 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001934}
1935
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001936Range Renderer11::getViewportBounds() const
1937{
1938 switch (mFeatureLevel)
1939 {
1940 case D3D_FEATURE_LEVEL_11_0:
1941 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1942 case D3D_FEATURE_LEVEL_10_1:
1943 case D3D_FEATURE_LEVEL_10_0:
1944 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1945 default: UNREACHABLE();
1946 return Range(0, 0);
1947 }
1948}
1949
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001950unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001951{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001952 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1953 switch (mFeatureLevel)
1954 {
1955 case D3D_FEATURE_LEVEL_11_0:
1956 case D3D_FEATURE_LEVEL_10_1:
1957 case D3D_FEATURE_LEVEL_10_0:
1958 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1959 default: UNREACHABLE();
1960 return 0;
1961 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001962}
1963
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001964unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1965{
1966 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1967}
1968
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001969unsigned int Renderer11::getReservedVertexUniformVectors() const
1970{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001971 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001972}
1973
1974unsigned int Renderer11::getReservedFragmentUniformVectors() const
1975{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001976 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001977}
1978
1979unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001980{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001981 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1982 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1983 return MAX_VERTEX_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::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001987{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001988 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1989 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1990 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001991}
1992
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001993unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001994{
1995 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001996 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1997 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001998 switch (mFeatureLevel)
1999 {
2000 case D3D_FEATURE_LEVEL_11_0:
2001 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2002 case D3D_FEATURE_LEVEL_10_1:
2003 case D3D_FEATURE_LEVEL_10_0:
2004 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2005 default: UNREACHABLE();
2006 return 0;
2007 }
2008}
2009
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002010unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2011{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002012 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2013 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2014
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002015 switch (mFeatureLevel)
2016 {
2017 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002018 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002019 case D3D_FEATURE_LEVEL_10_1:
2020 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002021 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002022 default: UNREACHABLE();
2023 return 0;
2024 }
2025}
2026
2027unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2028{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002029 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2030 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2031
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002032 switch (mFeatureLevel)
2033 {
2034 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002035 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002036 case D3D_FEATURE_LEVEL_10_1:
2037 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002038 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002039 default: UNREACHABLE();
2040 return 0;
2041 }
2042}
2043
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002044unsigned int Renderer11::getReservedVertexUniformBuffers() const
2045{
2046 // we reserve one buffer for the application uniforms, and one for driver uniforms
2047 return 2;
2048}
2049
2050unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2051{
2052 // we reserve one buffer for the application uniforms, and one for driver uniforms
2053 return 2;
2054}
2055
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002056unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2057{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002058 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2059 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2060
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002061 switch (mFeatureLevel)
2062 {
2063 case D3D_FEATURE_LEVEL_11_0:
2064 return D3D11_SO_BUFFER_SLOT_COUNT;
2065 case D3D_FEATURE_LEVEL_10_1:
2066 case D3D_FEATURE_LEVEL_10_0:
2067 return D3D10_SO_BUFFER_SLOT_COUNT;
2068 default: UNREACHABLE();
2069 return 0;
2070 }
2071}
2072
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002073unsigned int Renderer11::getMaxUniformBufferSize() const
2074{
2075 // Each component is a 4-element vector of 4-byte units (floats)
2076 const unsigned int bytesPerComponent = 4 * sizeof(float);
2077
2078 switch (mFeatureLevel)
2079 {
2080 case D3D_FEATURE_LEVEL_11_0:
2081 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2082 case D3D_FEATURE_LEVEL_10_1:
2083 case D3D_FEATURE_LEVEL_10_0:
2084 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2085 default: UNREACHABLE();
2086 return 0;
2087 }
2088}
2089
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002090bool Renderer11::getNonPower2TextureSupport() const
2091{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002092 switch (mFeatureLevel)
2093 {
2094 case D3D_FEATURE_LEVEL_11_0:
2095 case D3D_FEATURE_LEVEL_10_1:
2096 case D3D_FEATURE_LEVEL_10_0:
2097 return true;
2098 default: UNREACHABLE();
2099 return false;
2100 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002101}
2102
2103bool Renderer11::getOcclusionQuerySupport() const
2104{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002105 switch (mFeatureLevel)
2106 {
2107 case D3D_FEATURE_LEVEL_11_0:
2108 case D3D_FEATURE_LEVEL_10_1:
2109 case D3D_FEATURE_LEVEL_10_0:
2110 return true;
2111 default: UNREACHABLE();
2112 return false;
2113 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002114}
2115
2116bool Renderer11::getInstancingSupport() const
2117{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002118 switch (mFeatureLevel)
2119 {
2120 case D3D_FEATURE_LEVEL_11_0:
2121 case D3D_FEATURE_LEVEL_10_1:
2122 case D3D_FEATURE_LEVEL_10_0:
2123 return true;
2124 default: UNREACHABLE();
2125 return false;
2126 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002127}
2128
2129bool Renderer11::getShareHandleSupport() const
2130{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002131 // We only currently support share handles with BGRA surfaces, because
2132 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002133 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002134 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002135}
2136
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002137bool Renderer11::getDerivativeInstructionSupport() const
2138{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002139 switch (mFeatureLevel)
2140 {
2141 case D3D_FEATURE_LEVEL_11_0:
2142 case D3D_FEATURE_LEVEL_10_1:
2143 case D3D_FEATURE_LEVEL_10_0:
2144 return true;
2145 default: UNREACHABLE();
2146 return false;
2147 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002148}
2149
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002150bool Renderer11::getPostSubBufferSupport() const
2151{
2152 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2153 return false;
2154}
2155
Jamie Madill13a2f852013-12-11 16:35:08 -05002156int Renderer11::getMaxRecommendedElementsIndices() const
2157{
2158 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2159 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2160
2161 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2162 return std::numeric_limits<GLint>::max();
2163}
2164
2165int Renderer11::getMaxRecommendedElementsVertices() const
2166{
2167 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2168 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2169
2170 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2171 return std::numeric_limits<GLint>::max();
2172}
2173
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002174int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002175{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002176 switch (mFeatureLevel)
2177 {
2178 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002179 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002180 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2181 default: UNREACHABLE(); return 0;
2182 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002183}
2184
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002185int Renderer11::getMinorShaderModel() const
2186{
2187 switch (mFeatureLevel)
2188 {
2189 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2190 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2191 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2192 default: UNREACHABLE(); return 0;
2193 }
2194}
2195
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002196float Renderer11::getMaxPointSize() const
2197{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002198 // choose a reasonable maximum. we enforce this in the shader.
2199 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2200 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002201}
2202
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002203int Renderer11::getMaxViewportDimension() const
2204{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002205 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2206 // In our case return the maximum texture size, which is the maximum render buffer size.
2207 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2208 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2209
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002210 switch (mFeatureLevel)
2211 {
2212 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002213 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002214 case D3D_FEATURE_LEVEL_10_1:
2215 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002216 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002217 default: UNREACHABLE();
2218 return 0;
2219 }
2220}
2221
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002222int Renderer11::getMaxTextureWidth() const
2223{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002224 switch (mFeatureLevel)
2225 {
2226 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2227 case D3D_FEATURE_LEVEL_10_1:
2228 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2229 default: UNREACHABLE(); return 0;
2230 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002231}
2232
2233int Renderer11::getMaxTextureHeight() const
2234{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002235 switch (mFeatureLevel)
2236 {
2237 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2238 case D3D_FEATURE_LEVEL_10_1:
2239 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2240 default: UNREACHABLE(); return 0;
2241 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002242}
2243
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002244int Renderer11::getMaxTextureDepth() const
2245{
2246 switch (mFeatureLevel)
2247 {
2248 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2249 case D3D_FEATURE_LEVEL_10_1:
2250 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2251 default: UNREACHABLE(); return 0;
2252 }
2253}
2254
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002255int Renderer11::getMaxTextureArrayLayers() const
2256{
2257 switch (mFeatureLevel)
2258 {
2259 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2260 case D3D_FEATURE_LEVEL_10_1:
2261 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2262 default: UNREACHABLE(); return 0;
2263 }
2264}
2265
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002266bool Renderer11::get32BitIndexSupport() const
2267{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002268 switch (mFeatureLevel)
2269 {
2270 case D3D_FEATURE_LEVEL_11_0:
2271 case D3D_FEATURE_LEVEL_10_1:
2272 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2273 default: UNREACHABLE(); return false;
2274 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002275}
2276
2277int Renderer11::getMinSwapInterval() const
2278{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002279 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002280}
2281
2282int Renderer11::getMaxSwapInterval() const
2283{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002284 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002285}
2286
2287int Renderer11::getMaxSupportedSamples() const
2288{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002289 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002290}
2291
Geoff Lang005df412013-10-16 14:12:50 -04002292GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002293{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002294 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002295 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2296 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2297}
2298
Geoff Lang005df412013-10-16 14:12:50 -04002299GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002300{
2301 unsigned int numCounts = 0;
2302
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)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002306 {
2307 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2308 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2309
2310 if (iter != mMultisampleSupportMap.end())
2311 {
2312 const MultisampleSupportInfo& info = iter->second;
2313 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2314 {
2315 if (info.qualityLevels[i] > 0)
2316 {
2317 numCounts++;
2318 }
2319 }
2320 }
2321 }
2322
2323 return numCounts;
2324}
2325
Geoff Lang005df412013-10-16 14:12:50 -04002326void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002327{
2328 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002329 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2330 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2331 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002332 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002333 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002334
2335 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2336 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2337
2338 if (iter != mMultisampleSupportMap.end())
2339 {
2340 const MultisampleSupportInfo& info = iter->second;
2341 int bufPos = 0;
2342 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2343 {
2344 if (info.qualityLevels[i] > 0)
2345 {
2346 params[bufPos++] = i + 1;
2347 }
2348 }
2349 }
2350}
2351
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002352int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2353{
2354 if (requested == 0)
2355 {
2356 return 0;
2357 }
2358
2359 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2360 if (iter != mMultisampleSupportMap.end())
2361 {
2362 const MultisampleSupportInfo& info = iter->second;
2363 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2364 {
2365 if (info.qualityLevels[i] > 0)
2366 {
2367 return i + 1;
2368 }
2369 }
2370 }
2371
2372 return -1;
2373}
2374
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002375unsigned int Renderer11::getMaxRenderTargets() const
2376{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002377 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2378 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2379
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002380 switch (mFeatureLevel)
2381 {
2382 case D3D_FEATURE_LEVEL_11_0:
2383 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2384 case D3D_FEATURE_LEVEL_10_1:
2385 case D3D_FEATURE_LEVEL_10_0:
2386 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2387 default:
2388 UNREACHABLE();
2389 return 1;
2390 }
2391}
2392
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002393bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002394{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002395 if (source && dest)
2396 {
2397 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2398 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2399
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002400 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002401
2402 dest11->invalidateSwizzleCache();
2403
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002404 return true;
2405 }
2406
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002407 return false;
2408}
2409
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002410bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002411{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002412 if (source && dest)
2413 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002414 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2415 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002416
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002417 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002418
2419 dest11->invalidateSwizzleCache();
2420
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002421 return true;
2422 }
2423
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002424 return false;
2425}
2426
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002427bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2428{
2429 if (source && dest)
2430 {
2431 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2432 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2433
2434 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002435
2436 dest11->invalidateSwizzleCache();
2437
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002438 return true;
2439 }
2440
2441 return false;
2442}
2443
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002444bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2445{
2446 if (source && dest)
2447 {
2448 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2449 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2450
2451 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002452
2453 dest11->invalidateSwizzleCache();
2454
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002455 return true;
2456 }
2457
2458 return false;
2459}
2460
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002461bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002462 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002463{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002464 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002465 if (!colorbuffer)
2466 {
2467 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002468 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002469 }
2470
2471 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2472 if (!sourceRenderTarget)
2473 {
2474 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002475 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002476 }
2477
2478 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2479 if (!source)
2480 {
2481 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002482 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002483 }
2484
2485 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2486 if (!storage11)
2487 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002488 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002489 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002490 }
2491
2492 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2493 if (!destRenderTarget)
2494 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002495 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002496 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002497 }
2498
2499 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2500 if (!dest)
2501 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002502 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002503 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002504 }
2505
Geoff Langb86b9792013-06-04 16:32:05 -04002506 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2507 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002508
Geoff Langb86b9792013-06-04 16:32:05 -04002509 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2510 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002511
Geoff Langb86b9792013-06-04 16:32:05 -04002512 // Use nearest filtering because source and destination are the same size for the direct
2513 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002514 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002515 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002516
Geoff Lang42477a42013-09-17 17:07:02 -04002517 storage11->invalidateSwizzleCacheLevel(level);
2518
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002519 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002520}
2521
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002522bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002523 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002524{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002525 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002526 if (!colorbuffer)
2527 {
2528 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002529 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002530 }
2531
2532 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2533 if (!sourceRenderTarget)
2534 {
2535 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002536 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002537 }
2538
2539 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2540 if (!source)
2541 {
2542 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002543 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002544 }
2545
2546 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2547 if (!storage11)
2548 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002549 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002550 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002551 }
2552
Nicolas Capensb13f8662013-06-04 13:30:19 -04002553 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002554 if (!destRenderTarget)
2555 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002556 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002557 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002558 }
2559
2560 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2561 if (!dest)
2562 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002563 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002564 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002565 }
2566
Geoff Langb86b9792013-06-04 16:32:05 -04002567 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2568 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002569
Geoff Langb86b9792013-06-04 16:32:05 -04002570 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2571 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002572
Geoff Langb86b9792013-06-04 16:32:05 -04002573 // Use nearest filtering because source and destination are the same size for the direct
2574 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002575 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002576 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002577
Geoff Lang42477a42013-09-17 17:07:02 -04002578 storage11->invalidateSwizzleCacheLevel(level);
2579
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002580 return ret;
2581}
2582
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002583bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2584 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2585{
2586 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2587 if (!colorbuffer)
2588 {
2589 ERR("Failed to retrieve the color buffer from the frame buffer.");
2590 return gl::error(GL_OUT_OF_MEMORY, false);
2591 }
2592
2593 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2594 if (!sourceRenderTarget)
2595 {
2596 ERR("Failed to retrieve the render target from the frame buffer.");
2597 return gl::error(GL_OUT_OF_MEMORY, false);
2598 }
2599
2600 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2601 if (!source)
2602 {
2603 ERR("Failed to retrieve the render target view from the render target.");
2604 return gl::error(GL_OUT_OF_MEMORY, false);
2605 }
2606
2607 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2608 if (!storage11)
2609 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002610 ERR("Failed to retrieve the texture storage from the destination.");
2611 return gl::error(GL_OUT_OF_MEMORY, false);
2612 }
2613
2614 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2615 if (!destRenderTarget)
2616 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002617 ERR("Failed to retrieve the render target from the destination storage.");
2618 return gl::error(GL_OUT_OF_MEMORY, false);
2619 }
2620
2621 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2622 if (!dest)
2623 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002624 ERR("Failed to retrieve the render target view from the destination render target.");
2625 return gl::error(GL_OUT_OF_MEMORY, false);
2626 }
2627
Geoff Langb86b9792013-06-04 16:32:05 -04002628 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2629 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002630
Geoff Langb86b9792013-06-04 16:32:05 -04002631 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2632 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002633
Geoff Langb86b9792013-06-04 16:32:05 -04002634 // Use nearest filtering because source and destination are the same size for the direct
2635 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002636 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002637 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002638
Geoff Lang42477a42013-09-17 17:07:02 -04002639 storage11->invalidateSwizzleCacheLevel(level);
2640
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002641 return ret;
2642}
2643
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002644bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2645 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2646{
2647 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2648 if (!colorbuffer)
2649 {
2650 ERR("Failed to retrieve the color buffer from the frame buffer.");
2651 return gl::error(GL_OUT_OF_MEMORY, false);
2652 }
2653
2654 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2655 if (!sourceRenderTarget)
2656 {
2657 ERR("Failed to retrieve the render target from the frame buffer.");
2658 return gl::error(GL_OUT_OF_MEMORY, false);
2659 }
2660
2661 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2662 if (!source)
2663 {
2664 ERR("Failed to retrieve the render target view from the render target.");
2665 return gl::error(GL_OUT_OF_MEMORY, false);
2666 }
2667
2668 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2669 if (!storage11)
2670 {
Geoff Langea228632013-07-30 15:17:12 -04002671 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002672 ERR("Failed to retrieve the texture storage from the destination.");
2673 return gl::error(GL_OUT_OF_MEMORY, false);
2674 }
2675
2676 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2677 if (!destRenderTarget)
2678 {
Geoff Langea228632013-07-30 15:17:12 -04002679 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002680 ERR("Failed to retrieve the render target from the destination storage.");
2681 return gl::error(GL_OUT_OF_MEMORY, false);
2682 }
2683
2684 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2685 if (!dest)
2686 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002687 ERR("Failed to retrieve the render target view from the destination render target.");
2688 return gl::error(GL_OUT_OF_MEMORY, false);
2689 }
2690
Geoff Langb86b9792013-06-04 16:32:05 -04002691 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2692 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002693
Geoff Langb86b9792013-06-04 16:32:05 -04002694 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2695 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002696
Geoff Langb86b9792013-06-04 16:32:05 -04002697 // Use nearest filtering because source and destination are the same size for the direct
2698 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002699 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002700 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002701
Geoff Lang42477a42013-09-17 17:07:02 -04002702 storage11->invalidateSwizzleCacheLevel(level);
2703
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002704 return ret;
2705}
2706
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002707void Renderer11::unapplyRenderTargets()
2708{
2709 setOneTimeRenderTarget(NULL);
2710}
2711
2712void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2713{
2714 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2715
2716 rtvArray[0] = renderTargetView;
2717
2718 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2719
2720 // Do not preserve the serial for this one-time-use render target
2721 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2722 {
2723 mAppliedRenderTargetSerials[rtIndex] = 0;
2724 }
2725}
2726
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002727RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2728{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002729 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002730 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002731
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002732 if (depth)
2733 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002734 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002735 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002736 swapChain11->getDepthStencilTexture(),
2737 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002738 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002739 }
2740 else
2741 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002742 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002743 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002744 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002745 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002746 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002747 }
2748 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002749}
2750
Geoff Langa2d97f12013-06-11 11:44:02 -04002751RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002752{
Geoff Langa2d97f12013-06-11 11:44:02 -04002753 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002754 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002755}
2756
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002757ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002758{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002759 ShaderExecutable11 *executable = NULL;
2760
2761 switch (type)
2762 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002763 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002764 {
2765 ID3D11VertexShader *vshader = NULL;
2766 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2767 ASSERT(SUCCEEDED(result));
2768
2769 if (vshader)
2770 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002771 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002772 }
2773 }
2774 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002775 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002776 {
2777 ID3D11PixelShader *pshader = NULL;
2778 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2779 ASSERT(SUCCEEDED(result));
2780
2781 if (pshader)
2782 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002783 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002784 }
2785 }
2786 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002787 case rx::SHADER_GEOMETRY:
2788 {
2789 ID3D11GeometryShader *gshader = NULL;
2790 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2791 ASSERT(SUCCEEDED(result));
2792
2793 if (gshader)
2794 {
2795 executable = new ShaderExecutable11(function, length, gshader);
2796 }
2797 }
2798 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002799 default:
2800 UNREACHABLE();
2801 break;
2802 }
2803
2804 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002805}
2806
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002807ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002808{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002809 const char *profile = NULL;
2810
2811 switch (type)
2812 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002813 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002814 profile = "vs_4_0";
2815 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002816 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002817 profile = "ps_4_0";
2818 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002819 case rx::SHADER_GEOMETRY:
2820 profile = "gs_4_0";
2821 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002822 default:
2823 UNREACHABLE();
2824 return NULL;
2825 }
2826
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00002827 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002828 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002829 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002830 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002831 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002832
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002833 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002834 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002835
2836 return executable;
2837}
2838
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002839VertexBuffer *Renderer11::createVertexBuffer()
2840{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002841 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002842}
2843
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002844IndexBuffer *Renderer11::createIndexBuffer()
2845{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002846 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002847}
2848
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002849BufferStorage *Renderer11::createBufferStorage()
2850{
2851 return new BufferStorage11(this);
2852}
2853
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002854QueryImpl *Renderer11::createQuery(GLenum type)
2855{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002856 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002857}
2858
2859FenceImpl *Renderer11::createFence()
2860{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002861 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002862}
2863
Geoff Lang005df412013-10-16 14:12:50 -04002864bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002865{
Jamie Madill4461f092013-10-10 15:10:39 -04002866 int clientVersion = getCurrentClientVersion();
2867
2868 // We only support buffer to texture copies in ES3
2869 if (clientVersion <= 2)
2870 {
2871 return false;
2872 }
2873
2874 // sRGB formats do not work with D3D11 buffer SRVs
2875 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2876 {
2877 return false;
2878 }
2879
2880 // We cannot support direct copies to non-color-renderable formats
2881 if (!gl::IsColorRenderingSupported(internalFormat, this))
2882 {
2883 return false;
2884 }
2885
2886 // We skip all 3-channel formats since sometimes format support is missing
2887 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2888 {
2889 return false;
2890 }
2891
2892 // We don't support formats which we can't represent without conversion
2893 if (getNativeTextureFormat(internalFormat) != internalFormat)
2894 {
2895 return false;
2896 }
2897
2898 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002899}
2900
Jamie Madilla21eea32013-09-18 14:36:25 -04002901bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2902 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2903{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002904 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002905 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2906}
2907
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002908bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002909{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002910 ASSERT(colorbuffer != NULL);
2911
2912 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2913 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002914 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002915 *subresourceIndex = renderTarget->getSubresourceIndex();
2916
2917 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2918 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002919 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002920 ID3D11Resource *textureResource = NULL;
2921 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002922
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002923 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002924 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002925 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002926 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002927
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002928 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002929 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002930 return true;
2931 }
2932 else
2933 {
2934 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2935 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002936 }
2937 }
2938 }
2939 }
2940
2941 return false;
2942}
2943
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002944bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002945 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002946{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002947 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002948 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002949 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002950
2951 if (!readBuffer)
2952 {
2953 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2954 return gl::error(GL_OUT_OF_MEMORY, false);
2955 }
2956
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002957 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002958
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002959 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002960 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002961 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2962 {
2963 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
2964
2965 if (!drawBuffer)
2966 {
2967 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2968 return gl::error(GL_OUT_OF_MEMORY, false);
2969 }
2970
2971 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2972
Geoff Lang125deab2013-08-09 13:34:16 -04002973 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002974 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002975 {
2976 return false;
2977 }
2978 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002979 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002980 }
2981
Geoff Lang685806d2013-06-12 11:16:36 -04002982 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002983 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002984 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
2985 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
2986
2987 if (!readBuffer)
2988 {
2989 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2990 return gl::error(GL_OUT_OF_MEMORY, false);
2991 }
2992
2993 if (!drawBuffer)
2994 {
2995 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2996 return gl::error(GL_OUT_OF_MEMORY, false);
2997 }
2998
2999 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3000 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3001
Geoff Lang125deab2013-08-09 13:34:16 -04003002 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04003003 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003004 {
3005 return false;
3006 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003007 }
3008
Geoff Lang42477a42013-09-17 17:07:02 -04003009 invalidateFramebufferSwizzles(drawTarget);
3010
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003011 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003012}
3013
3014void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3015 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3016{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003017 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003018 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003019
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003020 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3021
3022 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003023 {
3024 gl::Rectangle area;
3025 area.x = x;
3026 area.y = y;
3027 area.width = width;
3028 area.height = height;
3029
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003030 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3031 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003032
Geoff Langea228632013-07-30 15:17:12 -04003033 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003034 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003035}
3036
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003037Image *Renderer11::createImage()
3038{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003039 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003040}
3041
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003042void Renderer11::generateMipmap(Image *dest, Image *src)
3043{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003044 Image11 *dest11 = Image11::makeImage11(dest);
3045 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003046 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003047}
3048
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003049TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3050{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003051 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3052 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003053}
3054
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003055TextureStorage *Renderer11::createTextureStorage2D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003056{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003057 return new TextureStorage11_2D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003058}
3059
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003060TextureStorage *Renderer11::createTextureStorageCube(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, int size)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003061{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003062 return new TextureStorage11_Cube(this, baseLevel, maxLevel, internalformat, renderTarget, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003063}
3064
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003065TextureStorage *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 +00003066{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003067 return new TextureStorage11_3D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003068}
3069
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003070TextureStorage *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 +00003071{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003072 return new TextureStorage11_2DArray(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003073}
3074
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003075void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3076 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3077 GLint packAlignment, void *pixels)
3078{
3079 D3D11_TEXTURE2D_DESC textureDesc;
3080 texture->GetDesc(&textureDesc);
3081
3082 D3D11_TEXTURE2D_DESC stagingDesc;
3083 stagingDesc.Width = area.width;
3084 stagingDesc.Height = area.height;
3085 stagingDesc.MipLevels = 1;
3086 stagingDesc.ArraySize = 1;
3087 stagingDesc.Format = textureDesc.Format;
3088 stagingDesc.SampleDesc.Count = 1;
3089 stagingDesc.SampleDesc.Quality = 0;
3090 stagingDesc.Usage = D3D11_USAGE_STAGING;
3091 stagingDesc.BindFlags = 0;
3092 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3093 stagingDesc.MiscFlags = 0;
3094
3095 ID3D11Texture2D* stagingTex = NULL;
3096 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3097 if (FAILED(result))
3098 {
3099 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3100 return;
3101 }
3102
3103 ID3D11Texture2D* srcTex = NULL;
3104 if (textureDesc.SampleDesc.Count > 1)
3105 {
3106 D3D11_TEXTURE2D_DESC resolveDesc;
3107 resolveDesc.Width = textureDesc.Width;
3108 resolveDesc.Height = textureDesc.Height;
3109 resolveDesc.MipLevels = 1;
3110 resolveDesc.ArraySize = 1;
3111 resolveDesc.Format = textureDesc.Format;
3112 resolveDesc.SampleDesc.Count = 1;
3113 resolveDesc.SampleDesc.Quality = 0;
3114 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3115 resolveDesc.BindFlags = 0;
3116 resolveDesc.CPUAccessFlags = 0;
3117 resolveDesc.MiscFlags = 0;
3118
3119 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3120 if (FAILED(result))
3121 {
3122 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003123 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003124 return;
3125 }
3126
3127 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3128 subResource = 0;
3129 }
3130 else
3131 {
3132 srcTex = texture;
3133 srcTex->AddRef();
3134 }
3135
3136 D3D11_BOX srcBox;
3137 srcBox.left = area.x;
3138 srcBox.right = area.x + area.width;
3139 srcBox.top = area.y;
3140 srcBox.bottom = area.y + area.height;
3141 srcBox.front = 0;
3142 srcBox.back = 1;
3143
3144 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3145
Geoff Langea228632013-07-30 15:17:12 -04003146 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003147
3148 D3D11_MAPPED_SUBRESOURCE mapping;
3149 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3150
3151 unsigned char *source;
3152 int inputPitch;
3153 if (packReverseRowOrder)
3154 {
3155 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3156 inputPitch = -static_cast<int>(mapping.RowPitch);
3157 }
3158 else
3159 {
3160 source = static_cast<unsigned char*>(mapping.pData);
3161 inputPitch = static_cast<int>(mapping.RowPitch);
3162 }
3163
Geoff Lang697ad3e2013-06-04 10:11:28 -04003164 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003165
Geoff Lang005df412013-10-16 14:12:50 -04003166 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003167 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3168 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3169
3170 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3171
3172 if (sourceFormat == format && sourceType == type)
3173 {
3174 // Direct copy possible
3175 unsigned char *dest = static_cast<unsigned char*>(pixels);
3176 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003177 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003178 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003179 }
3180 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003181 else
3182 {
Geoff Lang005df412013-10-16 14:12:50 -04003183 GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003184 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3185
3186 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type, getCurrentClientVersion());
3187 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003188 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003189 // Fast copy is possible through some special function
3190 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003191 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003192 for (int x = 0; x < area.width; x++)
3193 {
3194 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3195 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3196
3197 fastCopyFunc(src, dest);
3198 }
3199 }
3200 }
3201 else
3202 {
Jamie Madilld6cb2442013-07-10 15:13:38 -04003203 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003204 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3205
3206 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3207 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3208 sizeof(temp) >= sizeof(gl::ColorUI) &&
3209 sizeof(temp) >= sizeof(gl::ColorI));
3210
3211 for (int y = 0; y < area.height; y++)
3212 {
3213 for (int x = 0; x < area.width; x++)
3214 {
3215 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3216 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3217
3218 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3219 // will not allow the copy otherwise.
3220 readFunc(src, temp);
3221 writeFunc(temp, dest);
3222 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003223 }
3224 }
3225 }
3226
3227 mDeviceContext->Unmap(stagingTex, 0);
3228
Geoff Langea228632013-07-30 15:17:12 -04003229 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003230}
3231
Geoff Lang758d5b22013-06-11 11:42:50 -04003232bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003233 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3234 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003235{
Geoff Lang975af372013-06-12 11:19:22 -04003236 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3237 // it should never be the case that both color and depth/stencil need to be blitted at
3238 // at the same time.
3239 ASSERT(colorBlit != (depthBlit || stencilBlit));
3240
Geoff Langc1f51be2013-06-11 11:49:14 -04003241 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003242
Geoff Lang4d782732013-07-22 10:44:18 -04003243 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3244 if (!drawRenderTarget)
3245 {
3246 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3247 return gl::error(GL_OUT_OF_MEMORY, false);
3248 }
3249
3250 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3251 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3252 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3253 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3254
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003255 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3256 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003257 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003258 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003259 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003260 }
3261
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003262 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003263 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003264 unsigned int readSubresource = 0;
3265 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003266 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003267 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3268 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003269
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003270 if (unresolvedTexture)
3271 {
3272 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3273 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003274
Geoff Langea228632013-07-30 15:17:12 -04003275 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003276
3277 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3278 if (FAILED(result))
3279 {
Geoff Langea228632013-07-30 15:17:12 -04003280 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003281 return gl::error(GL_OUT_OF_MEMORY, false);
3282 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003283 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003284 }
3285 else
3286 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003287 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003288 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003289 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003290 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003291 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003292 }
3293
Geoff Lang4d782732013-07-22 10:44:18 -04003294 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003295 {
Geoff Lang4d782732013-07-22 10:44:18 -04003296 SafeRelease(readTexture);
3297 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003298 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003299 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003300 }
3301
Geoff Lang125deab2013-08-09 13:34:16 -04003302 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3303 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3304
3305 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3306
3307 bool wholeBufferCopy = !scissorNeeded &&
3308 readRect.x == 0 && readRect.width == readSize.width &&
3309 readRect.y == 0 && readRect.height == readSize.height &&
3310 drawRect.x == 0 && drawRect.width == drawSize.width &&
3311 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003312
Geoff Langc1f51be2013-06-11 11:49:14 -04003313 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003314
Geoff Lang125deab2013-08-09 13:34:16 -04003315 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || readRect.height < 0;
3316
3317 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3318 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3319 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3320 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3321
3322 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3323 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3324 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3325
Geoff Langc1f51be2013-06-11 11:49:14 -04003326 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003327 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3328 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003329 {
Geoff Lang125deab2013-08-09 13:34:16 -04003330 UINT dstX = drawRect.x;
3331 UINT dstY = drawRect.y;
3332
Geoff Langc1f51be2013-06-11 11:49:14 -04003333 D3D11_BOX readBox;
3334 readBox.left = readRect.x;
3335 readBox.right = readRect.x + readRect.width;
3336 readBox.top = readRect.y;
3337 readBox.bottom = readRect.y + readRect.height;
3338 readBox.front = 0;
3339 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003340
Geoff Lang125deab2013-08-09 13:34:16 -04003341 if (scissorNeeded)
3342 {
3343 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3344 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3345
3346 if (drawRect.x < scissor->x)
3347 {
3348 dstX = scissor->x;
3349 readBox.left += (scissor->x - drawRect.x);
3350 }
3351 if (drawRect.y < scissor->y)
3352 {
3353 dstY = scissor->y;
3354 readBox.top += (scissor->y - drawRect.y);
3355 }
3356 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3357 {
3358 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3359 }
3360 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3361 {
3362 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3363 }
3364 }
3365
Geoff Langc1f51be2013-06-11 11:49:14 -04003366 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3367 // We also require complete framebuffer copies for depth-stencil blit.
3368 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003369
Geoff Lang125deab2013-08-09 13:34:16 -04003370 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003371 readTexture, readSubresource, pSrcBox);
3372 result = true;
3373 }
3374 else
3375 {
3376 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003377 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003378
Geoff Lang975af372013-06-12 11:19:22 -04003379 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003380 {
Geoff Lang975af372013-06-12 11:19:22 -04003381 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003382 drawTexture, drawSubresource, drawArea, drawSize,
3383 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003384 }
3385 else if (depthBlit)
3386 {
Geoff Lang125deab2013-08-09 13:34:16 -04003387 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3388 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003389 }
3390 else if (stencilBlit)
3391 {
3392 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003393 drawTexture, drawSubresource, drawArea, drawSize,
3394 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003395 }
3396 else
3397 {
Geoff Lang685806d2013-06-12 11:16:36 -04003398 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003399 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3400 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003401 }
3402 }
3403
3404 SafeRelease(readTexture);
3405 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003406
3407 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003408}
3409
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003410ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3411{
3412 D3D11_TEXTURE2D_DESC textureDesc;
3413 source->GetDesc(&textureDesc);
3414
3415 if (textureDesc.SampleDesc.Count > 1)
3416 {
3417 D3D11_TEXTURE2D_DESC resolveDesc;
3418 resolveDesc.Width = textureDesc.Width;
3419 resolveDesc.Height = textureDesc.Height;
3420 resolveDesc.MipLevels = 1;
3421 resolveDesc.ArraySize = 1;
3422 resolveDesc.Format = textureDesc.Format;
3423 resolveDesc.SampleDesc.Count = 1;
3424 resolveDesc.SampleDesc.Quality = 0;
3425 resolveDesc.Usage = textureDesc.Usage;
3426 resolveDesc.BindFlags = textureDesc.BindFlags;
3427 resolveDesc.CPUAccessFlags = 0;
3428 resolveDesc.MiscFlags = 0;
3429
3430 ID3D11Texture2D *resolveTexture = NULL;
3431 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3432 if (FAILED(result))
3433 {
3434 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3435 return NULL;
3436 }
3437
3438 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3439 return resolveTexture;
3440 }
3441 else
3442 {
3443 source->AddRef();
3444 return source;
3445 }
3446}
3447
Geoff Lang42477a42013-09-17 17:07:02 -04003448void Renderer11::invalidateRenderbufferSwizzles(gl::Renderbuffer *renderBuffer, int mipLevel)
3449{
3450 TextureStorage *texStorage = renderBuffer->getTextureStorage();
3451 if (texStorage)
3452 {
3453 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3454 if (!texStorage11)
3455 {
3456 ERR("texture storage pointer unexpectedly null.");
3457 return;
3458 }
3459
3460 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3461 }
3462}
3463
3464void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3465{
3466 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3467 {
3468 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
3469 if (colorbuffer)
3470 {
3471 invalidateRenderbufferSwizzles(colorbuffer, framebuffer->getColorbufferMipLevel(colorAttachment));
3472 }
3473 }
3474
3475 gl::Renderbuffer *depthBuffer = framebuffer->getDepthbuffer();
3476 if (depthBuffer)
3477 {
3478 invalidateRenderbufferSwizzles(depthBuffer, framebuffer->getDepthbufferMipLevel());
3479 }
3480
3481 gl::Renderbuffer *stencilBuffer = framebuffer->getStencilbuffer();
3482 if (stencilBuffer)
3483 {
3484 invalidateRenderbufferSwizzles(stencilBuffer, framebuffer->getStencilbufferMipLevel());
3485 }
3486}
3487
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003488bool Renderer11::getLUID(LUID *adapterLuid) const
3489{
3490 adapterLuid->HighPart = 0;
3491 adapterLuid->LowPart = 0;
3492
3493 if (!mDxgiAdapter)
3494 {
3495 return false;
3496 }
3497
3498 DXGI_ADAPTER_DESC adapterDesc;
3499 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3500 {
3501 return false;
3502 }
3503
3504 *adapterLuid = adapterDesc.AdapterLuid;
3505 return true;
3506}
3507
Geoff Lang005df412013-10-16 14:12:50 -04003508GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003509{
3510 int clientVersion = getCurrentClientVersion();
3511 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3512}
3513
Geoff Lang61e49a52013-05-29 10:22:58 -04003514Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3515{
3516 MultisampleSupportInfo supportInfo = { 0 };
3517
3518 UINT formatSupport;
3519 HRESULT result;
3520
3521 result = mDevice->CheckFormatSupport(format, &formatSupport);
3522 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3523 {
3524 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3525 {
3526 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3527 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3528 {
3529 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3530 }
3531 else
3532 {
3533 supportInfo.qualityLevels[i - 1] = 0;
3534 }
3535 }
3536 }
3537
3538 return supportInfo;
3539}
3540
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003541}