blob: 17c122d854566a77b5d9d5b912809fe0c9c8d06c [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
Jamie Madill6246dc82014-01-29 09:26:47 -050094 mAppliedVertexShader = NULL;
95 mAppliedGeometryShader = NULL;
96 mAppliedPixelShader = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000097}
98
99Renderer11::~Renderer11()
100{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000101 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000102}
103
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000104Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
105{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000106 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000107 return static_cast<rx::Renderer11*>(renderer);
108}
109
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000110#ifndef __d3d11_1_h__
111#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
112#endif
113
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000114EGLint Renderer11::initialize()
115{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000116 if (!initializeCompiler())
117 {
118 return EGL_NOT_INITIALIZED;
119 }
120
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000121 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
122 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000123
124 if (mD3d11Module == NULL || mDxgiModule == NULL)
125 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000126 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000127 return EGL_NOT_INITIALIZED;
128 }
129
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000130 // create the D3D11 device
131 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000132 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000133
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000134 if (D3D11CreateDevice == NULL)
135 {
136 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
137 return EGL_NOT_INITIALIZED;
138 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000139
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000140 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000141 {
142 D3D_FEATURE_LEVEL_11_0,
143 D3D_FEATURE_LEVEL_10_1,
144 D3D_FEATURE_LEVEL_10_0,
145 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000146
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000147 HRESULT result = S_OK;
148
149#ifdef _DEBUG
150 result = D3D11CreateDevice(NULL,
151 D3D_DRIVER_TYPE_HARDWARE,
152 NULL,
153 D3D11_CREATE_DEVICE_DEBUG,
154 featureLevels,
155 ArraySize(featureLevels),
156 D3D11_SDK_VERSION,
157 &mDevice,
158 &mFeatureLevel,
159 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000160
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000161 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000162 {
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000163 ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n");
164 }
165
166 if (!mDevice || FAILED(result))
167#endif
168 {
169 result = D3D11CreateDevice(NULL,
170 D3D_DRIVER_TYPE_HARDWARE,
171 NULL,
172 0,
173 featureLevels,
174 ArraySize(featureLevels),
175 D3D11_SDK_VERSION,
176 &mDevice,
177 &mFeatureLevel,
178 &mDeviceContext);
179
180 if (!mDevice || FAILED(result))
181 {
182 ERR("Could not create D3D11 device - aborting!\n");
183 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
184 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000185 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000186
187 IDXGIDevice *dxgiDevice = NULL;
188 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
189
190 if (FAILED(result))
191 {
192 ERR("Could not query DXGI device - aborting!\n");
193 return EGL_NOT_INITIALIZED;
194 }
195
196 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
197
198 if (FAILED(result))
199 {
200 ERR("Could not retrieve DXGI adapter - aborting!\n");
201 return EGL_NOT_INITIALIZED;
202 }
203
Geoff Langea228632013-07-30 15:17:12 -0400204 SafeRelease(dxgiDevice);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000205
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000206 mDxgiAdapter->GetDesc(&mAdapterDescription);
207 memset(mDescription, 0, sizeof(mDescription));
208 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
209
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000210 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
211
212 if (!mDxgiFactory || FAILED(result))
213 {
214 ERR("Could not create DXGI factory - aborting!\n");
215 return EGL_NOT_INITIALIZED;
216 }
217
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000218 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
219#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
220 ID3D11InfoQueue *infoQueue;
221 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
222
223 if (SUCCEEDED(result))
224 {
225 D3D11_MESSAGE_ID hideMessages[] =
226 {
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000227 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000228 };
229
230 D3D11_INFO_QUEUE_FILTER filter = {0};
231 filter.DenyList.NumIDs = ArraySize(hideMessages);
232 filter.DenyList.pIDList = hideMessages;
233
234 infoQueue->AddStorageFilterEntries(&filter);
Geoff Langea228632013-07-30 15:17:12 -0400235 SafeRelease(infoQueue);
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000236 }
237#endif
238
Geoff Lang61e49a52013-05-29 10:22:58 -0400239 mMaxSupportedSamples = 0;
240
241 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
242 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000243 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400244 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
245 mMultisampleSupportMap.insert(std::make_pair(*i, support));
246 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000247 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000248
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000249 initializeDevice();
250
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000251 // BGRA texture support is optional in feature levels 10 and 10_1
252 UINT formatSupport;
253 result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport);
254 if (FAILED(result))
255 {
256 ERR("Error checking BGRA format support: 0x%08X", result);
257 }
258 else
259 {
260 const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET);
261 mBGRATextureSupport = (formatSupport & flags) == flags;
262 }
263
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000264 // Check floating point texture support
265 static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE;
266 static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET;
267 static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE;
268
269 DXGI_FORMAT float16Formats[] =
270 {
271 DXGI_FORMAT_R16_FLOAT,
272 DXGI_FORMAT_R16G16_FLOAT,
273 DXGI_FORMAT_R16G16B16A16_FLOAT,
274 };
275
276 DXGI_FORMAT float32Formats[] =
277 {
278 DXGI_FORMAT_R32_FLOAT,
279 DXGI_FORMAT_R32G32_FLOAT,
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000280 DXGI_FORMAT_R32G32B32A32_FLOAT,
281 };
282
283 mFloat16TextureSupport = true;
284 mFloat16FilterSupport = true;
285 mFloat16RenderSupport = true;
286 for (unsigned int i = 0; i < ArraySize(float16Formats); i++)
287 {
288 if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport)))
289 {
290 mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
291 mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
292 mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
293 }
294 else
295 {
296 mFloat16TextureSupport = false;
297 mFloat16RenderSupport = false;
298 mFloat16FilterSupport = false;
299 }
300 }
301
302 mFloat32TextureSupport = true;
303 mFloat32FilterSupport = true;
304 mFloat32RenderSupport = true;
305 for (unsigned int i = 0; i < ArraySize(float32Formats); i++)
306 {
307 if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport)))
308 {
309 mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
310 mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
311 mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
312 }
313 else
314 {
315 mFloat32TextureSupport = false;
316 mFloat32FilterSupport = false;
317 mFloat32RenderSupport = false;
318 }
319 }
320
Geoff Lang632192d2013-10-04 13:40:46 -0400321 DXGI_FORMAT rgTextureFormats[] =
322 {
323 DXGI_FORMAT_R8_UNORM,
324 DXGI_FORMAT_R8G8_UNORM,
325 DXGI_FORMAT_R16_FLOAT,
326 DXGI_FORMAT_R16G16_FLOAT,
327 DXGI_FORMAT_R32_FLOAT,
328 DXGI_FORMAT_R32G32_FLOAT,
329 };
330
331 mRGTextureSupport = true;
332 for (unsigned int i = 0; i < ArraySize(rgTextureFormats); i++)
333 {
334 if (SUCCEEDED(mDevice->CheckFormatSupport(rgTextureFormats[i], &formatSupport)))
335 {
336 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
337 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
338 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
339 }
340 else
341 {
342 mRGTextureSupport = false;
343 }
344 }
345
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +0000346 // Check compressed texture support
347 const unsigned int requiredCompressedTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D;
348
349 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &formatSupport)))
350 {
351 mDXT1TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
352 }
353 else
354 {
355 mDXT1TextureSupport = false;
356 }
357
358 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport)))
359 {
360 mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
361 }
362 else
363 {
364 mDXT3TextureSupport = false;
365 }
366
367 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC5_UNORM, &formatSupport)))
368 {
369 mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
370 }
371 else
372 {
373 mDXT5TextureSupport = false;
374 }
375
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +0000376 // Check depth texture support
377 DXGI_FORMAT depthTextureFormats[] =
378 {
379 DXGI_FORMAT_D16_UNORM,
380 DXGI_FORMAT_D24_UNORM_S8_UINT,
381 };
382
383 static const unsigned int requiredDepthTextureFlags = D3D11_FORMAT_SUPPORT_DEPTH_STENCIL |
384 D3D11_FORMAT_SUPPORT_TEXTURE2D;
385
386 mDepthTextureSupport = true;
387 for (unsigned int i = 0; i < ArraySize(depthTextureFormats); i++)
388 {
389 if (SUCCEEDED(mDevice->CheckFormatSupport(depthTextureFormats[i], &formatSupport)))
390 {
391 mDepthTextureSupport = mDepthTextureSupport && ((formatSupport & requiredDepthTextureFlags) == requiredDepthTextureFlags);
392 }
393 else
394 {
395 mDepthTextureSupport = false;
396 }
397 }
398
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000399 return EGL_SUCCESS;
400}
401
402// do any one-time device initialization
403// NOTE: this is also needed after a device lost/reset
404// to reset the scene status and ensure the default states are reset.
405void Renderer11::initializeDevice()
406{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000407 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000408 mInputLayoutCache.initialize(mDevice, mDeviceContext);
409
410 ASSERT(!mVertexDataManager && !mIndexDataManager);
411 mVertexDataManager = new VertexDataManager(this);
412 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000413
Geoff Langb86b9792013-06-04 16:32:05 -0400414 ASSERT(!mBlit);
415 mBlit = new Blit11(this);
416
Geoff Langda507fe2013-08-20 12:01:42 -0400417 ASSERT(!mClear);
418 mClear = new Clear11(this);
419
Jamie Madilla21eea32013-09-18 14:36:25 -0400420 ASSERT(!mPixelTransfer);
421 mPixelTransfer = new PixelTransfer11(this);
422
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000423 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000424}
425
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000426int Renderer11::generateConfigs(ConfigDesc **configDescList)
427{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000428 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
429 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000430 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
431 int numConfigs = 0;
432
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000433 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000434 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000435 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000436 {
437 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
438
439 UINT formatSupport = 0;
440 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000441
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000442 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
443 {
444 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
445
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000446 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000447
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000448 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
449 {
450 UINT formatSupport = 0;
451 result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
452 depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
453 }
454
455 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000456 {
Jamie Madilld6cb2442013-07-10 15:13:38 -0400457 // FIXME: parse types from context version
458 ASSERT(d3d11_gl::GetInternalFormat(renderTargetFormat, 2) == d3d11_gl::GetInternalFormat(renderTargetFormat, 3));
459 ASSERT(d3d11_gl::GetInternalFormat(depthStencilFormat, 2) == d3d11_gl::GetInternalFormat(depthStencilFormat, 3));
460
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000461 ConfigDesc newConfig;
Jamie Madilld6cb2442013-07-10 15:13:38 -0400462 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat, getCurrentClientVersion());
463 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat, getCurrentClientVersion());
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000464 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
465 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000466 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000467
468 (*configDescList)[numConfigs++] = newConfig;
469 }
470 }
471 }
472 }
473
474 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000475}
476
477void Renderer11::deleteConfigs(ConfigDesc *configDescList)
478{
479 delete [] (configDescList);
480}
481
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000482void Renderer11::sync(bool block)
483{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000484 if (block)
485 {
486 HRESULT result;
487
488 if (!mSyncQuery)
489 {
490 D3D11_QUERY_DESC queryDesc;
491 queryDesc.Query = D3D11_QUERY_EVENT;
492 queryDesc.MiscFlags = 0;
493
494 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
495 ASSERT(SUCCEEDED(result));
496 }
497
498 mDeviceContext->End(mSyncQuery);
499 mDeviceContext->Flush();
500
501 do
502 {
503 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
504
505 // Keep polling, but allow other threads to do something useful first
506 Sleep(0);
507
508 if (testDeviceLost(true))
509 {
510 return;
511 }
512 }
513 while (result == S_FALSE);
514 }
515 else
516 {
517 mDeviceContext->Flush();
518 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000519}
520
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000521SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
522{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000523 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000524}
525
Geoff Lange2e0ce02013-09-17 17:05:08 -0400526void Renderer11::generateSwizzle(gl::Texture *texture)
527{
Geoff Lang42477a42013-09-17 17:07:02 -0400528 if (texture)
529 {
530 TextureStorageInterface *texStorage = texture->getNativeTexture();
531 if (texStorage)
532 {
533 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
534
535 storage11->generateSwizzles(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
536 texture->getSwizzleAlpha());
537 }
538 }
Geoff Lange2e0ce02013-09-17 17:05:08 -0400539}
540
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000541void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
542{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000543 if (type == gl::SAMPLER_PIXEL)
544 {
545 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
546 {
547 ERR("Pixel shader sampler index %i is not valid.", index);
548 return;
549 }
550
551 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
552 {
553 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
554
555 if (!dxSamplerState)
556 {
557 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
558 "sampler state for pixel shaders at slot %i.", index);
559 }
560
561 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
562
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000563 mCurPixelSamplerStates[index] = samplerState;
564 }
565
566 mForceSetPixelSamplerStates[index] = false;
567 }
568 else if (type == gl::SAMPLER_VERTEX)
569 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000570 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000571 {
572 ERR("Vertex shader sampler index %i is not valid.", index);
573 return;
574 }
575
576 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
577 {
578 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
579
580 if (!dxSamplerState)
581 {
582 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
583 "sampler state for vertex shaders at slot %i.", index);
584 }
585
586 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
587
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000588 mCurVertexSamplerStates[index] = samplerState;
589 }
590
591 mForceSetVertexSamplerStates[index] = false;
592 }
593 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000594}
595
596void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
597{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000598 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000599 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000600
601 if (texture)
602 {
603 TextureStorageInterface *texStorage = texture->getNativeTexture();
604 if (texStorage)
605 {
606 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
Geoff Lang644bbf22013-09-17 17:02:43 -0400607 textureSRV = storage11->getSRV(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
608 texture->getSwizzleAlpha());
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000609 }
610
611 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
612 // missing the shader resource view
613 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000614
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000615 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
Geoff Lang91382e52014-01-07 16:16:30 -0500626 if (forceSetTexture || mCurPixelSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000627 {
628 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
629 }
630
Geoff Lang91382e52014-01-07 16:16:30 -0500631 mCurPixelSRVs[index] = textureSRV;
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
Geoff Lang91382e52014-01-07 16:16:30 -0500641 if (forceSetTexture || mCurVertexSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000642 {
643 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
644 }
645
Geoff Lang91382e52014-01-07 16:16:30 -0500646 mCurVertexSRVs[index] = textureSRV;
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());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500663 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
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());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500685 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
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 Langc142e9d2013-09-30 15:19:47 -0400726void Renderer11::setBlendState(gl::Framebuffer *framebuffer, 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 {
Geoff Langc142e9d2013-09-30 15:19:47 -0400734 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(framebuffer, blendState);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000735 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
Jamie Madillec91cd32014-01-21 16:38:12 -0500791 // Max D3D11 stencil reference value is 0xFF, corresponding to the max 8 bits in a stencil buffer
792 // GL specifies we should clamp the ref value to the nearest bit depth when doing stencil ops
793 META_ASSERT(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF);
794 META_ASSERT(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF);
795 UINT stencilRef = std::min(stencilRef, 0xFFu);
796
797 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, stencilRef);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000798
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000799 mCurDepthStencilState = depthStencilState;
800 mCurStencilRef = stencilRef;
801 mCurStencilBackRef = stencilBackRef;
802 }
803
804 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000805}
806
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000807void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000808{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000809 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
810 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000811 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000812 if (enabled)
813 {
814 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000815 rect.left = std::max(0, scissor.x);
816 rect.top = std::max(0, scissor.y);
817 rect.right = scissor.x + std::max(0, scissor.width);
818 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000819
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000820 mDeviceContext->RSSetScissorRects(1, &rect);
821 }
822
823 if (enabled != mScissorEnabled)
824 {
825 mForceSetRasterState = true;
826 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000827
828 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000829 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000830 }
831
832 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000833}
834
daniel@transgaming.com12985182012-12-20 20:56:31 +0000835bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000836 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000837{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000838 gl::Rectangle actualViewport = viewport;
839 float actualZNear = gl::clamp01(zNear);
840 float actualZFar = gl::clamp01(zFar);
841 if (ignoreViewport)
842 {
843 actualViewport.x = 0;
844 actualViewport.y = 0;
845 actualViewport.width = mRenderTargetDesc.width;
846 actualViewport.height = mRenderTargetDesc.height;
847 actualZNear = 0.0f;
848 actualZFar = 1.0f;
849 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000850
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000851 // Get D3D viewport bounds, which depends on the feature level
852 const Range& viewportBounds = getViewportBounds();
853
854 // 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 +0000855 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000856 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
857 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
858 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
859 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
860 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
861 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000862 dxViewport.MinDepth = actualZNear;
863 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000864
865 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
866 {
867 return false; // Nothing to render
868 }
869
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000870 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
871 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000872
daniel@transgaming.com53670042012-11-28 20:55:51 +0000873 if (viewportChanged)
874 {
875 mDeviceContext->RSSetViewports(1, &dxViewport);
876
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000877 mCurViewport = actualViewport;
878 mCurNear = actualZNear;
879 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000880
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000881 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
882 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
883 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
884 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000885
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000886 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
887 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000888
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000889 mVertexConstants.depthRange[0] = actualZNear;
890 mVertexConstants.depthRange[1] = actualZFar;
891 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000892
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000893 mPixelConstants.depthRange[0] = actualZNear;
894 mPixelConstants.depthRange[1] = actualZFar;
895 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000896 }
897
898 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000899 return true;
900}
901
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000902bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
903{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000904 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000905
Geoff Lang57e713e2013-07-31 17:01:58 -0400906 GLsizei minCount = 0;
907
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000908 switch (mode)
909 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400910 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
911 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
912 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
913 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
914 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
915 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000916 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400917 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000918 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000919 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000920 }
921
Geoff Lang4c095862013-07-22 10:43:36 -0400922 if (primitiveTopology != mCurrentPrimitiveTopology)
923 {
924 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
925 mCurrentPrimitiveTopology = primitiveTopology;
926 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000927
Geoff Lang57e713e2013-07-31 17:01:58 -0400928 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000929}
930
931bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000932{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000933 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000934 // Also extract the render target dimensions and view
935 unsigned int renderTargetWidth = 0;
936 unsigned int renderTargetHeight = 0;
937 GLenum renderTargetFormat = 0;
938 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
939 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
940 bool missingColorRenderTarget = true;
941
942 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000943 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000944 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
945
946 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000947 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000948 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
949 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
950
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000951 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000952
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000953 if (!colorbuffer)
954 {
955 ERR("render target pointer unexpectedly null.");
956 return false;
957 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000958
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000959 // check for zero-sized default framebuffer, which is a special case.
960 // in this case we do not wish to modify any state and just silently return false.
961 // this will not report any gl error but will cause the calling method to return.
962 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
963 {
964 return false;
965 }
966
967 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
968
969 // Extract the render target dimensions and view
970 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
971 if (!renderTarget)
972 {
973 ERR("render target pointer unexpectedly null.");
974 return false;
975 }
976
977 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
978 if (!framebufferRTVs[colorAttachment])
979 {
980 ERR("render target view pointer unexpectedly null.");
981 return false;
982 }
983
984 if (missingColorRenderTarget)
985 {
986 renderTargetWidth = colorbuffer->getWidth();
987 renderTargetHeight = colorbuffer->getHeight();
988 renderTargetFormat = colorbuffer->getActualFormat();
989 missingColorRenderTarget = false;
990 }
Jamie Madillba597af2013-10-22 13:12:15 -0400991
Geoff Lang91382e52014-01-07 16:16:30 -0500992 // TODO: Detect if this color buffer is already bound as a texture and unbind it first to prevent
993 // D3D11 warnings.
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000994 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000995 }
996
997 // Get the depth stencil render buffer and serials
998 gl::Renderbuffer *depthStencil = NULL;
999 unsigned int depthbufferSerial = 0;
1000 unsigned int stencilbufferSerial = 0;
1001 if (framebuffer->getDepthbufferType() != GL_NONE)
1002 {
1003 depthStencil = framebuffer->getDepthbuffer();
1004 if (!depthStencil)
1005 {
1006 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001007 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001008 return false;
1009 }
1010
1011 depthbufferSerial = depthStencil->getSerial();
1012 }
1013 else if (framebuffer->getStencilbufferType() != GL_NONE)
1014 {
1015 depthStencil = framebuffer->getStencilbuffer();
1016 if (!depthStencil)
1017 {
1018 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001019 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001020 return false;
1021 }
1022
1023 stencilbufferSerial = depthStencil->getSerial();
1024 }
1025
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001026 // Extract the depth stencil sizes and view
1027 unsigned int depthSize = 0;
1028 unsigned int stencilSize = 0;
1029 ID3D11DepthStencilView* framebufferDSV = NULL;
1030 if (depthStencil)
1031 {
1032 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
1033 if (!depthStencilRenderTarget)
1034 {
1035 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001036 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001037 return false;
1038 }
1039
1040 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
1041 if (!framebufferDSV)
1042 {
1043 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001044 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001045 return false;
1046 }
1047
1048 // If there is no render buffer, the width, height and format values come from
1049 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001050 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001051 {
1052 renderTargetWidth = depthStencil->getWidth();
1053 renderTargetHeight = depthStencil->getHeight();
1054 renderTargetFormat = depthStencil->getActualFormat();
1055 }
1056
1057 depthSize = depthStencil->getDepthSize();
1058 stencilSize = depthStencil->getStencilSize();
1059 }
1060
1061 // Apply the render target and depth stencil
1062 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001063 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001064 depthbufferSerial != mAppliedDepthbufferSerial ||
1065 stencilbufferSerial != mAppliedStencilbufferSerial)
1066 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001067 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001068
1069 mRenderTargetDesc.width = renderTargetWidth;
1070 mRenderTargetDesc.height = renderTargetHeight;
1071 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001072 mForceSetViewport = true;
1073 mForceSetScissor = true;
Geoff Langc142e9d2013-09-30 15:19:47 -04001074 mForceSetBlendState = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001075
1076 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1077 {
1078 mCurDepthSize = depthSize;
1079 mForceSetRasterState = true;
1080 }
1081
1082 mCurStencilSize = stencilSize;
1083
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001084 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1085 {
1086 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
1087 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001088 mAppliedDepthbufferSerial = depthbufferSerial;
1089 mAppliedStencilbufferSerial = stencilbufferSerial;
1090 mRenderTargetDescInitialized = true;
1091 mDepthStencilInitialized = true;
1092 }
1093
Geoff Lang42477a42013-09-17 17:07:02 -04001094 invalidateFramebufferSwizzles(framebuffer);
1095
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001096 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001097}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001098
Jamie Madill57a89722013-07-02 11:57:03 -04001099GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001100 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001101{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001102 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001103 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001104 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001105 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001106 return err;
1107 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001108
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001109 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001110}
1111
daniel@transgaming.com31240482012-11-28 21:06:41 +00001112GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001113{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001114 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001115
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001116 if (err == GL_NO_ERROR)
1117 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001118 if (indexInfo->storage)
1119 {
1120 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1121 {
1122 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1123 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1124
Geoff Lang9c53f1e2014-01-08 13:41:47 -05001125 mDeviceContext->IASetIndexBuffer(storage->getBuffer(BUFFER_USAGE_INDEX), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001126
1127 mAppliedIBSerial = 0;
1128 mAppliedStorageIBSerial = storage->getSerial();
1129 mAppliedIBOffset = indexInfo->startOffset;
1130 }
1131 }
1132 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001133 {
1134 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1135
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001136 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001137
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001138 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001139 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001140 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001141 }
1142 }
1143
1144 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001145}
1146
1147void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1148{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001149 if (mode == GL_LINE_LOOP)
1150 {
1151 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1152 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001153 else if (mode == GL_TRIANGLE_FAN)
1154 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001155 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001156 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001157 else if (instances > 0)
1158 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001159 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001160 }
1161 else
1162 {
1163 mDeviceContext->Draw(count, 0);
1164 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001165}
1166
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001167void 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 +00001168{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001169 if (mode == GL_LINE_LOOP)
1170 {
1171 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1172 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001173 else if (mode == GL_TRIANGLE_FAN)
1174 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001175 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1176 }
1177 else if (instances > 0)
1178 {
1179 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001180 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001181 else
1182 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001183 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001184 }
1185}
1186
1187void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1188{
1189 // Get the raw indices for an indexed draw
1190 if (type != GL_NONE && elementArrayBuffer)
1191 {
1192 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001193 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001194 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001195 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001196 }
1197
1198 if (!mLineLoopIB)
1199 {
1200 mLineLoopIB = new StreamingIndexBufferInterface(this);
1201 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1202 {
1203 delete mLineLoopIB;
1204 mLineLoopIB = NULL;
1205
1206 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001207 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001208 }
1209 }
1210
Geoff Lang57e713e2013-07-31 17:01:58 -04001211 // Checked by Renderer11::applyPrimitiveType
1212 ASSERT(count >= 0);
1213
1214 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001215 {
1216 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1217 return gl::error(GL_OUT_OF_MEMORY);
1218 }
1219
Geoff Lang57e713e2013-07-31 17:01:58 -04001220 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001221 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1222 {
1223 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001224 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001225 }
1226
1227 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001228 unsigned int offset;
1229 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001230 {
1231 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001232 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001233 }
1234
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001235 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001236 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001237
1238 switch (type)
1239 {
1240 case GL_NONE: // Non-indexed draw
1241 for (int i = 0; i < count; i++)
1242 {
1243 data[i] = i;
1244 }
1245 data[count] = 0;
1246 break;
1247 case GL_UNSIGNED_BYTE:
1248 for (int i = 0; i < count; i++)
1249 {
1250 data[i] = static_cast<const GLubyte*>(indices)[i];
1251 }
1252 data[count] = static_cast<const GLubyte*>(indices)[0];
1253 break;
1254 case GL_UNSIGNED_SHORT:
1255 for (int i = 0; i < count; i++)
1256 {
1257 data[i] = static_cast<const GLushort*>(indices)[i];
1258 }
1259 data[count] = static_cast<const GLushort*>(indices)[0];
1260 break;
1261 case GL_UNSIGNED_INT:
1262 for (int i = 0; i < count; i++)
1263 {
1264 data[i] = static_cast<const GLuint*>(indices)[i];
1265 }
1266 data[count] = static_cast<const GLuint*>(indices)[0];
1267 break;
1268 default: UNREACHABLE();
1269 }
1270
1271 if (!mLineLoopIB->unmapBuffer())
1272 {
1273 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001274 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001275 }
1276
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001277 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001278 {
1279 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1280
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001281 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001282 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001283 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001284 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001285 }
1286
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001287 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001288}
1289
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001290void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001291{
1292 // Get the raw indices for an indexed draw
1293 if (type != GL_NONE && elementArrayBuffer)
1294 {
1295 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001296 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001297 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001298 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001299 }
1300
1301 if (!mTriangleFanIB)
1302 {
1303 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1304 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1305 {
1306 delete mTriangleFanIB;
1307 mTriangleFanIB = NULL;
1308
1309 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001310 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001311 }
1312 }
1313
Geoff Lang57e713e2013-07-31 17:01:58 -04001314 // Checked by Renderer11::applyPrimitiveType
1315 ASSERT(count >= 3);
1316
Geoff Langeadfd572013-07-09 15:55:07 -04001317 const unsigned int numTris = count - 2;
1318
Geoff Lang57e713e2013-07-31 17:01:58 -04001319 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001320 {
1321 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1322 return gl::error(GL_OUT_OF_MEMORY);
1323 }
1324
1325 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001326 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1327 {
1328 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001329 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001330 }
1331
1332 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001333 unsigned int offset;
1334 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001335 {
1336 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001337 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001338 }
1339
1340 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001341 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001342
1343 switch (type)
1344 {
1345 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001346 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001347 {
1348 data[i*3 + 0] = 0;
1349 data[i*3 + 1] = i + 1;
1350 data[i*3 + 2] = i + 2;
1351 }
1352 break;
1353 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001354 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001355 {
1356 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1357 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1358 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1359 }
1360 break;
1361 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001362 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001363 {
1364 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1365 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1366 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1367 }
1368 break;
1369 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001370 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001371 {
1372 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1373 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1374 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1375 }
1376 break;
1377 default: UNREACHABLE();
1378 }
1379
1380 if (!mTriangleFanIB->unmapBuffer())
1381 {
1382 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001383 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001384 }
1385
1386 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1387 {
1388 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1389
1390 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1391 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001392 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001393 mAppliedIBOffset = indexBufferOffset;
1394 }
1395
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001396 if (instances > 0)
1397 {
1398 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1399 }
1400 else
1401 {
1402 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1403 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001404}
1405
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001406void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1407{
Jamie Madill6246dc82014-01-29 09:26:47 -05001408 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1409 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
1410 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001411
Jamie Madill6246dc82014-01-29 09:26:47 -05001412 ID3D11VertexShader *vertexShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader() : NULL);
1413 ID3D11PixelShader *pixelShader = (pixelExe ? ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader() : NULL);
1414 ID3D11GeometryShader *geometryShader = (geometryExe ? ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader() : NULL);
1415
1416 // Skip GS if we aren't drawing points
1417 if (!mCurRasterState.pointDrawMode)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001418 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001419 geometryShader = NULL;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001420 }
1421
Jamie Madill6246dc82014-01-29 09:26:47 -05001422 bool dirtyUniforms = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001423
Jamie Madill6246dc82014-01-29 09:26:47 -05001424 if (vertexShader != mAppliedVertexShader)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001425 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001426 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1427 mAppliedVertexShader = vertexShader;
1428 dirtyUniforms = true;
1429 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001430
Jamie Madill6246dc82014-01-29 09:26:47 -05001431 if (geometryShader != mAppliedGeometryShader)
1432 {
1433 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1434 mAppliedGeometryShader = geometryShader;
1435 dirtyUniforms = true;
1436 }
1437
1438 if (pixelShader != mAppliedPixelShader)
1439 {
1440 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1441 mAppliedPixelShader = pixelShader;
1442 dirtyUniforms = true;
1443 }
1444
1445 if (dirtyUniforms)
1446 {
1447 programBinary->dirtyAllUniforms();
daniel@transgaming.come4991412012-12-20 20:55:34 +00001448 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001449}
1450
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001451void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001452{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001453 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1454 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001455
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001456 unsigned int totalRegisterCountVS = 0;
1457 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001458
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001459 bool vertexUniformsDirty = false;
1460 bool pixelUniformsDirty = false;
1461
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001462 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1463 {
1464 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001465
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001466 if (uniform->isReferencedByVertexShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001467 {
1468 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001469 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001470 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001471
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001472 if (uniform->isReferencedByFragmentShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001473 {
1474 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001475 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001476 }
1477 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001478
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001479 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1480 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1481
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001482 float (*mapVS)[4] = NULL;
1483 float (*mapPS)[4] = NULL;
1484
Shannon Woods5ab33c82013-06-26 15:31:09 -04001485 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1486 {
1487 D3D11_MAPPED_SUBRESOURCE map = {0};
1488 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1489 ASSERT(SUCCEEDED(result));
1490 mapVS = (float(*)[4])map.pData;
1491 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001492
Shannon Woods5ab33c82013-06-26 15:31:09 -04001493 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1494 {
1495 D3D11_MAPPED_SUBRESOURCE map = {0};
1496 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1497 ASSERT(SUCCEEDED(result));
1498 mapPS = (float(*)[4])map.pData;
1499 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001500
Jamie Madill5b085dc2013-08-30 13:21:11 -04001501 for (size_t uniformIndex = 0; uniformIndex < uniformArray->size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001502 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001503 gl::Uniform *uniform = (*uniformArray)[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001504
Nicolas Capense6050882013-07-08 10:43:10 -04001505 if (!gl::IsSampler(uniform->type))
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001506 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001507 unsigned int componentCount = (4 - uniform->registerElement);
1508
Jamie Madill71cc91f2013-09-18 12:51:22 -04001509 // 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 -04001510 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001511
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001512 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001513 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001514 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001515 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001516
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001517 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001518 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001519 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001520 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001521 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001522
1523 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001524 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001525
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001526 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001527 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001528 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001529 }
1530
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001531 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001532 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001533 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001534 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001535
1536 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1537 {
1538 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1539 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1540 }
1541
1542 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1543 {
1544 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1545 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1546 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001547
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001548 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001549 if (!mDriverConstantBufferVS)
1550 {
1551 D3D11_BUFFER_DESC constantBufferDescription = {0};
1552 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1553 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1554 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1555 constantBufferDescription.CPUAccessFlags = 0;
1556 constantBufferDescription.MiscFlags = 0;
1557 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001558
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001559 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001560 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001561
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001562 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1563 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001564
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001565 if (!mDriverConstantBufferPS)
1566 {
1567 D3D11_BUFFER_DESC constantBufferDescription = {0};
1568 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1569 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1570 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1571 constantBufferDescription.CPUAccessFlags = 0;
1572 constantBufferDescription.MiscFlags = 0;
1573 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001574
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001575 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001576 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001577
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001578 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1579 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001580
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001581 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1582 {
1583 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1584 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1585 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001586
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001587 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1588 {
1589 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1590 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1591 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001592
1593 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001594 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1595 {
1596 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1597 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1598 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001599}
1600
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001601void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001602{
Geoff Langda507fe2013-08-20 12:01:42 -04001603 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001604 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001605}
1606
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001607void Renderer11::markAllStateDirty()
1608{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001609 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1610 {
1611 mAppliedRenderTargetSerials[rtIndex] = 0;
1612 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001613 mAppliedDepthbufferSerial = 0;
1614 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001615 mDepthStencilInitialized = false;
1616 mRenderTargetDescInitialized = false;
1617
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001618 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001619 {
1620 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001621 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001622 }
1623 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1624 {
1625 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001626 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001627 }
1628
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001629 mForceSetBlendState = true;
1630 mForceSetRasterState = true;
1631 mForceSetDepthStencilState = true;
1632 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001633 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001634
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001635 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001636 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001637 mAppliedIBOffset = 0;
1638
Jamie Madill6246dc82014-01-29 09:26:47 -05001639 mAppliedVertexShader = NULL;
1640 mAppliedGeometryShader = NULL;
1641 mAppliedPixelShader = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001642 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1643 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001644
1645 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001646
1647 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1648 {
1649 mCurrentConstantBufferVS[i] = -1;
1650 mCurrentConstantBufferPS[i] = -1;
1651 }
1652
1653 mCurrentVertexConstantBuffer = NULL;
1654 mCurrentPixelConstantBuffer = NULL;
1655 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001656
1657 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001658}
1659
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001660void Renderer11::releaseDeviceResources()
1661{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001662 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001663 mInputLayoutCache.clear();
1664
Geoff Langea228632013-07-30 15:17:12 -04001665 SafeDelete(mVertexDataManager);
1666 SafeDelete(mIndexDataManager);
1667 SafeDelete(mLineLoopIB);
1668 SafeDelete(mTriangleFanIB);
1669 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001670 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001671 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001672
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001673 SafeRelease(mDriverConstantBufferVS);
1674 SafeRelease(mDriverConstantBufferPS);
1675 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001676}
1677
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001678void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001679{
1680 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001681 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001682}
1683
1684bool Renderer11::isDeviceLost()
1685{
1686 return mDeviceLost;
1687}
1688
1689// set notify to true to broadcast a message to all contexts of the device loss
1690bool Renderer11::testDeviceLost(bool notify)
1691{
1692 bool isLost = false;
1693
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001694 // GetRemovedReason is used to test if the device is removed
1695 HRESULT result = mDevice->GetDeviceRemovedReason();
1696 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001697
1698 if (isLost)
1699 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001700 // Log error if this is a new device lost event
1701 if (mDeviceLost == false)
1702 {
1703 ERR("The D3D11 device was removed: 0x%08X", result);
1704 }
1705
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001706 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001707 // we'll probably get this done again by notifyDeviceLost
1708 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001709 // Note that we don't want to clear the device loss status here
1710 // -- this needs to be done by resetDevice
1711 mDeviceLost = true;
1712 if (notify)
1713 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001714 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001715 }
1716 }
1717
1718 return isLost;
1719}
1720
1721bool Renderer11::testDeviceResettable()
1722{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001723 // determine if the device is resettable by creating a dummy device
1724 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001725
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001726 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001727 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001728 return false;
1729 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001730
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001731 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001732 {
1733 D3D_FEATURE_LEVEL_11_0,
1734 D3D_FEATURE_LEVEL_10_1,
1735 D3D_FEATURE_LEVEL_10_0,
1736 };
1737
1738 ID3D11Device* dummyDevice;
1739 D3D_FEATURE_LEVEL dummyFeatureLevel;
1740 ID3D11DeviceContext* dummyContext;
1741
1742 HRESULT result = D3D11CreateDevice(NULL,
1743 D3D_DRIVER_TYPE_HARDWARE,
1744 NULL,
1745 #if defined(_DEBUG)
1746 D3D11_CREATE_DEVICE_DEBUG,
1747 #else
1748 0,
1749 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001750 featureLevels,
1751 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001752 D3D11_SDK_VERSION,
1753 &dummyDevice,
1754 &dummyFeatureLevel,
1755 &dummyContext);
1756
1757 if (!mDevice || FAILED(result))
1758 {
1759 return false;
1760 }
1761
Geoff Langea228632013-07-30 15:17:12 -04001762 SafeRelease(dummyContext);
1763 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001764
1765 return true;
1766}
1767
1768void Renderer11::release()
1769{
1770 releaseDeviceResources();
1771
Geoff Langea228632013-07-30 15:17:12 -04001772 SafeRelease(mDxgiFactory);
1773 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001774
1775 if (mDeviceContext)
1776 {
1777 mDeviceContext->ClearState();
1778 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001779 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001780 }
1781
Geoff Langea228632013-07-30 15:17:12 -04001782 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001783
1784 if (mD3d11Module)
1785 {
1786 FreeLibrary(mD3d11Module);
1787 mD3d11Module = NULL;
1788 }
1789
1790 if (mDxgiModule)
1791 {
1792 FreeLibrary(mDxgiModule);
1793 mDxgiModule = NULL;
1794 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001795}
1796
1797bool Renderer11::resetDevice()
1798{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001799 // recreate everything
1800 release();
1801 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001802
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001803 if (result != EGL_SUCCESS)
1804 {
1805 ERR("Could not reinitialize D3D11 device: %08X", result);
1806 return false;
1807 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001808
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001809 mDeviceLost = false;
1810
1811 return true;
1812}
1813
1814DWORD Renderer11::getAdapterVendor() const
1815{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001816 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001817}
1818
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001819std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001820{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001821 std::ostringstream rendererString;
1822
1823 rendererString << mDescription;
1824 rendererString << " Direct3D11";
1825
1826 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1827 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1828
1829 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001830}
1831
1832GUID Renderer11::getAdapterIdentifier() const
1833{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001834 // Use the adapter LUID as our adapter ID
1835 // This number is local to a machine is only guaranteed to be unique between restarts
1836 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1837 GUID adapterId = {0};
1838 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1839 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001840}
1841
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001842bool Renderer11::getBGRATextureSupport() const
1843{
1844 return mBGRATextureSupport;
1845}
1846
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001847bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001848{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001849 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001850}
1851
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001852bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001853{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001854 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001855}
1856
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001857bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001858{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001859 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001860}
1861
1862bool Renderer11::getDepthTextureSupport() const
1863{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001864 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001865}
1866
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001867bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001868{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001869 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001870}
1871
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001872bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001873{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001874 return mFloat32FilterSupport;
1875}
1876
1877bool Renderer11::getFloat32TextureRenderingSupport() const
1878{
1879 return mFloat32RenderSupport;
1880}
1881
1882bool Renderer11::getFloat16TextureSupport() const
1883{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001884 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001885}
1886
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001887bool Renderer11::getFloat16TextureFilteringSupport() const
1888{
1889 return mFloat16FilterSupport;
1890}
1891
1892bool Renderer11::getFloat16TextureRenderingSupport() const
1893{
1894 return mFloat16RenderSupport;
1895}
1896
Geoff Langd42cf4e2013-06-05 16:09:17 -04001897bool Renderer11::getRGB565TextureSupport() const
1898{
1899 return false;
1900}
1901
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001902bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001903{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001904 return false;
1905}
1906
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001907bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001908{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001909 return false;
1910}
1911
Geoff Lang632192d2013-10-04 13:40:46 -04001912bool Renderer11::getRGTextureSupport() const
1913{
1914 return mRGTextureSupport;
1915}
1916
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001917bool Renderer11::getTextureFilterAnisotropySupport() const
1918{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001919 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001920}
1921
1922float Renderer11::getTextureMaxAnisotropy() const
1923{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001924 switch (mFeatureLevel)
1925 {
1926 case D3D_FEATURE_LEVEL_11_0:
1927 return D3D11_MAX_MAXANISOTROPY;
1928 case D3D_FEATURE_LEVEL_10_1:
1929 case D3D_FEATURE_LEVEL_10_0:
1930 return D3D10_MAX_MAXANISOTROPY;
1931 default: UNREACHABLE();
1932 return 0;
1933 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001934}
1935
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001936bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001937{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00001938 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001939}
1940
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001941Range Renderer11::getViewportBounds() const
1942{
1943 switch (mFeatureLevel)
1944 {
1945 case D3D_FEATURE_LEVEL_11_0:
1946 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1947 case D3D_FEATURE_LEVEL_10_1:
1948 case D3D_FEATURE_LEVEL_10_0:
1949 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1950 default: UNREACHABLE();
1951 return Range(0, 0);
1952 }
1953}
1954
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001955unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001956{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001957 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1958 switch (mFeatureLevel)
1959 {
1960 case D3D_FEATURE_LEVEL_11_0:
1961 case D3D_FEATURE_LEVEL_10_1:
1962 case D3D_FEATURE_LEVEL_10_0:
1963 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1964 default: UNREACHABLE();
1965 return 0;
1966 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001967}
1968
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001969unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1970{
1971 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1972}
1973
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001974unsigned int Renderer11::getReservedVertexUniformVectors() const
1975{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001976 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001977}
1978
1979unsigned int Renderer11::getReservedFragmentUniformVectors() const
1980{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001981 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001982}
1983
1984unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001985{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001986 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1987 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1988 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001989}
1990
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001991unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001992{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001993 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1994 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1995 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001996}
1997
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001998unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001999{
2000 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00002001 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
2002 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002003 switch (mFeatureLevel)
2004 {
2005 case D3D_FEATURE_LEVEL_11_0:
2006 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2007 case D3D_FEATURE_LEVEL_10_1:
2008 case D3D_FEATURE_LEVEL_10_0:
2009 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2010 default: UNREACHABLE();
2011 return 0;
2012 }
2013}
2014
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002015unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2016{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002017 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2018 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2019
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002020 switch (mFeatureLevel)
2021 {
2022 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002023 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002024 case D3D_FEATURE_LEVEL_10_1:
2025 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002026 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002027 default: UNREACHABLE();
2028 return 0;
2029 }
2030}
2031
2032unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2033{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002034 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2035 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2036
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002037 switch (mFeatureLevel)
2038 {
2039 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002040 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002041 case D3D_FEATURE_LEVEL_10_1:
2042 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002043 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002044 default: UNREACHABLE();
2045 return 0;
2046 }
2047}
2048
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002049unsigned int Renderer11::getReservedVertexUniformBuffers() const
2050{
2051 // we reserve one buffer for the application uniforms, and one for driver uniforms
2052 return 2;
2053}
2054
2055unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2056{
2057 // we reserve one buffer for the application uniforms, and one for driver uniforms
2058 return 2;
2059}
2060
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002061unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2062{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002063 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2064 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2065
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002066 switch (mFeatureLevel)
2067 {
2068 case D3D_FEATURE_LEVEL_11_0:
2069 return D3D11_SO_BUFFER_SLOT_COUNT;
2070 case D3D_FEATURE_LEVEL_10_1:
2071 case D3D_FEATURE_LEVEL_10_0:
2072 return D3D10_SO_BUFFER_SLOT_COUNT;
2073 default: UNREACHABLE();
2074 return 0;
2075 }
2076}
2077
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002078unsigned int Renderer11::getMaxUniformBufferSize() const
2079{
2080 // Each component is a 4-element vector of 4-byte units (floats)
2081 const unsigned int bytesPerComponent = 4 * sizeof(float);
2082
2083 switch (mFeatureLevel)
2084 {
2085 case D3D_FEATURE_LEVEL_11_0:
2086 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2087 case D3D_FEATURE_LEVEL_10_1:
2088 case D3D_FEATURE_LEVEL_10_0:
2089 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2090 default: UNREACHABLE();
2091 return 0;
2092 }
2093}
2094
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002095bool Renderer11::getNonPower2TextureSupport() const
2096{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002097 switch (mFeatureLevel)
2098 {
2099 case D3D_FEATURE_LEVEL_11_0:
2100 case D3D_FEATURE_LEVEL_10_1:
2101 case D3D_FEATURE_LEVEL_10_0:
2102 return true;
2103 default: UNREACHABLE();
2104 return false;
2105 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002106}
2107
2108bool Renderer11::getOcclusionQuerySupport() const
2109{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002110 switch (mFeatureLevel)
2111 {
2112 case D3D_FEATURE_LEVEL_11_0:
2113 case D3D_FEATURE_LEVEL_10_1:
2114 case D3D_FEATURE_LEVEL_10_0:
2115 return true;
2116 default: UNREACHABLE();
2117 return false;
2118 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002119}
2120
2121bool Renderer11::getInstancingSupport() const
2122{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002123 switch (mFeatureLevel)
2124 {
2125 case D3D_FEATURE_LEVEL_11_0:
2126 case D3D_FEATURE_LEVEL_10_1:
2127 case D3D_FEATURE_LEVEL_10_0:
2128 return true;
2129 default: UNREACHABLE();
2130 return false;
2131 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002132}
2133
2134bool Renderer11::getShareHandleSupport() const
2135{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002136 // We only currently support share handles with BGRA surfaces, because
2137 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002138 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002139 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002140}
2141
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002142bool Renderer11::getDerivativeInstructionSupport() const
2143{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002144 switch (mFeatureLevel)
2145 {
2146 case D3D_FEATURE_LEVEL_11_0:
2147 case D3D_FEATURE_LEVEL_10_1:
2148 case D3D_FEATURE_LEVEL_10_0:
2149 return true;
2150 default: UNREACHABLE();
2151 return false;
2152 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002153}
2154
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002155bool Renderer11::getPostSubBufferSupport() const
2156{
2157 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2158 return false;
2159}
2160
Jamie Madill13a2f852013-12-11 16:35:08 -05002161int Renderer11::getMaxRecommendedElementsIndices() const
2162{
2163 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2164 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2165
2166 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2167 return std::numeric_limits<GLint>::max();
2168}
2169
2170int Renderer11::getMaxRecommendedElementsVertices() const
2171{
2172 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2173 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2174
2175 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2176 return std::numeric_limits<GLint>::max();
2177}
2178
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002179int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002180{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002181 switch (mFeatureLevel)
2182 {
2183 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002184 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002185 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2186 default: UNREACHABLE(); return 0;
2187 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002188}
2189
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002190int Renderer11::getMinorShaderModel() const
2191{
2192 switch (mFeatureLevel)
2193 {
2194 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2195 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2196 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2197 default: UNREACHABLE(); return 0;
2198 }
2199}
2200
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002201float Renderer11::getMaxPointSize() const
2202{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002203 // choose a reasonable maximum. we enforce this in the shader.
2204 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2205 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002206}
2207
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002208int Renderer11::getMaxViewportDimension() const
2209{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002210 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2211 // In our case return the maximum texture size, which is the maximum render buffer size.
2212 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2213 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2214
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002215 switch (mFeatureLevel)
2216 {
2217 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002218 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002219 case D3D_FEATURE_LEVEL_10_1:
2220 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002221 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002222 default: UNREACHABLE();
2223 return 0;
2224 }
2225}
2226
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002227int Renderer11::getMaxTextureWidth() const
2228{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002229 switch (mFeatureLevel)
2230 {
2231 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2232 case D3D_FEATURE_LEVEL_10_1:
2233 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2234 default: UNREACHABLE(); return 0;
2235 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002236}
2237
2238int Renderer11::getMaxTextureHeight() const
2239{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002240 switch (mFeatureLevel)
2241 {
2242 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2243 case D3D_FEATURE_LEVEL_10_1:
2244 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2245 default: UNREACHABLE(); return 0;
2246 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002247}
2248
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002249int Renderer11::getMaxTextureDepth() const
2250{
2251 switch (mFeatureLevel)
2252 {
2253 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2254 case D3D_FEATURE_LEVEL_10_1:
2255 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2256 default: UNREACHABLE(); return 0;
2257 }
2258}
2259
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002260int Renderer11::getMaxTextureArrayLayers() const
2261{
2262 switch (mFeatureLevel)
2263 {
2264 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2265 case D3D_FEATURE_LEVEL_10_1:
2266 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2267 default: UNREACHABLE(); return 0;
2268 }
2269}
2270
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002271bool Renderer11::get32BitIndexSupport() const
2272{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002273 switch (mFeatureLevel)
2274 {
2275 case D3D_FEATURE_LEVEL_11_0:
2276 case D3D_FEATURE_LEVEL_10_1:
2277 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2278 default: UNREACHABLE(); return false;
2279 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002280}
2281
2282int Renderer11::getMinSwapInterval() const
2283{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002284 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002285}
2286
2287int Renderer11::getMaxSwapInterval() const
2288{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002289 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002290}
2291
2292int Renderer11::getMaxSupportedSamples() const
2293{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002294 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002295}
2296
Geoff Lang005df412013-10-16 14:12:50 -04002297GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002298{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002299 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002300 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2301 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2302}
2303
Geoff Lang005df412013-10-16 14:12:50 -04002304GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002305{
2306 unsigned int numCounts = 0;
2307
2308 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002309 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2310 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002311 {
2312 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2313 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2314
2315 if (iter != mMultisampleSupportMap.end())
2316 {
2317 const MultisampleSupportInfo& info = iter->second;
2318 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2319 {
2320 if (info.qualityLevels[i] > 0)
2321 {
2322 numCounts++;
2323 }
2324 }
2325 }
2326 }
2327
2328 return numCounts;
2329}
2330
Geoff Lang005df412013-10-16 14:12:50 -04002331void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002332{
2333 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002334 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2335 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2336 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002337 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002338 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002339
2340 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2341 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2342
2343 if (iter != mMultisampleSupportMap.end())
2344 {
2345 const MultisampleSupportInfo& info = iter->second;
2346 int bufPos = 0;
2347 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2348 {
2349 if (info.qualityLevels[i] > 0)
2350 {
2351 params[bufPos++] = i + 1;
2352 }
2353 }
2354 }
2355}
2356
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002357int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2358{
2359 if (requested == 0)
2360 {
2361 return 0;
2362 }
2363
2364 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2365 if (iter != mMultisampleSupportMap.end())
2366 {
2367 const MultisampleSupportInfo& info = iter->second;
2368 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2369 {
2370 if (info.qualityLevels[i] > 0)
2371 {
2372 return i + 1;
2373 }
2374 }
2375 }
2376
2377 return -1;
2378}
2379
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002380unsigned int Renderer11::getMaxRenderTargets() const
2381{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002382 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2383 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2384
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002385 switch (mFeatureLevel)
2386 {
2387 case D3D_FEATURE_LEVEL_11_0:
2388 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2389 case D3D_FEATURE_LEVEL_10_1:
2390 case D3D_FEATURE_LEVEL_10_0:
2391 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2392 default:
2393 UNREACHABLE();
2394 return 1;
2395 }
2396}
2397
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002398bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002399{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002400 if (source && dest)
2401 {
2402 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2403 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2404
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002405 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002406
2407 dest11->invalidateSwizzleCache();
2408
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002409 return true;
2410 }
2411
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002412 return false;
2413}
2414
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002415bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002416{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002417 if (source && dest)
2418 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002419 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2420 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002421
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002422 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002423
2424 dest11->invalidateSwizzleCache();
2425
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002426 return true;
2427 }
2428
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002429 return false;
2430}
2431
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002432bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2433{
2434 if (source && dest)
2435 {
2436 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2437 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2438
2439 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002440
2441 dest11->invalidateSwizzleCache();
2442
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002443 return true;
2444 }
2445
2446 return false;
2447}
2448
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002449bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2450{
2451 if (source && dest)
2452 {
2453 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2454 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2455
2456 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002457
2458 dest11->invalidateSwizzleCache();
2459
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002460 return true;
2461 }
2462
2463 return false;
2464}
2465
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002466bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002467 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002468{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002469 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002470 if (!colorbuffer)
2471 {
2472 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002473 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002474 }
2475
2476 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2477 if (!sourceRenderTarget)
2478 {
2479 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002480 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002481 }
2482
2483 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2484 if (!source)
2485 {
2486 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002487 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002488 }
2489
2490 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2491 if (!storage11)
2492 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002493 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002494 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002495 }
2496
2497 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2498 if (!destRenderTarget)
2499 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002500 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002501 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002502 }
2503
2504 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2505 if (!dest)
2506 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002507 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002508 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002509 }
2510
Geoff Langb86b9792013-06-04 16:32:05 -04002511 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2512 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002513
Geoff Langb86b9792013-06-04 16:32:05 -04002514 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2515 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002516
Geoff Langb86b9792013-06-04 16:32:05 -04002517 // Use nearest filtering because source and destination are the same size for the direct
2518 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002519 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002520 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002521
Geoff Lang42477a42013-09-17 17:07:02 -04002522 storage11->invalidateSwizzleCacheLevel(level);
2523
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002524 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002525}
2526
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002527bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002528 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002529{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002530 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002531 if (!colorbuffer)
2532 {
2533 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002534 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002535 }
2536
2537 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2538 if (!sourceRenderTarget)
2539 {
2540 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002541 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002542 }
2543
2544 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2545 if (!source)
2546 {
2547 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002548 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002549 }
2550
2551 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2552 if (!storage11)
2553 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002554 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002555 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002556 }
2557
Nicolas Capensb13f8662013-06-04 13:30:19 -04002558 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002559 if (!destRenderTarget)
2560 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002561 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002562 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002563 }
2564
2565 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2566 if (!dest)
2567 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002568 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002569 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002570 }
2571
Geoff Langb86b9792013-06-04 16:32:05 -04002572 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2573 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002574
Geoff Langb86b9792013-06-04 16:32:05 -04002575 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2576 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002577
Geoff Langb86b9792013-06-04 16:32:05 -04002578 // Use nearest filtering because source and destination are the same size for the direct
2579 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002580 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002581 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002582
Geoff Lang42477a42013-09-17 17:07:02 -04002583 storage11->invalidateSwizzleCacheLevel(level);
2584
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002585 return ret;
2586}
2587
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002588bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2589 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2590{
2591 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2592 if (!colorbuffer)
2593 {
2594 ERR("Failed to retrieve the color buffer from the frame buffer.");
2595 return gl::error(GL_OUT_OF_MEMORY, false);
2596 }
2597
2598 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2599 if (!sourceRenderTarget)
2600 {
2601 ERR("Failed to retrieve the render target from the frame buffer.");
2602 return gl::error(GL_OUT_OF_MEMORY, false);
2603 }
2604
2605 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2606 if (!source)
2607 {
2608 ERR("Failed to retrieve the render target view from the render target.");
2609 return gl::error(GL_OUT_OF_MEMORY, false);
2610 }
2611
2612 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2613 if (!storage11)
2614 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002615 ERR("Failed to retrieve the texture storage from the destination.");
2616 return gl::error(GL_OUT_OF_MEMORY, false);
2617 }
2618
2619 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2620 if (!destRenderTarget)
2621 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002622 ERR("Failed to retrieve the render target from the destination storage.");
2623 return gl::error(GL_OUT_OF_MEMORY, false);
2624 }
2625
2626 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2627 if (!dest)
2628 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002629 ERR("Failed to retrieve the render target view from the destination render target.");
2630 return gl::error(GL_OUT_OF_MEMORY, false);
2631 }
2632
Geoff Langb86b9792013-06-04 16:32:05 -04002633 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2634 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002635
Geoff Langb86b9792013-06-04 16:32:05 -04002636 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2637 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002638
Geoff Langb86b9792013-06-04 16:32:05 -04002639 // Use nearest filtering because source and destination are the same size for the direct
2640 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002641 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002642 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002643
Geoff Lang42477a42013-09-17 17:07:02 -04002644 storage11->invalidateSwizzleCacheLevel(level);
2645
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002646 return ret;
2647}
2648
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002649bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2650 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2651{
2652 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2653 if (!colorbuffer)
2654 {
2655 ERR("Failed to retrieve the color buffer from the frame buffer.");
2656 return gl::error(GL_OUT_OF_MEMORY, false);
2657 }
2658
2659 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2660 if (!sourceRenderTarget)
2661 {
2662 ERR("Failed to retrieve the render target from the frame buffer.");
2663 return gl::error(GL_OUT_OF_MEMORY, false);
2664 }
2665
2666 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2667 if (!source)
2668 {
2669 ERR("Failed to retrieve the render target view from the render target.");
2670 return gl::error(GL_OUT_OF_MEMORY, false);
2671 }
2672
2673 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2674 if (!storage11)
2675 {
Geoff Langea228632013-07-30 15:17:12 -04002676 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002677 ERR("Failed to retrieve the texture storage from the destination.");
2678 return gl::error(GL_OUT_OF_MEMORY, false);
2679 }
2680
2681 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2682 if (!destRenderTarget)
2683 {
Geoff Langea228632013-07-30 15:17:12 -04002684 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002685 ERR("Failed to retrieve the render target from the destination storage.");
2686 return gl::error(GL_OUT_OF_MEMORY, false);
2687 }
2688
2689 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2690 if (!dest)
2691 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002692 ERR("Failed to retrieve the render target view from the destination render target.");
2693 return gl::error(GL_OUT_OF_MEMORY, false);
2694 }
2695
Geoff Langb86b9792013-06-04 16:32:05 -04002696 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2697 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002698
Geoff Langb86b9792013-06-04 16:32:05 -04002699 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2700 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002701
Geoff Langb86b9792013-06-04 16:32:05 -04002702 // Use nearest filtering because source and destination are the same size for the direct
2703 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002704 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002705 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002706
Geoff Lang42477a42013-09-17 17:07:02 -04002707 storage11->invalidateSwizzleCacheLevel(level);
2708
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002709 return ret;
2710}
2711
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002712void Renderer11::unapplyRenderTargets()
2713{
2714 setOneTimeRenderTarget(NULL);
2715}
2716
2717void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2718{
2719 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2720
2721 rtvArray[0] = renderTargetView;
2722
2723 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2724
2725 // Do not preserve the serial for this one-time-use render target
2726 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2727 {
2728 mAppliedRenderTargetSerials[rtIndex] = 0;
2729 }
2730}
2731
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002732RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2733{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002734 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002735 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002736
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002737 if (depth)
2738 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002739 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002740 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002741 swapChain11->getDepthStencilTexture(),
2742 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002743 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002744 }
2745 else
2746 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002747 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002748 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002749 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002750 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002751 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002752 }
2753 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002754}
2755
Geoff Langa2d97f12013-06-11 11:44:02 -04002756RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002757{
Geoff Langa2d97f12013-06-11 11:44:02 -04002758 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002759 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002760}
2761
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002762ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002763{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002764 ShaderExecutable11 *executable = NULL;
2765
2766 switch (type)
2767 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002768 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002769 {
2770 ID3D11VertexShader *vshader = NULL;
2771 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2772 ASSERT(SUCCEEDED(result));
2773
2774 if (vshader)
2775 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002776 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002777 }
2778 }
2779 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002780 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002781 {
2782 ID3D11PixelShader *pshader = NULL;
2783 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2784 ASSERT(SUCCEEDED(result));
2785
2786 if (pshader)
2787 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002788 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002789 }
2790 }
2791 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002792 case rx::SHADER_GEOMETRY:
2793 {
2794 ID3D11GeometryShader *gshader = NULL;
2795 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2796 ASSERT(SUCCEEDED(result));
2797
2798 if (gshader)
2799 {
2800 executable = new ShaderExecutable11(function, length, gshader);
2801 }
2802 }
2803 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002804 default:
2805 UNREACHABLE();
2806 break;
2807 }
2808
2809 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002810}
2811
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002812ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002813{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002814 const char *profile = NULL;
2815
2816 switch (type)
2817 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002818 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002819 profile = "vs_4_0";
2820 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002821 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002822 profile = "ps_4_0";
2823 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002824 case rx::SHADER_GEOMETRY:
2825 profile = "gs_4_0";
2826 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002827 default:
2828 UNREACHABLE();
2829 return NULL;
2830 }
2831
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00002832 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002833 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002834 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002835 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002836 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002837
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002838 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002839 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002840
2841 return executable;
2842}
2843
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002844VertexBuffer *Renderer11::createVertexBuffer()
2845{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002846 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002847}
2848
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002849IndexBuffer *Renderer11::createIndexBuffer()
2850{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002851 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002852}
2853
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002854BufferStorage *Renderer11::createBufferStorage()
2855{
2856 return new BufferStorage11(this);
2857}
2858
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002859QueryImpl *Renderer11::createQuery(GLenum type)
2860{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002861 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002862}
2863
2864FenceImpl *Renderer11::createFence()
2865{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002866 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002867}
2868
Geoff Lang005df412013-10-16 14:12:50 -04002869bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002870{
Jamie Madill4461f092013-10-10 15:10:39 -04002871 int clientVersion = getCurrentClientVersion();
2872
2873 // We only support buffer to texture copies in ES3
2874 if (clientVersion <= 2)
2875 {
2876 return false;
2877 }
2878
2879 // sRGB formats do not work with D3D11 buffer SRVs
2880 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2881 {
2882 return false;
2883 }
2884
2885 // We cannot support direct copies to non-color-renderable formats
2886 if (!gl::IsColorRenderingSupported(internalFormat, this))
2887 {
2888 return false;
2889 }
2890
2891 // We skip all 3-channel formats since sometimes format support is missing
2892 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2893 {
2894 return false;
2895 }
2896
2897 // We don't support formats which we can't represent without conversion
2898 if (getNativeTextureFormat(internalFormat) != internalFormat)
2899 {
2900 return false;
2901 }
2902
2903 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002904}
2905
Jamie Madilla21eea32013-09-18 14:36:25 -04002906bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2907 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2908{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002909 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002910 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2911}
2912
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002913bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002914{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002915 ASSERT(colorbuffer != NULL);
2916
2917 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2918 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002919 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002920 *subresourceIndex = renderTarget->getSubresourceIndex();
2921
2922 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2923 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002924 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002925 ID3D11Resource *textureResource = NULL;
2926 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002927
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002928 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002929 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002930 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002931 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002932
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002933 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002934 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002935 return true;
2936 }
2937 else
2938 {
2939 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2940 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002941 }
2942 }
2943 }
2944 }
2945
2946 return false;
2947}
2948
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002949bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002950 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002951{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002952 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002953 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002954 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002955
2956 if (!readBuffer)
2957 {
2958 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2959 return gl::error(GL_OUT_OF_MEMORY, false);
2960 }
2961
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002962 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002963
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002964 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002965 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002966 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2967 {
2968 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
2969
2970 if (!drawBuffer)
2971 {
2972 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2973 return gl::error(GL_OUT_OF_MEMORY, false);
2974 }
2975
2976 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2977
Geoff Lang125deab2013-08-09 13:34:16 -04002978 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002979 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002980 {
2981 return false;
2982 }
2983 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002984 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002985 }
2986
Geoff Lang685806d2013-06-12 11:16:36 -04002987 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002988 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002989 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
2990 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
2991
2992 if (!readBuffer)
2993 {
2994 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2995 return gl::error(GL_OUT_OF_MEMORY, false);
2996 }
2997
2998 if (!drawBuffer)
2999 {
3000 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
3001 return gl::error(GL_OUT_OF_MEMORY, false);
3002 }
3003
3004 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3005 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3006
Geoff Lang125deab2013-08-09 13:34:16 -04003007 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04003008 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003009 {
3010 return false;
3011 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003012 }
3013
Geoff Lang42477a42013-09-17 17:07:02 -04003014 invalidateFramebufferSwizzles(drawTarget);
3015
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003016 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003017}
3018
3019void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3020 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3021{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003022 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003023 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003024
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003025 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3026
3027 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003028 {
3029 gl::Rectangle area;
3030 area.x = x;
3031 area.y = y;
3032 area.width = width;
3033 area.height = height;
3034
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003035 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3036 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003037
Geoff Langea228632013-07-30 15:17:12 -04003038 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003039 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003040}
3041
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003042Image *Renderer11::createImage()
3043{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003044 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003045}
3046
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003047void Renderer11::generateMipmap(Image *dest, Image *src)
3048{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003049 Image11 *dest11 = Image11::makeImage11(dest);
3050 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003051 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003052}
3053
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003054TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3055{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003056 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3057 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003058}
3059
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003060TextureStorage *Renderer11::createTextureStorage2D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003061{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003062 return new TextureStorage11_2D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003063}
3064
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003065TextureStorage *Renderer11::createTextureStorageCube(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, int size)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003066{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003067 return new TextureStorage11_Cube(this, baseLevel, maxLevel, internalformat, renderTarget, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003068}
3069
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003070TextureStorage *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 +00003071{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003072 return new TextureStorage11_3D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003073}
3074
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003075TextureStorage *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 +00003076{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003077 return new TextureStorage11_2DArray(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003078}
3079
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003080void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3081 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3082 GLint packAlignment, void *pixels)
3083{
3084 D3D11_TEXTURE2D_DESC textureDesc;
3085 texture->GetDesc(&textureDesc);
3086
3087 D3D11_TEXTURE2D_DESC stagingDesc;
3088 stagingDesc.Width = area.width;
3089 stagingDesc.Height = area.height;
3090 stagingDesc.MipLevels = 1;
3091 stagingDesc.ArraySize = 1;
3092 stagingDesc.Format = textureDesc.Format;
3093 stagingDesc.SampleDesc.Count = 1;
3094 stagingDesc.SampleDesc.Quality = 0;
3095 stagingDesc.Usage = D3D11_USAGE_STAGING;
3096 stagingDesc.BindFlags = 0;
3097 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3098 stagingDesc.MiscFlags = 0;
3099
3100 ID3D11Texture2D* stagingTex = NULL;
3101 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3102 if (FAILED(result))
3103 {
3104 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3105 return;
3106 }
3107
3108 ID3D11Texture2D* srcTex = NULL;
3109 if (textureDesc.SampleDesc.Count > 1)
3110 {
3111 D3D11_TEXTURE2D_DESC resolveDesc;
3112 resolveDesc.Width = textureDesc.Width;
3113 resolveDesc.Height = textureDesc.Height;
3114 resolveDesc.MipLevels = 1;
3115 resolveDesc.ArraySize = 1;
3116 resolveDesc.Format = textureDesc.Format;
3117 resolveDesc.SampleDesc.Count = 1;
3118 resolveDesc.SampleDesc.Quality = 0;
3119 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3120 resolveDesc.BindFlags = 0;
3121 resolveDesc.CPUAccessFlags = 0;
3122 resolveDesc.MiscFlags = 0;
3123
3124 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3125 if (FAILED(result))
3126 {
3127 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003128 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003129 return;
3130 }
3131
3132 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3133 subResource = 0;
3134 }
3135 else
3136 {
3137 srcTex = texture;
3138 srcTex->AddRef();
3139 }
3140
3141 D3D11_BOX srcBox;
3142 srcBox.left = area.x;
3143 srcBox.right = area.x + area.width;
3144 srcBox.top = area.y;
3145 srcBox.bottom = area.y + area.height;
3146 srcBox.front = 0;
3147 srcBox.back = 1;
3148
3149 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3150
Geoff Langea228632013-07-30 15:17:12 -04003151 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003152
3153 D3D11_MAPPED_SUBRESOURCE mapping;
3154 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3155
3156 unsigned char *source;
3157 int inputPitch;
3158 if (packReverseRowOrder)
3159 {
3160 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3161 inputPitch = -static_cast<int>(mapping.RowPitch);
3162 }
3163 else
3164 {
3165 source = static_cast<unsigned char*>(mapping.pData);
3166 inputPitch = static_cast<int>(mapping.RowPitch);
3167 }
3168
Geoff Lang697ad3e2013-06-04 10:11:28 -04003169 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003170
Geoff Lang005df412013-10-16 14:12:50 -04003171 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003172 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3173 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3174
3175 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3176
3177 if (sourceFormat == format && sourceType == type)
3178 {
3179 // Direct copy possible
3180 unsigned char *dest = static_cast<unsigned char*>(pixels);
3181 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003182 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003183 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003184 }
3185 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003186 else
3187 {
Geoff Lang005df412013-10-16 14:12:50 -04003188 GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003189 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3190
3191 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type, getCurrentClientVersion());
3192 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003193 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003194 // Fast copy is possible through some special function
3195 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003196 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003197 for (int x = 0; x < area.width; x++)
3198 {
3199 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3200 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3201
3202 fastCopyFunc(src, dest);
3203 }
3204 }
3205 }
3206 else
3207 {
Jamie Madilld6cb2442013-07-10 15:13:38 -04003208 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003209 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3210
3211 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3212 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3213 sizeof(temp) >= sizeof(gl::ColorUI) &&
3214 sizeof(temp) >= sizeof(gl::ColorI));
3215
3216 for (int y = 0; y < area.height; y++)
3217 {
3218 for (int x = 0; x < area.width; x++)
3219 {
3220 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3221 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3222
3223 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3224 // will not allow the copy otherwise.
3225 readFunc(src, temp);
3226 writeFunc(temp, dest);
3227 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003228 }
3229 }
3230 }
3231
3232 mDeviceContext->Unmap(stagingTex, 0);
3233
Geoff Langea228632013-07-30 15:17:12 -04003234 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003235}
3236
Geoff Lang758d5b22013-06-11 11:42:50 -04003237bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003238 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3239 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003240{
Geoff Lang975af372013-06-12 11:19:22 -04003241 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3242 // it should never be the case that both color and depth/stencil need to be blitted at
3243 // at the same time.
3244 ASSERT(colorBlit != (depthBlit || stencilBlit));
3245
Geoff Langc1f51be2013-06-11 11:49:14 -04003246 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003247
Geoff Lang4d782732013-07-22 10:44:18 -04003248 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3249 if (!drawRenderTarget)
3250 {
3251 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3252 return gl::error(GL_OUT_OF_MEMORY, false);
3253 }
3254
3255 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3256 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3257 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3258 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3259
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003260 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3261 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003262 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003263 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003264 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003265 }
3266
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003267 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003268 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003269 unsigned int readSubresource = 0;
3270 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003271 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003272 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3273 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003274
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003275 if (unresolvedTexture)
3276 {
3277 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3278 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003279
Geoff Langea228632013-07-30 15:17:12 -04003280 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003281
3282 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3283 if (FAILED(result))
3284 {
Geoff Langea228632013-07-30 15:17:12 -04003285 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003286 return gl::error(GL_OUT_OF_MEMORY, false);
3287 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003288 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003289 }
3290 else
3291 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003292 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003293 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003294 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003295 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003296 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003297 }
3298
Geoff Lang4d782732013-07-22 10:44:18 -04003299 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003300 {
Geoff Lang4d782732013-07-22 10:44:18 -04003301 SafeRelease(readTexture);
3302 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003303 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003304 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003305 }
3306
Geoff Lang125deab2013-08-09 13:34:16 -04003307 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3308 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3309
3310 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3311
3312 bool wholeBufferCopy = !scissorNeeded &&
3313 readRect.x == 0 && readRect.width == readSize.width &&
3314 readRect.y == 0 && readRect.height == readSize.height &&
3315 drawRect.x == 0 && drawRect.width == drawSize.width &&
3316 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003317
Geoff Langc1f51be2013-06-11 11:49:14 -04003318 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003319
Geoff Lang125deab2013-08-09 13:34:16 -04003320 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || readRect.height < 0;
3321
3322 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3323 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3324 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3325 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3326
3327 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3328 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3329 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3330
Geoff Langc1f51be2013-06-11 11:49:14 -04003331 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003332 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3333 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003334 {
Geoff Lang125deab2013-08-09 13:34:16 -04003335 UINT dstX = drawRect.x;
3336 UINT dstY = drawRect.y;
3337
Geoff Langc1f51be2013-06-11 11:49:14 -04003338 D3D11_BOX readBox;
3339 readBox.left = readRect.x;
3340 readBox.right = readRect.x + readRect.width;
3341 readBox.top = readRect.y;
3342 readBox.bottom = readRect.y + readRect.height;
3343 readBox.front = 0;
3344 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003345
Geoff Lang125deab2013-08-09 13:34:16 -04003346 if (scissorNeeded)
3347 {
3348 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3349 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3350
3351 if (drawRect.x < scissor->x)
3352 {
3353 dstX = scissor->x;
3354 readBox.left += (scissor->x - drawRect.x);
3355 }
3356 if (drawRect.y < scissor->y)
3357 {
3358 dstY = scissor->y;
3359 readBox.top += (scissor->y - drawRect.y);
3360 }
3361 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3362 {
3363 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3364 }
3365 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3366 {
3367 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3368 }
3369 }
3370
Geoff Langc1f51be2013-06-11 11:49:14 -04003371 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3372 // We also require complete framebuffer copies for depth-stencil blit.
3373 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003374
Geoff Lang125deab2013-08-09 13:34:16 -04003375 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003376 readTexture, readSubresource, pSrcBox);
3377 result = true;
3378 }
3379 else
3380 {
3381 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003382 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003383
Geoff Lang975af372013-06-12 11:19:22 -04003384 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003385 {
Geoff Lang975af372013-06-12 11:19:22 -04003386 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003387 drawTexture, drawSubresource, drawArea, drawSize,
3388 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003389 }
3390 else if (depthBlit)
3391 {
Geoff Lang125deab2013-08-09 13:34:16 -04003392 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3393 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003394 }
3395 else if (stencilBlit)
3396 {
3397 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003398 drawTexture, drawSubresource, drawArea, drawSize,
3399 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003400 }
3401 else
3402 {
Geoff Lang685806d2013-06-12 11:16:36 -04003403 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003404 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3405 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003406 }
3407 }
3408
3409 SafeRelease(readTexture);
3410 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003411
3412 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003413}
3414
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003415ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3416{
3417 D3D11_TEXTURE2D_DESC textureDesc;
3418 source->GetDesc(&textureDesc);
3419
3420 if (textureDesc.SampleDesc.Count > 1)
3421 {
3422 D3D11_TEXTURE2D_DESC resolveDesc;
3423 resolveDesc.Width = textureDesc.Width;
3424 resolveDesc.Height = textureDesc.Height;
3425 resolveDesc.MipLevels = 1;
3426 resolveDesc.ArraySize = 1;
3427 resolveDesc.Format = textureDesc.Format;
3428 resolveDesc.SampleDesc.Count = 1;
3429 resolveDesc.SampleDesc.Quality = 0;
3430 resolveDesc.Usage = textureDesc.Usage;
3431 resolveDesc.BindFlags = textureDesc.BindFlags;
3432 resolveDesc.CPUAccessFlags = 0;
3433 resolveDesc.MiscFlags = 0;
3434
3435 ID3D11Texture2D *resolveTexture = NULL;
3436 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3437 if (FAILED(result))
3438 {
3439 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3440 return NULL;
3441 }
3442
3443 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3444 return resolveTexture;
3445 }
3446 else
3447 {
3448 source->AddRef();
3449 return source;
3450 }
3451}
3452
Geoff Lang42477a42013-09-17 17:07:02 -04003453void Renderer11::invalidateRenderbufferSwizzles(gl::Renderbuffer *renderBuffer, int mipLevel)
3454{
3455 TextureStorage *texStorage = renderBuffer->getTextureStorage();
3456 if (texStorage)
3457 {
3458 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3459 if (!texStorage11)
3460 {
3461 ERR("texture storage pointer unexpectedly null.");
3462 return;
3463 }
3464
3465 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3466 }
3467}
3468
3469void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3470{
3471 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3472 {
3473 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
3474 if (colorbuffer)
3475 {
3476 invalidateRenderbufferSwizzles(colorbuffer, framebuffer->getColorbufferMipLevel(colorAttachment));
3477 }
3478 }
3479
3480 gl::Renderbuffer *depthBuffer = framebuffer->getDepthbuffer();
3481 if (depthBuffer)
3482 {
3483 invalidateRenderbufferSwizzles(depthBuffer, framebuffer->getDepthbufferMipLevel());
3484 }
3485
3486 gl::Renderbuffer *stencilBuffer = framebuffer->getStencilbuffer();
3487 if (stencilBuffer)
3488 {
3489 invalidateRenderbufferSwizzles(stencilBuffer, framebuffer->getStencilbufferMipLevel());
3490 }
3491}
3492
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003493bool Renderer11::getLUID(LUID *adapterLuid) const
3494{
3495 adapterLuid->HighPart = 0;
3496 adapterLuid->LowPart = 0;
3497
3498 if (!mDxgiAdapter)
3499 {
3500 return false;
3501 }
3502
3503 DXGI_ADAPTER_DESC adapterDesc;
3504 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3505 {
3506 return false;
3507 }
3508
3509 *adapterLuid = adapterDesc.AdapterLuid;
3510 return true;
3511}
3512
Geoff Lang005df412013-10-16 14:12:50 -04003513GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003514{
3515 int clientVersion = getCurrentClientVersion();
3516 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3517}
3518
Geoff Lang61e49a52013-05-29 10:22:58 -04003519Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3520{
3521 MultisampleSupportInfo supportInfo = { 0 };
3522
3523 UINT formatSupport;
3524 HRESULT result;
3525
3526 result = mDevice->CheckFormatSupport(format, &formatSupport);
3527 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3528 {
3529 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3530 {
3531 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3532 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3533 {
3534 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3535 }
3536 else
3537 {
3538 supportInfo.qualityLevels[i - 1] = 0;
3539 }
3540 }
3541 }
3542
3543 return supportInfo;
3544}
3545
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003546}