blob: 40b6ee74971accb92b9a11c83523f08ac18619e9 [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 unsigned int serial = 0;
598 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000599
600 if (texture)
601 {
602 TextureStorageInterface *texStorage = texture->getNativeTexture();
603 if (texStorage)
604 {
605 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
Geoff Lang644bbf22013-09-17 17:02:43 -0400606 textureSRV = storage11->getSRV(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
607 texture->getSwizzleAlpha());
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000608 }
609
610 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
611 // missing the shader resource view
612 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000613
614 serial = texture->getTextureSerial();
615 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000616 }
617
618 if (type == gl::SAMPLER_PIXEL)
619 {
620 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
621 {
622 ERR("Pixel shader sampler index %i is not valid.", index);
623 return;
624 }
625
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000626 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
627 {
628 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
629 }
630
631 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000632 }
633 else if (type == gl::SAMPLER_VERTEX)
634 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000635 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000636 {
637 ERR("Vertex shader sampler index %i is not valid.", index);
638 return;
639 }
640
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000641 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
642 {
643 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
644 }
645
646 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000647 }
648 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000649}
650
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000651bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
652{
653 // convert buffers to ID3D11Buffer*
654 ID3D11Buffer *vertexConstantBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = { NULL };
655 ID3D11Buffer *pixelConstantBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = { NULL };
656
657 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
658 {
659 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
660 if (uniformBuffer)
661 {
662 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Jamie Madill171ca0e2013-10-10 15:10:31 -0400663 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(true);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000664
665 if (!constantBuffer)
666 {
667 return false;
668 }
669
Geoff Langc6354ee2013-07-22 10:40:07 -0400670 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
671 {
672 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
673 1, &constantBuffer);
674 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
675 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000676 }
677 }
678
679 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
680 {
681 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
682 if (uniformBuffer)
683 {
684 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Jamie Madill171ca0e2013-10-10 15:10:31 -0400685 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(true);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000686
687 if (!constantBuffer)
688 {
689 return false;
690 }
691
Geoff Langc6354ee2013-07-22 10:40:07 -0400692 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
693 {
694 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
695 1, &constantBuffer);
696 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
697 }
698
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000699 pixelConstantBuffers[uniformBufferIndex] = constantBuffer;
700 }
701 }
702
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000703 return true;
704}
705
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000706void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000707{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000708 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000709 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000710 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
711 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000712 if (!dxRasterState)
713 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000714 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000715 "rasterizer state.");
716 }
717
718 mDeviceContext->RSSetState(dxRasterState);
719
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000720 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000721 }
722
723 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000724}
725
Geoff Lang2a64ee42013-05-31 11:22:40 -0400726void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000727 unsigned int sampleMask)
728{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000729 if (mForceSetBlendState ||
730 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400731 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000732 sampleMask != mCurSampleMask)
733 {
734 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
735 if (!dxBlendState)
736 {
737 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
738 "blend state.");
739 }
740
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000741 float blendColors[4] = {0.0f};
742 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
743 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
744 {
745 blendColors[0] = blendColor.red;
746 blendColors[1] = blendColor.green;
747 blendColors[2] = blendColor.blue;
748 blendColors[3] = blendColor.alpha;
749 }
750 else
751 {
752 blendColors[0] = blendColor.alpha;
753 blendColors[1] = blendColor.alpha;
754 blendColors[2] = blendColor.alpha;
755 blendColors[3] = blendColor.alpha;
756 }
757
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000758 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
759
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000760 mCurBlendState = blendState;
761 mCurBlendColor = blendColor;
762 mCurSampleMask = sampleMask;
763 }
764
765 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000766}
767
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000768void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000769 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000770{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000771 if (mForceSetDepthStencilState ||
772 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
773 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
774 {
775 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
776 stencilRef != stencilBackRef ||
777 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
778 {
779 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
780 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000781 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000782 }
783
784 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
785 if (!dxDepthStencilState)
786 {
787 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
788 "setting the default depth stencil state.");
789 }
790
791 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
792
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000793 mCurDepthStencilState = depthStencilState;
794 mCurStencilRef = stencilRef;
795 mCurStencilBackRef = stencilBackRef;
796 }
797
798 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000799}
800
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000801void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000802{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000803 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
804 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000805 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000806 if (enabled)
807 {
808 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000809 rect.left = std::max(0, scissor.x);
810 rect.top = std::max(0, scissor.y);
811 rect.right = scissor.x + std::max(0, scissor.width);
812 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000813
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000814 mDeviceContext->RSSetScissorRects(1, &rect);
815 }
816
817 if (enabled != mScissorEnabled)
818 {
819 mForceSetRasterState = true;
820 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000821
822 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000823 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000824 }
825
826 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000827}
828
daniel@transgaming.com12985182012-12-20 20:56:31 +0000829bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000830 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000831{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000832 gl::Rectangle actualViewport = viewport;
833 float actualZNear = gl::clamp01(zNear);
834 float actualZFar = gl::clamp01(zFar);
835 if (ignoreViewport)
836 {
837 actualViewport.x = 0;
838 actualViewport.y = 0;
839 actualViewport.width = mRenderTargetDesc.width;
840 actualViewport.height = mRenderTargetDesc.height;
841 actualZNear = 0.0f;
842 actualZFar = 1.0f;
843 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000844
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000845 // Get D3D viewport bounds, which depends on the feature level
846 const Range& viewportBounds = getViewportBounds();
847
848 // 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 +0000849 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000850 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
851 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
852 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
853 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
854 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
855 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000856 dxViewport.MinDepth = actualZNear;
857 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000858
859 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
860 {
861 return false; // Nothing to render
862 }
863
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000864 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
865 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000866
daniel@transgaming.com53670042012-11-28 20:55:51 +0000867 if (viewportChanged)
868 {
869 mDeviceContext->RSSetViewports(1, &dxViewport);
870
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000871 mCurViewport = actualViewport;
872 mCurNear = actualZNear;
873 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000874
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000875 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
876 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
877 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
878 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000879
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000880 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
881 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000882
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000883 mVertexConstants.depthRange[0] = actualZNear;
884 mVertexConstants.depthRange[1] = actualZFar;
885 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000886
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000887 mPixelConstants.depthRange[0] = actualZNear;
888 mPixelConstants.depthRange[1] = actualZFar;
889 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000890 }
891
892 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000893 return true;
894}
895
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000896bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
897{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000898 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000899
Geoff Lang57e713e2013-07-31 17:01:58 -0400900 GLsizei minCount = 0;
901
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000902 switch (mode)
903 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400904 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
905 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
906 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
907 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
908 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
909 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000910 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400911 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000912 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000913 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000914 }
915
Geoff Lang4c095862013-07-22 10:43:36 -0400916 if (primitiveTopology != mCurrentPrimitiveTopology)
917 {
918 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
919 mCurrentPrimitiveTopology = primitiveTopology;
920 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000921
Geoff Lang57e713e2013-07-31 17:01:58 -0400922 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000923}
924
925bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000926{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000927 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000928 // Also extract the render target dimensions and view
929 unsigned int renderTargetWidth = 0;
930 unsigned int renderTargetHeight = 0;
931 GLenum renderTargetFormat = 0;
932 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
933 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
934 bool missingColorRenderTarget = true;
935
936 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000937 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000938 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
939
940 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000941 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000942 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
943 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
944
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000945 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000946
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000947 if (!colorbuffer)
948 {
949 ERR("render target pointer unexpectedly null.");
950 return false;
951 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000952
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000953 // check for zero-sized default framebuffer, which is a special case.
954 // in this case we do not wish to modify any state and just silently return false.
955 // this will not report any gl error but will cause the calling method to return.
956 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
957 {
958 return false;
959 }
960
961 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
962
963 // Extract the render target dimensions and view
964 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
965 if (!renderTarget)
966 {
967 ERR("render target pointer unexpectedly null.");
968 return false;
969 }
970
971 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
972 if (!framebufferRTVs[colorAttachment])
973 {
974 ERR("render target view pointer unexpectedly null.");
975 return false;
976 }
977
978 if (missingColorRenderTarget)
979 {
980 renderTargetWidth = colorbuffer->getWidth();
981 renderTargetHeight = colorbuffer->getHeight();
982 renderTargetFormat = colorbuffer->getActualFormat();
983 missingColorRenderTarget = false;
984 }
Jamie Madillba597af2013-10-22 13:12:15 -0400985
986#ifdef _DEBUG
987 // Workaround for Debug SETSHADERRESOURCES_HAZARD D3D11 warnings
988 for (unsigned int vertexSerialIndex = 0; vertexSerialIndex < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; vertexSerialIndex++)
989 {
990 if (colorbuffer->getTextureSerial() != 0 && mCurVertexTextureSerials[vertexSerialIndex] == colorbuffer->getTextureSerial())
991 {
992 setTexture(gl::SAMPLER_VERTEX, vertexSerialIndex, NULL);
993 }
994 }
995
996 for (unsigned int pixelSerialIndex = 0; pixelSerialIndex < gl::MAX_TEXTURE_IMAGE_UNITS; pixelSerialIndex++)
997 {
998 if (colorbuffer->getTextureSerial() != 0 && mCurPixelTextureSerials[pixelSerialIndex] == colorbuffer->getTextureSerial())
999 {
1000 setTexture(gl::SAMPLER_PIXEL, pixelSerialIndex, NULL);
1001 }
1002 }
1003#endif
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001004 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001005 }
1006
1007 // Get the depth stencil render buffer and serials
1008 gl::Renderbuffer *depthStencil = NULL;
1009 unsigned int depthbufferSerial = 0;
1010 unsigned int stencilbufferSerial = 0;
1011 if (framebuffer->getDepthbufferType() != GL_NONE)
1012 {
1013 depthStencil = framebuffer->getDepthbuffer();
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 depthbufferSerial = depthStencil->getSerial();
1022 }
1023 else if (framebuffer->getStencilbufferType() != GL_NONE)
1024 {
1025 depthStencil = framebuffer->getStencilbuffer();
1026 if (!depthStencil)
1027 {
1028 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001029 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001030 return false;
1031 }
1032
1033 stencilbufferSerial = depthStencil->getSerial();
1034 }
1035
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001036 // Extract the depth stencil sizes and view
1037 unsigned int depthSize = 0;
1038 unsigned int stencilSize = 0;
1039 ID3D11DepthStencilView* framebufferDSV = NULL;
1040 if (depthStencil)
1041 {
1042 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
1043 if (!depthStencilRenderTarget)
1044 {
1045 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001046 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001047 return false;
1048 }
1049
1050 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
1051 if (!framebufferDSV)
1052 {
1053 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001054 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001055 return false;
1056 }
1057
1058 // If there is no render buffer, the width, height and format values come from
1059 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001060 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001061 {
1062 renderTargetWidth = depthStencil->getWidth();
1063 renderTargetHeight = depthStencil->getHeight();
1064 renderTargetFormat = depthStencil->getActualFormat();
1065 }
1066
1067 depthSize = depthStencil->getDepthSize();
1068 stencilSize = depthStencil->getStencilSize();
1069 }
1070
1071 // Apply the render target and depth stencil
1072 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001073 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001074 depthbufferSerial != mAppliedDepthbufferSerial ||
1075 stencilbufferSerial != mAppliedStencilbufferSerial)
1076 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001077 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001078
1079 mRenderTargetDesc.width = renderTargetWidth;
1080 mRenderTargetDesc.height = renderTargetHeight;
1081 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001082 mForceSetViewport = true;
1083 mForceSetScissor = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001084
1085 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1086 {
1087 mCurDepthSize = depthSize;
1088 mForceSetRasterState = true;
1089 }
1090
1091 mCurStencilSize = stencilSize;
1092
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001093 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1094 {
1095 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
1096 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001097 mAppliedDepthbufferSerial = depthbufferSerial;
1098 mAppliedStencilbufferSerial = stencilbufferSerial;
1099 mRenderTargetDescInitialized = true;
1100 mDepthStencilInitialized = true;
1101 }
1102
Geoff Lang42477a42013-09-17 17:07:02 -04001103 invalidateFramebufferSwizzles(framebuffer);
1104
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001105 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001106}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001107
Jamie Madill57a89722013-07-02 11:57:03 -04001108GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001109 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001110{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001111 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001112 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001113 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001114 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001115 return err;
1116 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001117
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001118 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001119}
1120
daniel@transgaming.com31240482012-11-28 21:06:41 +00001121GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001122{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001123 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001124
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001125 if (err == GL_NO_ERROR)
1126 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001127 if (indexInfo->storage)
1128 {
1129 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1130 {
1131 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1132 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1133
Jamie Madill171ca0e2013-10-10 15:10:31 -04001134 mDeviceContext->IASetIndexBuffer(storage->getBuffer(false), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001135
1136 mAppliedIBSerial = 0;
1137 mAppliedStorageIBSerial = storage->getSerial();
1138 mAppliedIBOffset = indexInfo->startOffset;
1139 }
1140 }
1141 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001142 {
1143 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1144
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001145 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001146
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001147 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001148 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001149 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001150 }
1151 }
1152
1153 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001154}
1155
1156void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1157{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001158 if (mode == GL_LINE_LOOP)
1159 {
1160 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1161 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001162 else if (mode == GL_TRIANGLE_FAN)
1163 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001164 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001165 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001166 else if (instances > 0)
1167 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001168 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001169 }
1170 else
1171 {
1172 mDeviceContext->Draw(count, 0);
1173 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001174}
1175
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001176void 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 +00001177{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001178 if (mode == GL_LINE_LOOP)
1179 {
1180 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1181 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001182 else if (mode == GL_TRIANGLE_FAN)
1183 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001184 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1185 }
1186 else if (instances > 0)
1187 {
1188 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001189 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001190 else
1191 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001192 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001193 }
1194}
1195
1196void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1197{
1198 // Get the raw indices for an indexed draw
1199 if (type != GL_NONE && elementArrayBuffer)
1200 {
1201 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001202 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001203 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001204 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001205 }
1206
1207 if (!mLineLoopIB)
1208 {
1209 mLineLoopIB = new StreamingIndexBufferInterface(this);
1210 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1211 {
1212 delete mLineLoopIB;
1213 mLineLoopIB = NULL;
1214
1215 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001216 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001217 }
1218 }
1219
Geoff Lang57e713e2013-07-31 17:01:58 -04001220 // Checked by Renderer11::applyPrimitiveType
1221 ASSERT(count >= 0);
1222
1223 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001224 {
1225 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1226 return gl::error(GL_OUT_OF_MEMORY);
1227 }
1228
Geoff Lang57e713e2013-07-31 17:01:58 -04001229 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001230 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1231 {
1232 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001233 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001234 }
1235
1236 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001237 unsigned int offset;
1238 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001239 {
1240 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001241 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001242 }
1243
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001244 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001245 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001246
1247 switch (type)
1248 {
1249 case GL_NONE: // Non-indexed draw
1250 for (int i = 0; i < count; i++)
1251 {
1252 data[i] = i;
1253 }
1254 data[count] = 0;
1255 break;
1256 case GL_UNSIGNED_BYTE:
1257 for (int i = 0; i < count; i++)
1258 {
1259 data[i] = static_cast<const GLubyte*>(indices)[i];
1260 }
1261 data[count] = static_cast<const GLubyte*>(indices)[0];
1262 break;
1263 case GL_UNSIGNED_SHORT:
1264 for (int i = 0; i < count; i++)
1265 {
1266 data[i] = static_cast<const GLushort*>(indices)[i];
1267 }
1268 data[count] = static_cast<const GLushort*>(indices)[0];
1269 break;
1270 case GL_UNSIGNED_INT:
1271 for (int i = 0; i < count; i++)
1272 {
1273 data[i] = static_cast<const GLuint*>(indices)[i];
1274 }
1275 data[count] = static_cast<const GLuint*>(indices)[0];
1276 break;
1277 default: UNREACHABLE();
1278 }
1279
1280 if (!mLineLoopIB->unmapBuffer())
1281 {
1282 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001283 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001284 }
1285
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001286 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001287 {
1288 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1289
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001290 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001291 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001292 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001293 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001294 }
1295
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001296 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001297}
1298
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001299void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001300{
1301 // Get the raw indices for an indexed draw
1302 if (type != GL_NONE && elementArrayBuffer)
1303 {
1304 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001305 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001306 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001307 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001308 }
1309
1310 if (!mTriangleFanIB)
1311 {
1312 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1313 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1314 {
1315 delete mTriangleFanIB;
1316 mTriangleFanIB = NULL;
1317
1318 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001319 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001320 }
1321 }
1322
Geoff Lang57e713e2013-07-31 17:01:58 -04001323 // Checked by Renderer11::applyPrimitiveType
1324 ASSERT(count >= 3);
1325
Geoff Langeadfd572013-07-09 15:55:07 -04001326 const unsigned int numTris = count - 2;
1327
Geoff Lang57e713e2013-07-31 17:01:58 -04001328 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001329 {
1330 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1331 return gl::error(GL_OUT_OF_MEMORY);
1332 }
1333
1334 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001335 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1336 {
1337 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001338 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001339 }
1340
1341 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001342 unsigned int offset;
1343 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001344 {
1345 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001346 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001347 }
1348
1349 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001350 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001351
1352 switch (type)
1353 {
1354 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001355 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001356 {
1357 data[i*3 + 0] = 0;
1358 data[i*3 + 1] = i + 1;
1359 data[i*3 + 2] = i + 2;
1360 }
1361 break;
1362 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001363 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001364 {
1365 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1366 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1367 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1368 }
1369 break;
1370 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001371 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001372 {
1373 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1374 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1375 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1376 }
1377 break;
1378 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001379 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001380 {
1381 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1382 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1383 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1384 }
1385 break;
1386 default: UNREACHABLE();
1387 }
1388
1389 if (!mTriangleFanIB->unmapBuffer())
1390 {
1391 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001392 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001393 }
1394
1395 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1396 {
1397 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1398
1399 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1400 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001401 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001402 mAppliedIBOffset = indexBufferOffset;
1403 }
1404
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001405 if (instances > 0)
1406 {
1407 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1408 }
1409 else
1410 {
1411 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1412 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001413}
1414
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001415void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1416{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001417 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001418 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1419
1420 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001421 {
1422 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1423 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001424
daniel@transgaming.come4991412012-12-20 20:55:34 +00001425 ID3D11VertexShader *vertexShader = NULL;
1426 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001427
daniel@transgaming.come4991412012-12-20 20:55:34 +00001428 ID3D11PixelShader *pixelShader = NULL;
1429 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001430
daniel@transgaming.come4991412012-12-20 20:55:34 +00001431 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1432 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001433
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001434 programBinary->dirtyAllUniforms();
1435
1436 mAppliedProgramBinarySerial = programBinarySerial;
1437 }
1438
1439 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001440 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001441
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001442 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001443 {
1444 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001445 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001446 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1447 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001448 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1449 }
1450 else
1451 {
1452 mDeviceContext->GSSetShader(NULL, NULL, 0);
1453 }
1454
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001455 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001456 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001457}
1458
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001459void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001460{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001461 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1462 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001463
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001464 unsigned int totalRegisterCountVS = 0;
1465 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001466
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001467 bool vertexUniformsDirty = false;
1468 bool pixelUniformsDirty = false;
1469
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001470 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1471 {
1472 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001473
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001474 if (uniform->isReferencedByVertexShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001475 {
1476 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001477 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001478 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001479
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001480 if (uniform->isReferencedByFragmentShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001481 {
1482 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001483 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001484 }
1485 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001486
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001487 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1488 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1489
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001490 float (*mapVS)[4] = NULL;
1491 float (*mapPS)[4] = NULL;
1492
Shannon Woods5ab33c82013-06-26 15:31:09 -04001493 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1494 {
1495 D3D11_MAPPED_SUBRESOURCE map = {0};
1496 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1497 ASSERT(SUCCEEDED(result));
1498 mapVS = (float(*)[4])map.pData;
1499 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001500
Shannon Woods5ab33c82013-06-26 15:31:09 -04001501 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1502 {
1503 D3D11_MAPPED_SUBRESOURCE map = {0};
1504 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1505 ASSERT(SUCCEEDED(result));
1506 mapPS = (float(*)[4])map.pData;
1507 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001508
Jamie Madill5b085dc2013-08-30 13:21:11 -04001509 for (size_t uniformIndex = 0; uniformIndex < uniformArray->size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001510 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001511 gl::Uniform *uniform = (*uniformArray)[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001512
Nicolas Capense6050882013-07-08 10:43:10 -04001513 if (!gl::IsSampler(uniform->type))
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001514 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001515 unsigned int componentCount = (4 - uniform->registerElement);
1516
Jamie Madill71cc91f2013-09-18 12:51:22 -04001517 // 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 -04001518 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001519
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001520 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001521 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001522 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001523 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001524
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001525 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001526 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001527 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001528 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001529 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001530
1531 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001532 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001533
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001534 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001535 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001536 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001537 }
1538
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001539 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001540 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001541 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001542 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001543
1544 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1545 {
1546 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1547 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1548 }
1549
1550 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1551 {
1552 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1553 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1554 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001555
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001556 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001557 if (!mDriverConstantBufferVS)
1558 {
1559 D3D11_BUFFER_DESC constantBufferDescription = {0};
1560 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1561 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1562 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1563 constantBufferDescription.CPUAccessFlags = 0;
1564 constantBufferDescription.MiscFlags = 0;
1565 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001566
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001567 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001568 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001569
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001570 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1571 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001572
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001573 if (!mDriverConstantBufferPS)
1574 {
1575 D3D11_BUFFER_DESC constantBufferDescription = {0};
1576 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1577 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1578 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1579 constantBufferDescription.CPUAccessFlags = 0;
1580 constantBufferDescription.MiscFlags = 0;
1581 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001582
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001583 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001584 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001585
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001586 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1587 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001588
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001589 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1590 {
1591 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1592 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1593 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001594
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001595 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1596 {
1597 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1598 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1599 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001600
1601 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001602 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1603 {
1604 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1605 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1606 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001607}
1608
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001609void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001610{
Geoff Langda507fe2013-08-20 12:01:42 -04001611 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001612 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001613}
1614
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001615void Renderer11::markAllStateDirty()
1616{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001617 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1618 {
1619 mAppliedRenderTargetSerials[rtIndex] = 0;
1620 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001621 mAppliedDepthbufferSerial = 0;
1622 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001623 mDepthStencilInitialized = false;
1624 mRenderTargetDescInitialized = false;
1625
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001626 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001627 {
1628 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001629 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001630 }
1631 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1632 {
1633 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001634 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001635 }
1636
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001637 mForceSetBlendState = true;
1638 mForceSetRasterState = true;
1639 mForceSetDepthStencilState = true;
1640 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001641 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001642
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001643 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001644 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001645 mAppliedIBOffset = 0;
1646
daniel@transgaming.come4991412012-12-20 20:55:34 +00001647 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001648 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1649 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001650
1651 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001652
1653 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1654 {
1655 mCurrentConstantBufferVS[i] = -1;
1656 mCurrentConstantBufferPS[i] = -1;
1657 }
1658
1659 mCurrentVertexConstantBuffer = NULL;
1660 mCurrentPixelConstantBuffer = NULL;
1661 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001662
1663 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001664}
1665
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001666void Renderer11::releaseDeviceResources()
1667{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001668 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001669 mInputLayoutCache.clear();
1670
Geoff Langea228632013-07-30 15:17:12 -04001671 SafeDelete(mVertexDataManager);
1672 SafeDelete(mIndexDataManager);
1673 SafeDelete(mLineLoopIB);
1674 SafeDelete(mTriangleFanIB);
1675 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001676 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001677 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001678
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001679 SafeRelease(mDriverConstantBufferVS);
1680 SafeRelease(mDriverConstantBufferPS);
1681 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001682}
1683
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001684void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001685{
1686 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001687 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001688}
1689
1690bool Renderer11::isDeviceLost()
1691{
1692 return mDeviceLost;
1693}
1694
1695// set notify to true to broadcast a message to all contexts of the device loss
1696bool Renderer11::testDeviceLost(bool notify)
1697{
1698 bool isLost = false;
1699
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001700 // GetRemovedReason is used to test if the device is removed
1701 HRESULT result = mDevice->GetDeviceRemovedReason();
1702 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001703
1704 if (isLost)
1705 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001706 // Log error if this is a new device lost event
1707 if (mDeviceLost == false)
1708 {
1709 ERR("The D3D11 device was removed: 0x%08X", result);
1710 }
1711
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001712 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001713 // we'll probably get this done again by notifyDeviceLost
1714 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001715 // Note that we don't want to clear the device loss status here
1716 // -- this needs to be done by resetDevice
1717 mDeviceLost = true;
1718 if (notify)
1719 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001720 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001721 }
1722 }
1723
1724 return isLost;
1725}
1726
1727bool Renderer11::testDeviceResettable()
1728{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001729 // determine if the device is resettable by creating a dummy device
1730 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001731
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001732 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001733 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001734 return false;
1735 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001736
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001737 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001738 {
1739 D3D_FEATURE_LEVEL_11_0,
1740 D3D_FEATURE_LEVEL_10_1,
1741 D3D_FEATURE_LEVEL_10_0,
1742 };
1743
1744 ID3D11Device* dummyDevice;
1745 D3D_FEATURE_LEVEL dummyFeatureLevel;
1746 ID3D11DeviceContext* dummyContext;
1747
1748 HRESULT result = D3D11CreateDevice(NULL,
1749 D3D_DRIVER_TYPE_HARDWARE,
1750 NULL,
1751 #if defined(_DEBUG)
1752 D3D11_CREATE_DEVICE_DEBUG,
1753 #else
1754 0,
1755 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001756 featureLevels,
1757 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001758 D3D11_SDK_VERSION,
1759 &dummyDevice,
1760 &dummyFeatureLevel,
1761 &dummyContext);
1762
1763 if (!mDevice || FAILED(result))
1764 {
1765 return false;
1766 }
1767
Geoff Langea228632013-07-30 15:17:12 -04001768 SafeRelease(dummyContext);
1769 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001770
1771 return true;
1772}
1773
1774void Renderer11::release()
1775{
1776 releaseDeviceResources();
1777
Geoff Langea228632013-07-30 15:17:12 -04001778 SafeRelease(mDxgiFactory);
1779 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001780
1781 if (mDeviceContext)
1782 {
1783 mDeviceContext->ClearState();
1784 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001785 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001786 }
1787
Geoff Langea228632013-07-30 15:17:12 -04001788 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001789
1790 if (mD3d11Module)
1791 {
1792 FreeLibrary(mD3d11Module);
1793 mD3d11Module = NULL;
1794 }
1795
1796 if (mDxgiModule)
1797 {
1798 FreeLibrary(mDxgiModule);
1799 mDxgiModule = NULL;
1800 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001801}
1802
1803bool Renderer11::resetDevice()
1804{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001805 // recreate everything
1806 release();
1807 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001808
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001809 if (result != EGL_SUCCESS)
1810 {
1811 ERR("Could not reinitialize D3D11 device: %08X", result);
1812 return false;
1813 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001814
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001815 mDeviceLost = false;
1816
1817 return true;
1818}
1819
1820DWORD Renderer11::getAdapterVendor() const
1821{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001822 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001823}
1824
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001825std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001826{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001827 std::ostringstream rendererString;
1828
1829 rendererString << mDescription;
1830 rendererString << " Direct3D11";
1831
1832 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1833 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1834
1835 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001836}
1837
1838GUID Renderer11::getAdapterIdentifier() const
1839{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001840 // Use the adapter LUID as our adapter ID
1841 // This number is local to a machine is only guaranteed to be unique between restarts
1842 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1843 GUID adapterId = {0};
1844 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1845 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001846}
1847
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001848bool Renderer11::getBGRATextureSupport() const
1849{
1850 return mBGRATextureSupport;
1851}
1852
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001853bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001854{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001855 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001856}
1857
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001858bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001859{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001860 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001861}
1862
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001863bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001864{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001865 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001866}
1867
1868bool Renderer11::getDepthTextureSupport() const
1869{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001870 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001871}
1872
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001873bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001874{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001875 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001876}
1877
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001878bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001879{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001880 return mFloat32FilterSupport;
1881}
1882
1883bool Renderer11::getFloat32TextureRenderingSupport() const
1884{
1885 return mFloat32RenderSupport;
1886}
1887
1888bool Renderer11::getFloat16TextureSupport() const
1889{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001890 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001891}
1892
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001893bool Renderer11::getFloat16TextureFilteringSupport() const
1894{
1895 return mFloat16FilterSupport;
1896}
1897
1898bool Renderer11::getFloat16TextureRenderingSupport() const
1899{
1900 return mFloat16RenderSupport;
1901}
1902
Geoff Langd42cf4e2013-06-05 16:09:17 -04001903bool Renderer11::getRGB565TextureSupport() const
1904{
1905 return false;
1906}
1907
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001908bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001909{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001910 return false;
1911}
1912
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001913bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001914{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001915 return false;
1916}
1917
Geoff Lang632192d2013-10-04 13:40:46 -04001918bool Renderer11::getRGTextureSupport() const
1919{
1920 return mRGTextureSupport;
1921}
1922
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001923bool Renderer11::getTextureFilterAnisotropySupport() const
1924{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001925 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001926}
1927
1928float Renderer11::getTextureMaxAnisotropy() const
1929{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001930 switch (mFeatureLevel)
1931 {
1932 case D3D_FEATURE_LEVEL_11_0:
1933 return D3D11_MAX_MAXANISOTROPY;
1934 case D3D_FEATURE_LEVEL_10_1:
1935 case D3D_FEATURE_LEVEL_10_0:
1936 return D3D10_MAX_MAXANISOTROPY;
1937 default: UNREACHABLE();
1938 return 0;
1939 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001940}
1941
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001942bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001943{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00001944 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001945}
1946
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001947Range Renderer11::getViewportBounds() const
1948{
1949 switch (mFeatureLevel)
1950 {
1951 case D3D_FEATURE_LEVEL_11_0:
1952 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1953 case D3D_FEATURE_LEVEL_10_1:
1954 case D3D_FEATURE_LEVEL_10_0:
1955 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1956 default: UNREACHABLE();
1957 return Range(0, 0);
1958 }
1959}
1960
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001961unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001962{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001963 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1964 switch (mFeatureLevel)
1965 {
1966 case D3D_FEATURE_LEVEL_11_0:
1967 case D3D_FEATURE_LEVEL_10_1:
1968 case D3D_FEATURE_LEVEL_10_0:
1969 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1970 default: UNREACHABLE();
1971 return 0;
1972 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001973}
1974
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001975unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1976{
1977 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1978}
1979
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001980unsigned int Renderer11::getReservedVertexUniformVectors() const
1981{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001982 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001983}
1984
1985unsigned int Renderer11::getReservedFragmentUniformVectors() const
1986{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001987 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001988}
1989
1990unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001991{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001992 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1993 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1994 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001995}
1996
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001997unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001998{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001999 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2000 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2001 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002002}
2003
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002004unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002005{
2006 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00002007 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
2008 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002009 switch (mFeatureLevel)
2010 {
2011 case D3D_FEATURE_LEVEL_11_0:
2012 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2013 case D3D_FEATURE_LEVEL_10_1:
2014 case D3D_FEATURE_LEVEL_10_0:
2015 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2016 default: UNREACHABLE();
2017 return 0;
2018 }
2019}
2020
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002021unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2022{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002023 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2024 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2025
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002026 switch (mFeatureLevel)
2027 {
2028 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002029 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002030 case D3D_FEATURE_LEVEL_10_1:
2031 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002032 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002033 default: UNREACHABLE();
2034 return 0;
2035 }
2036}
2037
2038unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2039{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002040 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2041 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2042
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002043 switch (mFeatureLevel)
2044 {
2045 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002046 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002047 case D3D_FEATURE_LEVEL_10_1:
2048 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002049 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002050 default: UNREACHABLE();
2051 return 0;
2052 }
2053}
2054
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002055unsigned int Renderer11::getReservedVertexUniformBuffers() const
2056{
2057 // we reserve one buffer for the application uniforms, and one for driver uniforms
2058 return 2;
2059}
2060
2061unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2062{
2063 // we reserve one buffer for the application uniforms, and one for driver uniforms
2064 return 2;
2065}
2066
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002067unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2068{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002069 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2070 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2071
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002072 switch (mFeatureLevel)
2073 {
2074 case D3D_FEATURE_LEVEL_11_0:
2075 return D3D11_SO_BUFFER_SLOT_COUNT;
2076 case D3D_FEATURE_LEVEL_10_1:
2077 case D3D_FEATURE_LEVEL_10_0:
2078 return D3D10_SO_BUFFER_SLOT_COUNT;
2079 default: UNREACHABLE();
2080 return 0;
2081 }
2082}
2083
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002084unsigned int Renderer11::getMaxUniformBufferSize() const
2085{
2086 // Each component is a 4-element vector of 4-byte units (floats)
2087 const unsigned int bytesPerComponent = 4 * sizeof(float);
2088
2089 switch (mFeatureLevel)
2090 {
2091 case D3D_FEATURE_LEVEL_11_0:
2092 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2093 case D3D_FEATURE_LEVEL_10_1:
2094 case D3D_FEATURE_LEVEL_10_0:
2095 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2096 default: UNREACHABLE();
2097 return 0;
2098 }
2099}
2100
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002101bool Renderer11::getNonPower2TextureSupport() const
2102{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002103 switch (mFeatureLevel)
2104 {
2105 case D3D_FEATURE_LEVEL_11_0:
2106 case D3D_FEATURE_LEVEL_10_1:
2107 case D3D_FEATURE_LEVEL_10_0:
2108 return true;
2109 default: UNREACHABLE();
2110 return false;
2111 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002112}
2113
2114bool Renderer11::getOcclusionQuerySupport() const
2115{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002116 switch (mFeatureLevel)
2117 {
2118 case D3D_FEATURE_LEVEL_11_0:
2119 case D3D_FEATURE_LEVEL_10_1:
2120 case D3D_FEATURE_LEVEL_10_0:
2121 return true;
2122 default: UNREACHABLE();
2123 return false;
2124 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002125}
2126
2127bool Renderer11::getInstancingSupport() const
2128{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002129 switch (mFeatureLevel)
2130 {
2131 case D3D_FEATURE_LEVEL_11_0:
2132 case D3D_FEATURE_LEVEL_10_1:
2133 case D3D_FEATURE_LEVEL_10_0:
2134 return true;
2135 default: UNREACHABLE();
2136 return false;
2137 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002138}
2139
2140bool Renderer11::getShareHandleSupport() const
2141{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002142 // We only currently support share handles with BGRA surfaces, because
2143 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002144 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002145 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002146}
2147
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002148bool Renderer11::getDerivativeInstructionSupport() const
2149{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002150 switch (mFeatureLevel)
2151 {
2152 case D3D_FEATURE_LEVEL_11_0:
2153 case D3D_FEATURE_LEVEL_10_1:
2154 case D3D_FEATURE_LEVEL_10_0:
2155 return true;
2156 default: UNREACHABLE();
2157 return false;
2158 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002159}
2160
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002161bool Renderer11::getPostSubBufferSupport() const
2162{
2163 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2164 return false;
2165}
2166
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002167int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002168{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002169 switch (mFeatureLevel)
2170 {
2171 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002172 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002173 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2174 default: UNREACHABLE(); return 0;
2175 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002176}
2177
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002178int Renderer11::getMinorShaderModel() const
2179{
2180 switch (mFeatureLevel)
2181 {
2182 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2183 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2184 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2185 default: UNREACHABLE(); return 0;
2186 }
2187}
2188
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002189float Renderer11::getMaxPointSize() const
2190{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002191 // choose a reasonable maximum. we enforce this in the shader.
2192 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2193 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002194}
2195
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002196int Renderer11::getMaxViewportDimension() const
2197{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002198 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2199 // In our case return the maximum texture size, which is the maximum render buffer size.
2200 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2201 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2202
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002203 switch (mFeatureLevel)
2204 {
2205 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002206 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002207 case D3D_FEATURE_LEVEL_10_1:
2208 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002209 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002210 default: UNREACHABLE();
2211 return 0;
2212 }
2213}
2214
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002215int Renderer11::getMaxTextureWidth() const
2216{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002217 switch (mFeatureLevel)
2218 {
2219 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2220 case D3D_FEATURE_LEVEL_10_1:
2221 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2222 default: UNREACHABLE(); return 0;
2223 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002224}
2225
2226int Renderer11::getMaxTextureHeight() const
2227{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002228 switch (mFeatureLevel)
2229 {
2230 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2231 case D3D_FEATURE_LEVEL_10_1:
2232 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2233 default: UNREACHABLE(); return 0;
2234 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002235}
2236
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002237int Renderer11::getMaxTextureDepth() const
2238{
2239 switch (mFeatureLevel)
2240 {
2241 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2242 case D3D_FEATURE_LEVEL_10_1:
2243 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2244 default: UNREACHABLE(); return 0;
2245 }
2246}
2247
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002248int Renderer11::getMaxTextureArrayLayers() const
2249{
2250 switch (mFeatureLevel)
2251 {
2252 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2253 case D3D_FEATURE_LEVEL_10_1:
2254 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2255 default: UNREACHABLE(); return 0;
2256 }
2257}
2258
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002259bool Renderer11::get32BitIndexSupport() const
2260{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002261 switch (mFeatureLevel)
2262 {
2263 case D3D_FEATURE_LEVEL_11_0:
2264 case D3D_FEATURE_LEVEL_10_1:
2265 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2266 default: UNREACHABLE(); return false;
2267 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002268}
2269
2270int Renderer11::getMinSwapInterval() const
2271{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002272 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002273}
2274
2275int Renderer11::getMaxSwapInterval() const
2276{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002277 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002278}
2279
2280int Renderer11::getMaxSupportedSamples() const
2281{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002282 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002283}
2284
Geoff Lang005df412013-10-16 14:12:50 -04002285GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002286{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002287 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002288 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2289 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2290}
2291
Geoff Lang005df412013-10-16 14:12:50 -04002292GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002293{
2294 unsigned int numCounts = 0;
2295
2296 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002297 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2298 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002299 {
2300 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2301 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2302
2303 if (iter != mMultisampleSupportMap.end())
2304 {
2305 const MultisampleSupportInfo& info = iter->second;
2306 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2307 {
2308 if (info.qualityLevels[i] > 0)
2309 {
2310 numCounts++;
2311 }
2312 }
2313 }
2314 }
2315
2316 return numCounts;
2317}
2318
Geoff Lang005df412013-10-16 14:12:50 -04002319void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002320{
2321 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002322 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2323 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2324 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002325 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002326 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002327
2328 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2329 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2330
2331 if (iter != mMultisampleSupportMap.end())
2332 {
2333 const MultisampleSupportInfo& info = iter->second;
2334 int bufPos = 0;
2335 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2336 {
2337 if (info.qualityLevels[i] > 0)
2338 {
2339 params[bufPos++] = i + 1;
2340 }
2341 }
2342 }
2343}
2344
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002345int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2346{
2347 if (requested == 0)
2348 {
2349 return 0;
2350 }
2351
2352 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2353 if (iter != mMultisampleSupportMap.end())
2354 {
2355 const MultisampleSupportInfo& info = iter->second;
2356 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2357 {
2358 if (info.qualityLevels[i] > 0)
2359 {
2360 return i + 1;
2361 }
2362 }
2363 }
2364
2365 return -1;
2366}
2367
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002368unsigned int Renderer11::getMaxRenderTargets() const
2369{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002370 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2371 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2372
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002373 switch (mFeatureLevel)
2374 {
2375 case D3D_FEATURE_LEVEL_11_0:
2376 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2377 case D3D_FEATURE_LEVEL_10_1:
2378 case D3D_FEATURE_LEVEL_10_0:
2379 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2380 default:
2381 UNREACHABLE();
2382 return 1;
2383 }
2384}
2385
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002386bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002387{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002388 if (source && dest)
2389 {
2390 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2391 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2392
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002393 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002394
2395 dest11->invalidateSwizzleCache();
2396
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002397 return true;
2398 }
2399
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002400 return false;
2401}
2402
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002403bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002404{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002405 if (source && dest)
2406 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002407 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2408 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002409
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002410 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002411
2412 dest11->invalidateSwizzleCache();
2413
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002414 return true;
2415 }
2416
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002417 return false;
2418}
2419
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002420bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2421{
2422 if (source && dest)
2423 {
2424 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2425 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2426
2427 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002428
2429 dest11->invalidateSwizzleCache();
2430
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002431 return true;
2432 }
2433
2434 return false;
2435}
2436
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002437bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2438{
2439 if (source && dest)
2440 {
2441 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2442 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2443
2444 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002445
2446 dest11->invalidateSwizzleCache();
2447
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002448 return true;
2449 }
2450
2451 return false;
2452}
2453
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002454bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002455 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002456{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002457 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002458 if (!colorbuffer)
2459 {
2460 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002461 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002462 }
2463
2464 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2465 if (!sourceRenderTarget)
2466 {
2467 ERR("Failed to retrieve the render target 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 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2472 if (!source)
2473 {
2474 ERR("Failed to retrieve the render target view from the render target.");
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 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2479 if (!storage11)
2480 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002481 ERR("Failed to retrieve the texture storage from the destination.");
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 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2486 if (!destRenderTarget)
2487 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002488 ERR("Failed to retrieve the render target from the destination storage.");
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 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2493 if (!dest)
2494 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002495 ERR("Failed to retrieve the render target view from the destination render target.");
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
Geoff Langb86b9792013-06-04 16:32:05 -04002499 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2500 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002501
Geoff Langb86b9792013-06-04 16:32:05 -04002502 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2503 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002504
Geoff Langb86b9792013-06-04 16:32:05 -04002505 // Use nearest filtering because source and destination are the same size for the direct
2506 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002507 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002508 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002509
Geoff Lang42477a42013-09-17 17:07:02 -04002510 storage11->invalidateSwizzleCacheLevel(level);
2511
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002512 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002513}
2514
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002515bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002516 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002517{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002518 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002519 if (!colorbuffer)
2520 {
2521 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002522 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002523 }
2524
2525 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2526 if (!sourceRenderTarget)
2527 {
2528 ERR("Failed to retrieve the render target 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 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2533 if (!source)
2534 {
2535 ERR("Failed to retrieve the render target view from the render target.");
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 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2540 if (!storage11)
2541 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002542 ERR("Failed to retrieve the texture storage from the destination.");
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
Nicolas Capensb13f8662013-06-04 13:30:19 -04002546 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002547 if (!destRenderTarget)
2548 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002549 ERR("Failed to retrieve the render target from the destination storage.");
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
2553 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2554 if (!dest)
2555 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002556 ERR("Failed to retrieve the render target view from the destination render target.");
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
Geoff Langb86b9792013-06-04 16:32:05 -04002560 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2561 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002562
Geoff Langb86b9792013-06-04 16:32:05 -04002563 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2564 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002565
Geoff Langb86b9792013-06-04 16:32:05 -04002566 // Use nearest filtering because source and destination are the same size for the direct
2567 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002568 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002569 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002570
Geoff Lang42477a42013-09-17 17:07:02 -04002571 storage11->invalidateSwizzleCacheLevel(level);
2572
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002573 return ret;
2574}
2575
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002576bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2577 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2578{
2579 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2580 if (!colorbuffer)
2581 {
2582 ERR("Failed to retrieve the color buffer from the frame buffer.");
2583 return gl::error(GL_OUT_OF_MEMORY, false);
2584 }
2585
2586 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2587 if (!sourceRenderTarget)
2588 {
2589 ERR("Failed to retrieve the render target from the frame buffer.");
2590 return gl::error(GL_OUT_OF_MEMORY, false);
2591 }
2592
2593 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2594 if (!source)
2595 {
2596 ERR("Failed to retrieve the render target view from the render target.");
2597 return gl::error(GL_OUT_OF_MEMORY, false);
2598 }
2599
2600 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2601 if (!storage11)
2602 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002603 ERR("Failed to retrieve the texture storage from the destination.");
2604 return gl::error(GL_OUT_OF_MEMORY, false);
2605 }
2606
2607 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2608 if (!destRenderTarget)
2609 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002610 ERR("Failed to retrieve the render target from the destination storage.");
2611 return gl::error(GL_OUT_OF_MEMORY, false);
2612 }
2613
2614 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2615 if (!dest)
2616 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002617 ERR("Failed to retrieve the render target view from the destination render target.");
2618 return gl::error(GL_OUT_OF_MEMORY, false);
2619 }
2620
Geoff Langb86b9792013-06-04 16:32:05 -04002621 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2622 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002623
Geoff Langb86b9792013-06-04 16:32:05 -04002624 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2625 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002626
Geoff Langb86b9792013-06-04 16:32:05 -04002627 // Use nearest filtering because source and destination are the same size for the direct
2628 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002629 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002630 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002631
Geoff Lang42477a42013-09-17 17:07:02 -04002632 storage11->invalidateSwizzleCacheLevel(level);
2633
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002634 return ret;
2635}
2636
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002637bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2638 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2639{
2640 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2641 if (!colorbuffer)
2642 {
2643 ERR("Failed to retrieve the color buffer from the frame buffer.");
2644 return gl::error(GL_OUT_OF_MEMORY, false);
2645 }
2646
2647 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2648 if (!sourceRenderTarget)
2649 {
2650 ERR("Failed to retrieve the render target from the frame buffer.");
2651 return gl::error(GL_OUT_OF_MEMORY, false);
2652 }
2653
2654 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2655 if (!source)
2656 {
2657 ERR("Failed to retrieve the render target view from the render target.");
2658 return gl::error(GL_OUT_OF_MEMORY, false);
2659 }
2660
2661 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2662 if (!storage11)
2663 {
Geoff Langea228632013-07-30 15:17:12 -04002664 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002665 ERR("Failed to retrieve the texture storage from the destination.");
2666 return gl::error(GL_OUT_OF_MEMORY, false);
2667 }
2668
2669 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2670 if (!destRenderTarget)
2671 {
Geoff Langea228632013-07-30 15:17:12 -04002672 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002673 ERR("Failed to retrieve the render target from the destination storage.");
2674 return gl::error(GL_OUT_OF_MEMORY, false);
2675 }
2676
2677 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2678 if (!dest)
2679 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002680 ERR("Failed to retrieve the render target view from the destination render target.");
2681 return gl::error(GL_OUT_OF_MEMORY, false);
2682 }
2683
Geoff Langb86b9792013-06-04 16:32:05 -04002684 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2685 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002686
Geoff Langb86b9792013-06-04 16:32:05 -04002687 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2688 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002689
Geoff Langb86b9792013-06-04 16:32:05 -04002690 // Use nearest filtering because source and destination are the same size for the direct
2691 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002692 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002693 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002694
Geoff Lang42477a42013-09-17 17:07:02 -04002695 storage11->invalidateSwizzleCacheLevel(level);
2696
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002697 return ret;
2698}
2699
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002700void Renderer11::unapplyRenderTargets()
2701{
2702 setOneTimeRenderTarget(NULL);
2703}
2704
2705void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2706{
2707 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2708
2709 rtvArray[0] = renderTargetView;
2710
2711 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2712
2713 // Do not preserve the serial for this one-time-use render target
2714 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2715 {
2716 mAppliedRenderTargetSerials[rtIndex] = 0;
2717 }
2718}
2719
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002720RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2721{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002722 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002723 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002724
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002725 if (depth)
2726 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002727 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002728 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002729 swapChain11->getDepthStencilTexture(),
2730 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002731 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002732 }
2733 else
2734 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002735 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002736 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002737 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002738 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002739 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002740 }
2741 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002742}
2743
Geoff Langa2d97f12013-06-11 11:44:02 -04002744RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002745{
Geoff Langa2d97f12013-06-11 11:44:02 -04002746 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002747 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002748}
2749
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002750ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002751{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002752 ShaderExecutable11 *executable = NULL;
2753
2754 switch (type)
2755 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002756 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002757 {
2758 ID3D11VertexShader *vshader = NULL;
2759 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2760 ASSERT(SUCCEEDED(result));
2761
2762 if (vshader)
2763 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002764 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002765 }
2766 }
2767 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002768 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002769 {
2770 ID3D11PixelShader *pshader = NULL;
2771 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2772 ASSERT(SUCCEEDED(result));
2773
2774 if (pshader)
2775 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002776 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002777 }
2778 }
2779 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002780 case rx::SHADER_GEOMETRY:
2781 {
2782 ID3D11GeometryShader *gshader = NULL;
2783 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2784 ASSERT(SUCCEEDED(result));
2785
2786 if (gshader)
2787 {
2788 executable = new ShaderExecutable11(function, length, gshader);
2789 }
2790 }
2791 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002792 default:
2793 UNREACHABLE();
2794 break;
2795 }
2796
2797 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002798}
2799
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002800ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002801{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002802 const char *profile = NULL;
2803
2804 switch (type)
2805 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002806 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002807 profile = "vs_4_0";
2808 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002809 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002810 profile = "ps_4_0";
2811 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002812 case rx::SHADER_GEOMETRY:
2813 profile = "gs_4_0";
2814 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002815 default:
2816 UNREACHABLE();
2817 return NULL;
2818 }
2819
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00002820 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002821 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002822 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002823 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002824 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002825
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002826 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002827 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002828
2829 return executable;
2830}
2831
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002832VertexBuffer *Renderer11::createVertexBuffer()
2833{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002834 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002835}
2836
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002837IndexBuffer *Renderer11::createIndexBuffer()
2838{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002839 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002840}
2841
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002842BufferStorage *Renderer11::createBufferStorage()
2843{
2844 return new BufferStorage11(this);
2845}
2846
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002847QueryImpl *Renderer11::createQuery(GLenum type)
2848{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002849 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002850}
2851
2852FenceImpl *Renderer11::createFence()
2853{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002854 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002855}
2856
Geoff Lang005df412013-10-16 14:12:50 -04002857bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002858{
Jamie Madill4461f092013-10-10 15:10:39 -04002859 int clientVersion = getCurrentClientVersion();
2860
2861 // We only support buffer to texture copies in ES3
2862 if (clientVersion <= 2)
2863 {
2864 return false;
2865 }
2866
2867 // sRGB formats do not work with D3D11 buffer SRVs
2868 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2869 {
2870 return false;
2871 }
2872
2873 // We cannot support direct copies to non-color-renderable formats
2874 if (!gl::IsColorRenderingSupported(internalFormat, this))
2875 {
2876 return false;
2877 }
2878
2879 // We skip all 3-channel formats since sometimes format support is missing
2880 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2881 {
2882 return false;
2883 }
2884
2885 // We don't support formats which we can't represent without conversion
2886 if (getNativeTextureFormat(internalFormat) != internalFormat)
2887 {
2888 return false;
2889 }
2890
2891 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002892}
2893
Jamie Madilla21eea32013-09-18 14:36:25 -04002894bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2895 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2896{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002897 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002898 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2899}
2900
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002901bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002902{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002903 ASSERT(colorbuffer != NULL);
2904
2905 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2906 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002907 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002908 *subresourceIndex = renderTarget->getSubresourceIndex();
2909
2910 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2911 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002912 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002913 ID3D11Resource *textureResource = NULL;
2914 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002915
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002916 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002917 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002918 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002919 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002920
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002921 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002922 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002923 return true;
2924 }
2925 else
2926 {
2927 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2928 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002929 }
2930 }
2931 }
2932 }
2933
2934 return false;
2935}
2936
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002937bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002938 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002939{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002940 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002941 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002942 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002943
2944 if (!readBuffer)
2945 {
2946 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2947 return gl::error(GL_OUT_OF_MEMORY, false);
2948 }
2949
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002950 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002951
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002952 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002953 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002954 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2955 {
2956 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
2957
2958 if (!drawBuffer)
2959 {
2960 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2961 return gl::error(GL_OUT_OF_MEMORY, false);
2962 }
2963
2964 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2965
Geoff Lang125deab2013-08-09 13:34:16 -04002966 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002967 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002968 {
2969 return false;
2970 }
2971 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002972 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002973 }
2974
Geoff Lang685806d2013-06-12 11:16:36 -04002975 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002976 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002977 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
2978 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
2979
2980 if (!readBuffer)
2981 {
2982 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2983 return gl::error(GL_OUT_OF_MEMORY, false);
2984 }
2985
2986 if (!drawBuffer)
2987 {
2988 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2989 return gl::error(GL_OUT_OF_MEMORY, false);
2990 }
2991
2992 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2993 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2994
Geoff Lang125deab2013-08-09 13:34:16 -04002995 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002996 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002997 {
2998 return false;
2999 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003000 }
3001
Geoff Lang42477a42013-09-17 17:07:02 -04003002 invalidateFramebufferSwizzles(drawTarget);
3003
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003004 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003005}
3006
3007void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3008 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3009{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003010 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003011 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003012
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003013 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3014
3015 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003016 {
3017 gl::Rectangle area;
3018 area.x = x;
3019 area.y = y;
3020 area.width = width;
3021 area.height = height;
3022
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003023 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3024 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003025
Geoff Langea228632013-07-30 15:17:12 -04003026 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003027 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003028}
3029
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003030Image *Renderer11::createImage()
3031{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003032 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003033}
3034
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003035void Renderer11::generateMipmap(Image *dest, Image *src)
3036{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003037 Image11 *dest11 = Image11::makeImage11(dest);
3038 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003039 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003040}
3041
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003042TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3043{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003044 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3045 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003046}
3047
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003048TextureStorage *Renderer11::createTextureStorage2D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003049{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003050 return new TextureStorage11_2D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003051}
3052
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003053TextureStorage *Renderer11::createTextureStorageCube(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, int size)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003054{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003055 return new TextureStorage11_Cube(this, baseLevel, maxLevel, internalformat, renderTarget, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003056}
3057
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003058TextureStorage *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 +00003059{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003060 return new TextureStorage11_3D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003061}
3062
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003063TextureStorage *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 +00003064{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003065 return new TextureStorage11_2DArray(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003066}
3067
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003068void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3069 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3070 GLint packAlignment, void *pixels)
3071{
3072 D3D11_TEXTURE2D_DESC textureDesc;
3073 texture->GetDesc(&textureDesc);
3074
3075 D3D11_TEXTURE2D_DESC stagingDesc;
3076 stagingDesc.Width = area.width;
3077 stagingDesc.Height = area.height;
3078 stagingDesc.MipLevels = 1;
3079 stagingDesc.ArraySize = 1;
3080 stagingDesc.Format = textureDesc.Format;
3081 stagingDesc.SampleDesc.Count = 1;
3082 stagingDesc.SampleDesc.Quality = 0;
3083 stagingDesc.Usage = D3D11_USAGE_STAGING;
3084 stagingDesc.BindFlags = 0;
3085 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3086 stagingDesc.MiscFlags = 0;
3087
3088 ID3D11Texture2D* stagingTex = NULL;
3089 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3090 if (FAILED(result))
3091 {
3092 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3093 return;
3094 }
3095
3096 ID3D11Texture2D* srcTex = NULL;
3097 if (textureDesc.SampleDesc.Count > 1)
3098 {
3099 D3D11_TEXTURE2D_DESC resolveDesc;
3100 resolveDesc.Width = textureDesc.Width;
3101 resolveDesc.Height = textureDesc.Height;
3102 resolveDesc.MipLevels = 1;
3103 resolveDesc.ArraySize = 1;
3104 resolveDesc.Format = textureDesc.Format;
3105 resolveDesc.SampleDesc.Count = 1;
3106 resolveDesc.SampleDesc.Quality = 0;
3107 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3108 resolveDesc.BindFlags = 0;
3109 resolveDesc.CPUAccessFlags = 0;
3110 resolveDesc.MiscFlags = 0;
3111
3112 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3113 if (FAILED(result))
3114 {
3115 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003116 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003117 return;
3118 }
3119
3120 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3121 subResource = 0;
3122 }
3123 else
3124 {
3125 srcTex = texture;
3126 srcTex->AddRef();
3127 }
3128
3129 D3D11_BOX srcBox;
3130 srcBox.left = area.x;
3131 srcBox.right = area.x + area.width;
3132 srcBox.top = area.y;
3133 srcBox.bottom = area.y + area.height;
3134 srcBox.front = 0;
3135 srcBox.back = 1;
3136
3137 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3138
Geoff Langea228632013-07-30 15:17:12 -04003139 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003140
3141 D3D11_MAPPED_SUBRESOURCE mapping;
3142 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3143
3144 unsigned char *source;
3145 int inputPitch;
3146 if (packReverseRowOrder)
3147 {
3148 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3149 inputPitch = -static_cast<int>(mapping.RowPitch);
3150 }
3151 else
3152 {
3153 source = static_cast<unsigned char*>(mapping.pData);
3154 inputPitch = static_cast<int>(mapping.RowPitch);
3155 }
3156
Geoff Lang697ad3e2013-06-04 10:11:28 -04003157 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003158
Geoff Lang005df412013-10-16 14:12:50 -04003159 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003160 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3161 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3162
3163 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3164
3165 if (sourceFormat == format && sourceType == type)
3166 {
3167 // Direct copy possible
3168 unsigned char *dest = static_cast<unsigned char*>(pixels);
3169 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003170 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003171 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003172 }
3173 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003174 else
3175 {
Geoff Lang005df412013-10-16 14:12:50 -04003176 GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003177 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3178
3179 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type, getCurrentClientVersion());
3180 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003181 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003182 // Fast copy is possible through some special function
3183 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003184 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003185 for (int x = 0; x < area.width; x++)
3186 {
3187 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3188 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3189
3190 fastCopyFunc(src, dest);
3191 }
3192 }
3193 }
3194 else
3195 {
Jamie Madilld6cb2442013-07-10 15:13:38 -04003196 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003197 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3198
3199 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3200 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3201 sizeof(temp) >= sizeof(gl::ColorUI) &&
3202 sizeof(temp) >= sizeof(gl::ColorI));
3203
3204 for (int y = 0; y < area.height; y++)
3205 {
3206 for (int x = 0; x < area.width; x++)
3207 {
3208 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3209 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3210
3211 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3212 // will not allow the copy otherwise.
3213 readFunc(src, temp);
3214 writeFunc(temp, dest);
3215 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003216 }
3217 }
3218 }
3219
3220 mDeviceContext->Unmap(stagingTex, 0);
3221
Geoff Langea228632013-07-30 15:17:12 -04003222 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003223}
3224
Geoff Lang758d5b22013-06-11 11:42:50 -04003225bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003226 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3227 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003228{
Geoff Lang975af372013-06-12 11:19:22 -04003229 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3230 // it should never be the case that both color and depth/stencil need to be blitted at
3231 // at the same time.
3232 ASSERT(colorBlit != (depthBlit || stencilBlit));
3233
Geoff Langc1f51be2013-06-11 11:49:14 -04003234 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003235
Geoff Lang4d782732013-07-22 10:44:18 -04003236 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3237 if (!drawRenderTarget)
3238 {
3239 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3240 return gl::error(GL_OUT_OF_MEMORY, false);
3241 }
3242
3243 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3244 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3245 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3246 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3247
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003248 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3249 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003250 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003251 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003252 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003253 }
3254
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003255 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003256 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003257 unsigned int readSubresource = 0;
3258 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003259 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003260 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3261 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003262
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003263 if (unresolvedTexture)
3264 {
3265 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3266 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003267
Geoff Langea228632013-07-30 15:17:12 -04003268 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003269
3270 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3271 if (FAILED(result))
3272 {
Geoff Langea228632013-07-30 15:17:12 -04003273 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003274 return gl::error(GL_OUT_OF_MEMORY, false);
3275 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003276 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003277 }
3278 else
3279 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003280 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003281 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003282 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003283 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003284 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003285 }
3286
Geoff Lang4d782732013-07-22 10:44:18 -04003287 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003288 {
Geoff Lang4d782732013-07-22 10:44:18 -04003289 SafeRelease(readTexture);
3290 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003291 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003292 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003293 }
3294
Geoff Lang125deab2013-08-09 13:34:16 -04003295 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3296 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3297
3298 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3299
3300 bool wholeBufferCopy = !scissorNeeded &&
3301 readRect.x == 0 && readRect.width == readSize.width &&
3302 readRect.y == 0 && readRect.height == readSize.height &&
3303 drawRect.x == 0 && drawRect.width == drawSize.width &&
3304 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003305
Geoff Langc1f51be2013-06-11 11:49:14 -04003306 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003307
Geoff Lang125deab2013-08-09 13:34:16 -04003308 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || readRect.height < 0;
3309
3310 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3311 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3312 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3313 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3314
3315 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3316 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3317 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3318
Geoff Langc1f51be2013-06-11 11:49:14 -04003319 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003320 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3321 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003322 {
Geoff Lang125deab2013-08-09 13:34:16 -04003323 UINT dstX = drawRect.x;
3324 UINT dstY = drawRect.y;
3325
Geoff Langc1f51be2013-06-11 11:49:14 -04003326 D3D11_BOX readBox;
3327 readBox.left = readRect.x;
3328 readBox.right = readRect.x + readRect.width;
3329 readBox.top = readRect.y;
3330 readBox.bottom = readRect.y + readRect.height;
3331 readBox.front = 0;
3332 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003333
Geoff Lang125deab2013-08-09 13:34:16 -04003334 if (scissorNeeded)
3335 {
3336 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3337 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3338
3339 if (drawRect.x < scissor->x)
3340 {
3341 dstX = scissor->x;
3342 readBox.left += (scissor->x - drawRect.x);
3343 }
3344 if (drawRect.y < scissor->y)
3345 {
3346 dstY = scissor->y;
3347 readBox.top += (scissor->y - drawRect.y);
3348 }
3349 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3350 {
3351 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3352 }
3353 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3354 {
3355 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3356 }
3357 }
3358
Geoff Langc1f51be2013-06-11 11:49:14 -04003359 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3360 // We also require complete framebuffer copies for depth-stencil blit.
3361 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003362
Geoff Lang125deab2013-08-09 13:34:16 -04003363 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003364 readTexture, readSubresource, pSrcBox);
3365 result = true;
3366 }
3367 else
3368 {
3369 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003370 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003371
Geoff Lang975af372013-06-12 11:19:22 -04003372 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003373 {
Geoff Lang975af372013-06-12 11:19:22 -04003374 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003375 drawTexture, drawSubresource, drawArea, drawSize,
3376 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003377 }
3378 else if (depthBlit)
3379 {
Geoff Lang125deab2013-08-09 13:34:16 -04003380 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3381 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003382 }
3383 else if (stencilBlit)
3384 {
3385 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003386 drawTexture, drawSubresource, drawArea, drawSize,
3387 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003388 }
3389 else
3390 {
Geoff Lang685806d2013-06-12 11:16:36 -04003391 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003392 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3393 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003394 }
3395 }
3396
3397 SafeRelease(readTexture);
3398 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003399
3400 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003401}
3402
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003403ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3404{
3405 D3D11_TEXTURE2D_DESC textureDesc;
3406 source->GetDesc(&textureDesc);
3407
3408 if (textureDesc.SampleDesc.Count > 1)
3409 {
3410 D3D11_TEXTURE2D_DESC resolveDesc;
3411 resolveDesc.Width = textureDesc.Width;
3412 resolveDesc.Height = textureDesc.Height;
3413 resolveDesc.MipLevels = 1;
3414 resolveDesc.ArraySize = 1;
3415 resolveDesc.Format = textureDesc.Format;
3416 resolveDesc.SampleDesc.Count = 1;
3417 resolveDesc.SampleDesc.Quality = 0;
3418 resolveDesc.Usage = textureDesc.Usage;
3419 resolveDesc.BindFlags = textureDesc.BindFlags;
3420 resolveDesc.CPUAccessFlags = 0;
3421 resolveDesc.MiscFlags = 0;
3422
3423 ID3D11Texture2D *resolveTexture = NULL;
3424 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3425 if (FAILED(result))
3426 {
3427 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3428 return NULL;
3429 }
3430
3431 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3432 return resolveTexture;
3433 }
3434 else
3435 {
3436 source->AddRef();
3437 return source;
3438 }
3439}
3440
Geoff Lang42477a42013-09-17 17:07:02 -04003441void Renderer11::invalidateRenderbufferSwizzles(gl::Renderbuffer *renderBuffer, int mipLevel)
3442{
3443 TextureStorage *texStorage = renderBuffer->getTextureStorage();
3444 if (texStorage)
3445 {
3446 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3447 if (!texStorage11)
3448 {
3449 ERR("texture storage pointer unexpectedly null.");
3450 return;
3451 }
3452
3453 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3454 }
3455}
3456
3457void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3458{
3459 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3460 {
3461 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
3462 if (colorbuffer)
3463 {
3464 invalidateRenderbufferSwizzles(colorbuffer, framebuffer->getColorbufferMipLevel(colorAttachment));
3465 }
3466 }
3467
3468 gl::Renderbuffer *depthBuffer = framebuffer->getDepthbuffer();
3469 if (depthBuffer)
3470 {
3471 invalidateRenderbufferSwizzles(depthBuffer, framebuffer->getDepthbufferMipLevel());
3472 }
3473
3474 gl::Renderbuffer *stencilBuffer = framebuffer->getStencilbuffer();
3475 if (stencilBuffer)
3476 {
3477 invalidateRenderbufferSwizzles(stencilBuffer, framebuffer->getStencilbufferMipLevel());
3478 }
3479}
3480
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003481bool Renderer11::getLUID(LUID *adapterLuid) const
3482{
3483 adapterLuid->HighPart = 0;
3484 adapterLuid->LowPart = 0;
3485
3486 if (!mDxgiAdapter)
3487 {
3488 return false;
3489 }
3490
3491 DXGI_ADAPTER_DESC adapterDesc;
3492 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3493 {
3494 return false;
3495 }
3496
3497 *adapterLuid = adapterDesc.AdapterLuid;
3498 return true;
3499}
3500
Geoff Lang005df412013-10-16 14:12:50 -04003501GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003502{
3503 int clientVersion = getCurrentClientVersion();
3504 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3505}
3506
Geoff Lang61e49a52013-05-29 10:22:58 -04003507Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3508{
3509 MultisampleSupportInfo supportInfo = { 0 };
3510
3511 UINT formatSupport;
3512 HRESULT result;
3513
3514 result = mDevice->CheckFormatSupport(format, &formatSupport);
3515 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3516 {
3517 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3518 {
3519 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3520 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3521 {
3522 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3523 }
3524 else
3525 {
3526 supportInfo.qualityLevels[i - 1] = 0;
3527 }
3528 }
3529 }
3530
3531 return supportInfo;
3532}
3533
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003534}