blob: ecc1887bb36386e7f9cb574adc0778588ebb6e23 [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
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001451void Renderer11::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001452{
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001453 const gl::UniformArray &uniformArray = programBinary.getUniforms();
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001454
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001455 unsigned int totalRegisterCountVS = 0;
1456 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001457
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001458 bool vertexUniformsDirty = false;
1459 bool pixelUniformsDirty = false;
1460
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001461 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001462 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001463 const gl::Uniform &uniform = *uniformArray[uniformIndex];
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001464
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001465 if (uniform.isReferencedByVertexShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001466 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001467 totalRegisterCountVS += uniform.registerCount;
1468 vertexUniformsDirty = (vertexUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001469 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001470
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001471 if (uniform.isReferencedByFragmentShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001472 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001473 totalRegisterCountPS += uniform.registerCount;
1474 pixelUniformsDirty = (pixelUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001475 }
1476 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001477
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001478 const UniformStorage11 *vertexUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getVertexUniformStorage());
1479 const UniformStorage11 *fragmentUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getFragmentUniformStorage());
1480 ASSERT(vertexUniformStorage);
1481 ASSERT(fragmentUniformStorage);
1482
1483 ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
1484 ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001485
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001486 float (*mapVS)[4] = NULL;
1487 float (*mapPS)[4] = NULL;
1488
Shannon Woods5ab33c82013-06-26 15:31:09 -04001489 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1490 {
1491 D3D11_MAPPED_SUBRESOURCE map = {0};
1492 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1493 ASSERT(SUCCEEDED(result));
1494 mapVS = (float(*)[4])map.pData;
1495 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001496
Shannon Woods5ab33c82013-06-26 15:31:09 -04001497 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1498 {
1499 D3D11_MAPPED_SUBRESOURCE map = {0};
1500 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1501 ASSERT(SUCCEEDED(result));
1502 mapPS = (float(*)[4])map.pData;
1503 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001504
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001505 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001506 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001507 gl::Uniform *uniform = uniformArray[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001508
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001509 if (!uniform->isSampler())
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001510 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001511 unsigned int componentCount = (4 - uniform->registerElement);
1512
Jamie Madill71cc91f2013-09-18 12:51:22 -04001513 // 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 -04001514 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001515
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001516 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001517 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001518 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001519 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001520
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001521 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001522 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001523 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001524 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001525 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001526 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001527
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001528 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001529 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001530 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001531 }
1532
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001533 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001534 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001535 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001536 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001537
1538 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1539 {
1540 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1541 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1542 }
1543
1544 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1545 {
1546 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1547 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1548 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001549
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001550 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001551 if (!mDriverConstantBufferVS)
1552 {
1553 D3D11_BUFFER_DESC constantBufferDescription = {0};
1554 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1555 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1556 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1557 constantBufferDescription.CPUAccessFlags = 0;
1558 constantBufferDescription.MiscFlags = 0;
1559 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001560
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001561 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001562 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001563
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001564 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1565 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001566
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001567 if (!mDriverConstantBufferPS)
1568 {
1569 D3D11_BUFFER_DESC constantBufferDescription = {0};
1570 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1571 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1572 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1573 constantBufferDescription.CPUAccessFlags = 0;
1574 constantBufferDescription.MiscFlags = 0;
1575 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001576
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001577 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001578 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001579
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001580 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1581 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001582
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001583 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1584 {
1585 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1586 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1587 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001588
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001589 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1590 {
1591 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1592 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1593 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001594
1595 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001596 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1597 {
1598 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1599 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1600 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001601}
1602
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001603void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001604{
Geoff Langda507fe2013-08-20 12:01:42 -04001605 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001606 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001607}
1608
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001609void Renderer11::markAllStateDirty()
1610{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001611 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1612 {
1613 mAppliedRenderTargetSerials[rtIndex] = 0;
1614 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001615 mAppliedDepthbufferSerial = 0;
1616 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001617 mDepthStencilInitialized = false;
1618 mRenderTargetDescInitialized = false;
1619
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001620 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001621 {
1622 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001623 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001624 }
1625 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1626 {
1627 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001628 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001629 }
1630
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001631 mForceSetBlendState = true;
1632 mForceSetRasterState = true;
1633 mForceSetDepthStencilState = true;
1634 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001635 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001636
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001637 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001638 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001639 mAppliedIBOffset = 0;
1640
Jamie Madill6246dc82014-01-29 09:26:47 -05001641 mAppliedVertexShader = NULL;
1642 mAppliedGeometryShader = NULL;
1643 mAppliedPixelShader = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001644 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1645 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001646
1647 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001648
1649 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1650 {
1651 mCurrentConstantBufferVS[i] = -1;
1652 mCurrentConstantBufferPS[i] = -1;
1653 }
1654
1655 mCurrentVertexConstantBuffer = NULL;
1656 mCurrentPixelConstantBuffer = NULL;
1657 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001658
1659 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001660}
1661
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001662void Renderer11::releaseDeviceResources()
1663{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001664 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001665 mInputLayoutCache.clear();
1666
Geoff Langea228632013-07-30 15:17:12 -04001667 SafeDelete(mVertexDataManager);
1668 SafeDelete(mIndexDataManager);
1669 SafeDelete(mLineLoopIB);
1670 SafeDelete(mTriangleFanIB);
1671 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001672 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001673 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001674
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001675 SafeRelease(mDriverConstantBufferVS);
1676 SafeRelease(mDriverConstantBufferPS);
1677 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001678}
1679
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001680void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001681{
1682 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001683 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001684}
1685
1686bool Renderer11::isDeviceLost()
1687{
1688 return mDeviceLost;
1689}
1690
1691// set notify to true to broadcast a message to all contexts of the device loss
1692bool Renderer11::testDeviceLost(bool notify)
1693{
1694 bool isLost = false;
1695
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001696 // GetRemovedReason is used to test if the device is removed
1697 HRESULT result = mDevice->GetDeviceRemovedReason();
1698 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001699
1700 if (isLost)
1701 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001702 // Log error if this is a new device lost event
1703 if (mDeviceLost == false)
1704 {
1705 ERR("The D3D11 device was removed: 0x%08X", result);
1706 }
1707
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001708 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001709 // we'll probably get this done again by notifyDeviceLost
1710 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001711 // Note that we don't want to clear the device loss status here
1712 // -- this needs to be done by resetDevice
1713 mDeviceLost = true;
1714 if (notify)
1715 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001716 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001717 }
1718 }
1719
1720 return isLost;
1721}
1722
1723bool Renderer11::testDeviceResettable()
1724{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001725 // determine if the device is resettable by creating a dummy device
1726 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001727
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001728 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001729 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001730 return false;
1731 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001732
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001733 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001734 {
1735 D3D_FEATURE_LEVEL_11_0,
1736 D3D_FEATURE_LEVEL_10_1,
1737 D3D_FEATURE_LEVEL_10_0,
1738 };
1739
1740 ID3D11Device* dummyDevice;
1741 D3D_FEATURE_LEVEL dummyFeatureLevel;
1742 ID3D11DeviceContext* dummyContext;
1743
1744 HRESULT result = D3D11CreateDevice(NULL,
1745 D3D_DRIVER_TYPE_HARDWARE,
1746 NULL,
1747 #if defined(_DEBUG)
1748 D3D11_CREATE_DEVICE_DEBUG,
1749 #else
1750 0,
1751 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001752 featureLevels,
1753 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001754 D3D11_SDK_VERSION,
1755 &dummyDevice,
1756 &dummyFeatureLevel,
1757 &dummyContext);
1758
1759 if (!mDevice || FAILED(result))
1760 {
1761 return false;
1762 }
1763
Geoff Langea228632013-07-30 15:17:12 -04001764 SafeRelease(dummyContext);
1765 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001766
1767 return true;
1768}
1769
1770void Renderer11::release()
1771{
1772 releaseDeviceResources();
1773
Geoff Langea228632013-07-30 15:17:12 -04001774 SafeRelease(mDxgiFactory);
1775 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001776
1777 if (mDeviceContext)
1778 {
1779 mDeviceContext->ClearState();
1780 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001781 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001782 }
1783
Geoff Langea228632013-07-30 15:17:12 -04001784 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001785
1786 if (mD3d11Module)
1787 {
1788 FreeLibrary(mD3d11Module);
1789 mD3d11Module = NULL;
1790 }
1791
1792 if (mDxgiModule)
1793 {
1794 FreeLibrary(mDxgiModule);
1795 mDxgiModule = NULL;
1796 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001797}
1798
1799bool Renderer11::resetDevice()
1800{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001801 // recreate everything
1802 release();
1803 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001804
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001805 if (result != EGL_SUCCESS)
1806 {
1807 ERR("Could not reinitialize D3D11 device: %08X", result);
1808 return false;
1809 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001810
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001811 mDeviceLost = false;
1812
1813 return true;
1814}
1815
1816DWORD Renderer11::getAdapterVendor() const
1817{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001818 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001819}
1820
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001821std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001822{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001823 std::ostringstream rendererString;
1824
1825 rendererString << mDescription;
1826 rendererString << " Direct3D11";
1827
1828 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1829 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1830
1831 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001832}
1833
1834GUID Renderer11::getAdapterIdentifier() const
1835{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001836 // Use the adapter LUID as our adapter ID
1837 // This number is local to a machine is only guaranteed to be unique between restarts
1838 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1839 GUID adapterId = {0};
1840 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1841 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001842}
1843
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001844bool Renderer11::getBGRATextureSupport() const
1845{
1846 return mBGRATextureSupport;
1847}
1848
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001849bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001850{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001851 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001852}
1853
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001854bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001855{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001856 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001857}
1858
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001859bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001860{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001861 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001862}
1863
1864bool Renderer11::getDepthTextureSupport() const
1865{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001866 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001867}
1868
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001869bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001870{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001871 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001872}
1873
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001874bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001875{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001876 return mFloat32FilterSupport;
1877}
1878
1879bool Renderer11::getFloat32TextureRenderingSupport() const
1880{
1881 return mFloat32RenderSupport;
1882}
1883
1884bool Renderer11::getFloat16TextureSupport() const
1885{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001886 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001887}
1888
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001889bool Renderer11::getFloat16TextureFilteringSupport() const
1890{
1891 return mFloat16FilterSupport;
1892}
1893
1894bool Renderer11::getFloat16TextureRenderingSupport() const
1895{
1896 return mFloat16RenderSupport;
1897}
1898
Geoff Langd42cf4e2013-06-05 16:09:17 -04001899bool Renderer11::getRGB565TextureSupport() const
1900{
1901 return false;
1902}
1903
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001904bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001905{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001906 return false;
1907}
1908
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001909bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001910{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001911 return false;
1912}
1913
Geoff Lang632192d2013-10-04 13:40:46 -04001914bool Renderer11::getRGTextureSupport() const
1915{
1916 return mRGTextureSupport;
1917}
1918
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001919bool Renderer11::getTextureFilterAnisotropySupport() const
1920{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001921 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001922}
1923
1924float Renderer11::getTextureMaxAnisotropy() const
1925{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001926 switch (mFeatureLevel)
1927 {
1928 case D3D_FEATURE_LEVEL_11_0:
1929 return D3D11_MAX_MAXANISOTROPY;
1930 case D3D_FEATURE_LEVEL_10_1:
1931 case D3D_FEATURE_LEVEL_10_0:
1932 return D3D10_MAX_MAXANISOTROPY;
1933 default: UNREACHABLE();
1934 return 0;
1935 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001936}
1937
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001938bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001939{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00001940 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001941}
1942
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001943Range Renderer11::getViewportBounds() const
1944{
1945 switch (mFeatureLevel)
1946 {
1947 case D3D_FEATURE_LEVEL_11_0:
1948 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1949 case D3D_FEATURE_LEVEL_10_1:
1950 case D3D_FEATURE_LEVEL_10_0:
1951 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1952 default: UNREACHABLE();
1953 return Range(0, 0);
1954 }
1955}
1956
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001957unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001958{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001959 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1960 switch (mFeatureLevel)
1961 {
1962 case D3D_FEATURE_LEVEL_11_0:
1963 case D3D_FEATURE_LEVEL_10_1:
1964 case D3D_FEATURE_LEVEL_10_0:
1965 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1966 default: UNREACHABLE();
1967 return 0;
1968 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001969}
1970
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001971unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1972{
1973 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1974}
1975
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001976unsigned int Renderer11::getReservedVertexUniformVectors() const
1977{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001978 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001979}
1980
1981unsigned int Renderer11::getReservedFragmentUniformVectors() const
1982{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001983 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001984}
1985
1986unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001987{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001988 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1989 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1990 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001991}
1992
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001993unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001994{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001995 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1996 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1997 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001998}
1999
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002000unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002001{
2002 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00002003 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
2004 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002005 switch (mFeatureLevel)
2006 {
2007 case D3D_FEATURE_LEVEL_11_0:
2008 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2009 case D3D_FEATURE_LEVEL_10_1:
2010 case D3D_FEATURE_LEVEL_10_0:
2011 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2012 default: UNREACHABLE();
2013 return 0;
2014 }
2015}
2016
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002017unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2018{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002019 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2020 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2021
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002022 switch (mFeatureLevel)
2023 {
2024 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002025 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002026 case D3D_FEATURE_LEVEL_10_1:
2027 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002028 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002029 default: UNREACHABLE();
2030 return 0;
2031 }
2032}
2033
2034unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2035{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002036 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2037 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2038
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002039 switch (mFeatureLevel)
2040 {
2041 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002042 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002043 case D3D_FEATURE_LEVEL_10_1:
2044 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002045 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002046 default: UNREACHABLE();
2047 return 0;
2048 }
2049}
2050
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002051unsigned int Renderer11::getReservedVertexUniformBuffers() const
2052{
2053 // we reserve one buffer for the application uniforms, and one for driver uniforms
2054 return 2;
2055}
2056
2057unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2058{
2059 // we reserve one buffer for the application uniforms, and one for driver uniforms
2060 return 2;
2061}
2062
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002063unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2064{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002065 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2066 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2067
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002068 switch (mFeatureLevel)
2069 {
2070 case D3D_FEATURE_LEVEL_11_0:
2071 return D3D11_SO_BUFFER_SLOT_COUNT;
2072 case D3D_FEATURE_LEVEL_10_1:
2073 case D3D_FEATURE_LEVEL_10_0:
2074 return D3D10_SO_BUFFER_SLOT_COUNT;
2075 default: UNREACHABLE();
2076 return 0;
2077 }
2078}
2079
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002080unsigned int Renderer11::getMaxUniformBufferSize() const
2081{
2082 // Each component is a 4-element vector of 4-byte units (floats)
2083 const unsigned int bytesPerComponent = 4 * sizeof(float);
2084
2085 switch (mFeatureLevel)
2086 {
2087 case D3D_FEATURE_LEVEL_11_0:
2088 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2089 case D3D_FEATURE_LEVEL_10_1:
2090 case D3D_FEATURE_LEVEL_10_0:
2091 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2092 default: UNREACHABLE();
2093 return 0;
2094 }
2095}
2096
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002097bool Renderer11::getNonPower2TextureSupport() const
2098{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002099 switch (mFeatureLevel)
2100 {
2101 case D3D_FEATURE_LEVEL_11_0:
2102 case D3D_FEATURE_LEVEL_10_1:
2103 case D3D_FEATURE_LEVEL_10_0:
2104 return true;
2105 default: UNREACHABLE();
2106 return false;
2107 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002108}
2109
2110bool Renderer11::getOcclusionQuerySupport() const
2111{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002112 switch (mFeatureLevel)
2113 {
2114 case D3D_FEATURE_LEVEL_11_0:
2115 case D3D_FEATURE_LEVEL_10_1:
2116 case D3D_FEATURE_LEVEL_10_0:
2117 return true;
2118 default: UNREACHABLE();
2119 return false;
2120 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002121}
2122
2123bool Renderer11::getInstancingSupport() const
2124{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002125 switch (mFeatureLevel)
2126 {
2127 case D3D_FEATURE_LEVEL_11_0:
2128 case D3D_FEATURE_LEVEL_10_1:
2129 case D3D_FEATURE_LEVEL_10_0:
2130 return true;
2131 default: UNREACHABLE();
2132 return false;
2133 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002134}
2135
2136bool Renderer11::getShareHandleSupport() const
2137{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002138 // We only currently support share handles with BGRA surfaces, because
2139 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002140 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002141 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002142}
2143
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002144bool Renderer11::getDerivativeInstructionSupport() const
2145{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002146 switch (mFeatureLevel)
2147 {
2148 case D3D_FEATURE_LEVEL_11_0:
2149 case D3D_FEATURE_LEVEL_10_1:
2150 case D3D_FEATURE_LEVEL_10_0:
2151 return true;
2152 default: UNREACHABLE();
2153 return false;
2154 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002155}
2156
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002157bool Renderer11::getPostSubBufferSupport() const
2158{
2159 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2160 return false;
2161}
2162
Jamie Madill13a2f852013-12-11 16:35:08 -05002163int Renderer11::getMaxRecommendedElementsIndices() const
2164{
2165 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2166 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2167
2168 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2169 return std::numeric_limits<GLint>::max();
2170}
2171
2172int Renderer11::getMaxRecommendedElementsVertices() const
2173{
2174 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2175 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2176
2177 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2178 return std::numeric_limits<GLint>::max();
2179}
2180
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002181int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002182{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002183 switch (mFeatureLevel)
2184 {
2185 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002186 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002187 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2188 default: UNREACHABLE(); return 0;
2189 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002190}
2191
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002192int Renderer11::getMinorShaderModel() const
2193{
2194 switch (mFeatureLevel)
2195 {
2196 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2197 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2198 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2199 default: UNREACHABLE(); return 0;
2200 }
2201}
2202
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002203float Renderer11::getMaxPointSize() const
2204{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002205 // choose a reasonable maximum. we enforce this in the shader.
2206 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2207 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002208}
2209
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002210int Renderer11::getMaxViewportDimension() const
2211{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002212 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2213 // In our case return the maximum texture size, which is the maximum render buffer size.
2214 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2215 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2216
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002217 switch (mFeatureLevel)
2218 {
2219 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002220 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002221 case D3D_FEATURE_LEVEL_10_1:
2222 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002223 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002224 default: UNREACHABLE();
2225 return 0;
2226 }
2227}
2228
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002229int Renderer11::getMaxTextureWidth() const
2230{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002231 switch (mFeatureLevel)
2232 {
2233 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2234 case D3D_FEATURE_LEVEL_10_1:
2235 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2236 default: UNREACHABLE(); return 0;
2237 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002238}
2239
2240int Renderer11::getMaxTextureHeight() const
2241{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002242 switch (mFeatureLevel)
2243 {
2244 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2245 case D3D_FEATURE_LEVEL_10_1:
2246 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2247 default: UNREACHABLE(); return 0;
2248 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002249}
2250
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002251int Renderer11::getMaxTextureDepth() const
2252{
2253 switch (mFeatureLevel)
2254 {
2255 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2256 case D3D_FEATURE_LEVEL_10_1:
2257 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2258 default: UNREACHABLE(); return 0;
2259 }
2260}
2261
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002262int Renderer11::getMaxTextureArrayLayers() const
2263{
2264 switch (mFeatureLevel)
2265 {
2266 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2267 case D3D_FEATURE_LEVEL_10_1:
2268 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2269 default: UNREACHABLE(); return 0;
2270 }
2271}
2272
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002273bool Renderer11::get32BitIndexSupport() const
2274{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002275 switch (mFeatureLevel)
2276 {
2277 case D3D_FEATURE_LEVEL_11_0:
2278 case D3D_FEATURE_LEVEL_10_1:
2279 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2280 default: UNREACHABLE(); return false;
2281 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002282}
2283
2284int Renderer11::getMinSwapInterval() const
2285{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002286 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002287}
2288
2289int Renderer11::getMaxSwapInterval() const
2290{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002291 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002292}
2293
2294int Renderer11::getMaxSupportedSamples() const
2295{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002296 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002297}
2298
Geoff Lang005df412013-10-16 14:12:50 -04002299GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002300{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002301 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002302 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2303 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2304}
2305
Geoff Lang005df412013-10-16 14:12:50 -04002306GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002307{
2308 unsigned int numCounts = 0;
2309
2310 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002311 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2312 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002313 {
2314 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2315 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2316
2317 if (iter != mMultisampleSupportMap.end())
2318 {
2319 const MultisampleSupportInfo& info = iter->second;
2320 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2321 {
2322 if (info.qualityLevels[i] > 0)
2323 {
2324 numCounts++;
2325 }
2326 }
2327 }
2328 }
2329
2330 return numCounts;
2331}
2332
Geoff Lang005df412013-10-16 14:12:50 -04002333void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002334{
2335 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002336 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2337 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2338 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002339 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002340 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002341
2342 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2343 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2344
2345 if (iter != mMultisampleSupportMap.end())
2346 {
2347 const MultisampleSupportInfo& info = iter->second;
2348 int bufPos = 0;
2349 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2350 {
2351 if (info.qualityLevels[i] > 0)
2352 {
2353 params[bufPos++] = i + 1;
2354 }
2355 }
2356 }
2357}
2358
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002359int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2360{
2361 if (requested == 0)
2362 {
2363 return 0;
2364 }
2365
2366 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2367 if (iter != mMultisampleSupportMap.end())
2368 {
2369 const MultisampleSupportInfo& info = iter->second;
2370 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2371 {
2372 if (info.qualityLevels[i] > 0)
2373 {
2374 return i + 1;
2375 }
2376 }
2377 }
2378
2379 return -1;
2380}
2381
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002382unsigned int Renderer11::getMaxRenderTargets() const
2383{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002384 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2385 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2386
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002387 switch (mFeatureLevel)
2388 {
2389 case D3D_FEATURE_LEVEL_11_0:
2390 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2391 case D3D_FEATURE_LEVEL_10_1:
2392 case D3D_FEATURE_LEVEL_10_0:
2393 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2394 default:
2395 UNREACHABLE();
2396 return 1;
2397 }
2398}
2399
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002400bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002401{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002402 if (source && dest)
2403 {
2404 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2405 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2406
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002407 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002408
2409 dest11->invalidateSwizzleCache();
2410
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002411 return true;
2412 }
2413
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002414 return false;
2415}
2416
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002417bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002418{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002419 if (source && dest)
2420 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002421 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2422 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002423
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002424 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002425
2426 dest11->invalidateSwizzleCache();
2427
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002428 return true;
2429 }
2430
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002431 return false;
2432}
2433
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002434bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2435{
2436 if (source && dest)
2437 {
2438 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2439 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2440
2441 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002442
2443 dest11->invalidateSwizzleCache();
2444
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002445 return true;
2446 }
2447
2448 return false;
2449}
2450
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002451bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2452{
2453 if (source && dest)
2454 {
2455 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2456 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2457
2458 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002459
2460 dest11->invalidateSwizzleCache();
2461
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002462 return true;
2463 }
2464
2465 return false;
2466}
2467
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002468bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002469 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002470{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002471 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002472 if (!colorbuffer)
2473 {
2474 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002475 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002476 }
2477
2478 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2479 if (!sourceRenderTarget)
2480 {
2481 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002482 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002483 }
2484
2485 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2486 if (!source)
2487 {
2488 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002489 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002490 }
2491
2492 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2493 if (!storage11)
2494 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002495 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002496 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002497 }
2498
2499 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2500 if (!destRenderTarget)
2501 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002502 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002503 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002504 }
2505
2506 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2507 if (!dest)
2508 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002509 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002510 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002511 }
2512
Geoff Langb86b9792013-06-04 16:32:05 -04002513 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2514 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002515
Geoff Langb86b9792013-06-04 16:32:05 -04002516 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2517 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002518
Geoff Langb86b9792013-06-04 16:32:05 -04002519 // Use nearest filtering because source and destination are the same size for the direct
2520 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002521 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002522 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002523
Geoff Lang42477a42013-09-17 17:07:02 -04002524 storage11->invalidateSwizzleCacheLevel(level);
2525
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002526 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002527}
2528
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002529bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002530 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002531{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002532 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002533 if (!colorbuffer)
2534 {
2535 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002536 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002537 }
2538
2539 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2540 if (!sourceRenderTarget)
2541 {
2542 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002543 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002544 }
2545
2546 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2547 if (!source)
2548 {
2549 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002550 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002551 }
2552
2553 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2554 if (!storage11)
2555 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002556 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002557 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002558 }
2559
Nicolas Capensb13f8662013-06-04 13:30:19 -04002560 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002561 if (!destRenderTarget)
2562 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002563 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002564 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002565 }
2566
2567 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2568 if (!dest)
2569 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002570 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002571 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002572 }
2573
Geoff Langb86b9792013-06-04 16:32:05 -04002574 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2575 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002576
Geoff Langb86b9792013-06-04 16:32:05 -04002577 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2578 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002579
Geoff Langb86b9792013-06-04 16:32:05 -04002580 // Use nearest filtering because source and destination are the same size for the direct
2581 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002582 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002583 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002584
Geoff Lang42477a42013-09-17 17:07:02 -04002585 storage11->invalidateSwizzleCacheLevel(level);
2586
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002587 return ret;
2588}
2589
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002590bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2591 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2592{
2593 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2594 if (!colorbuffer)
2595 {
2596 ERR("Failed to retrieve the color buffer from the frame buffer.");
2597 return gl::error(GL_OUT_OF_MEMORY, false);
2598 }
2599
2600 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2601 if (!sourceRenderTarget)
2602 {
2603 ERR("Failed to retrieve the render target from the frame buffer.");
2604 return gl::error(GL_OUT_OF_MEMORY, false);
2605 }
2606
2607 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2608 if (!source)
2609 {
2610 ERR("Failed to retrieve the render target view from the render target.");
2611 return gl::error(GL_OUT_OF_MEMORY, false);
2612 }
2613
2614 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2615 if (!storage11)
2616 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002617 ERR("Failed to retrieve the texture storage from the destination.");
2618 return gl::error(GL_OUT_OF_MEMORY, false);
2619 }
2620
2621 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2622 if (!destRenderTarget)
2623 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002624 ERR("Failed to retrieve the render target from the destination storage.");
2625 return gl::error(GL_OUT_OF_MEMORY, false);
2626 }
2627
2628 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2629 if (!dest)
2630 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002631 ERR("Failed to retrieve the render target view from the destination render target.");
2632 return gl::error(GL_OUT_OF_MEMORY, false);
2633 }
2634
Geoff Langb86b9792013-06-04 16:32:05 -04002635 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2636 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002637
Geoff Langb86b9792013-06-04 16:32:05 -04002638 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2639 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002640
Geoff Langb86b9792013-06-04 16:32:05 -04002641 // Use nearest filtering because source and destination are the same size for the direct
2642 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002643 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002644 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002645
Geoff Lang42477a42013-09-17 17:07:02 -04002646 storage11->invalidateSwizzleCacheLevel(level);
2647
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002648 return ret;
2649}
2650
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002651bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2652 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2653{
2654 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2655 if (!colorbuffer)
2656 {
2657 ERR("Failed to retrieve the color buffer from the frame buffer.");
2658 return gl::error(GL_OUT_OF_MEMORY, false);
2659 }
2660
2661 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2662 if (!sourceRenderTarget)
2663 {
2664 ERR("Failed to retrieve the render target from the frame buffer.");
2665 return gl::error(GL_OUT_OF_MEMORY, false);
2666 }
2667
2668 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2669 if (!source)
2670 {
2671 ERR("Failed to retrieve the render target view from the render target.");
2672 return gl::error(GL_OUT_OF_MEMORY, false);
2673 }
2674
2675 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2676 if (!storage11)
2677 {
Geoff Langea228632013-07-30 15:17:12 -04002678 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002679 ERR("Failed to retrieve the texture storage from the destination.");
2680 return gl::error(GL_OUT_OF_MEMORY, false);
2681 }
2682
2683 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2684 if (!destRenderTarget)
2685 {
Geoff Langea228632013-07-30 15:17:12 -04002686 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002687 ERR("Failed to retrieve the render target from the destination storage.");
2688 return gl::error(GL_OUT_OF_MEMORY, false);
2689 }
2690
2691 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2692 if (!dest)
2693 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002694 ERR("Failed to retrieve the render target view from the destination render target.");
2695 return gl::error(GL_OUT_OF_MEMORY, false);
2696 }
2697
Geoff Langb86b9792013-06-04 16:32:05 -04002698 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2699 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002700
Geoff Langb86b9792013-06-04 16:32:05 -04002701 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2702 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002703
Geoff Langb86b9792013-06-04 16:32:05 -04002704 // Use nearest filtering because source and destination are the same size for the direct
2705 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002706 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002707 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002708
Geoff Lang42477a42013-09-17 17:07:02 -04002709 storage11->invalidateSwizzleCacheLevel(level);
2710
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002711 return ret;
2712}
2713
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002714void Renderer11::unapplyRenderTargets()
2715{
2716 setOneTimeRenderTarget(NULL);
2717}
2718
2719void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2720{
2721 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2722
2723 rtvArray[0] = renderTargetView;
2724
2725 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2726
2727 // Do not preserve the serial for this one-time-use render target
2728 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2729 {
2730 mAppliedRenderTargetSerials[rtIndex] = 0;
2731 }
2732}
2733
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002734RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2735{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002736 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002737 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002738
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002739 if (depth)
2740 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002741 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002742 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002743 swapChain11->getDepthStencilTexture(),
2744 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002745 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002746 }
2747 else
2748 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002749 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002750 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002751 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002752 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002753 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002754 }
2755 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002756}
2757
Geoff Langa2d97f12013-06-11 11:44:02 -04002758RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002759{
Geoff Langa2d97f12013-06-11 11:44:02 -04002760 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002761 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002762}
2763
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002764ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002765{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002766 ShaderExecutable11 *executable = NULL;
2767
2768 switch (type)
2769 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002770 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002771 {
2772 ID3D11VertexShader *vshader = NULL;
2773 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2774 ASSERT(SUCCEEDED(result));
2775
2776 if (vshader)
2777 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002778 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002779 }
2780 }
2781 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002782 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002783 {
2784 ID3D11PixelShader *pshader = NULL;
2785 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2786 ASSERT(SUCCEEDED(result));
2787
2788 if (pshader)
2789 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002790 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002791 }
2792 }
2793 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002794 case rx::SHADER_GEOMETRY:
2795 {
2796 ID3D11GeometryShader *gshader = NULL;
2797 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2798 ASSERT(SUCCEEDED(result));
2799
2800 if (gshader)
2801 {
2802 executable = new ShaderExecutable11(function, length, gshader);
2803 }
2804 }
2805 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002806 default:
2807 UNREACHABLE();
2808 break;
2809 }
2810
2811 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002812}
2813
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002814ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002815{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002816 const char *profile = NULL;
2817
2818 switch (type)
2819 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002820 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002821 profile = "vs_4_0";
2822 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002823 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002824 profile = "ps_4_0";
2825 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002826 case rx::SHADER_GEOMETRY:
2827 profile = "gs_4_0";
2828 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002829 default:
2830 UNREACHABLE();
2831 return NULL;
2832 }
2833
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00002834 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002835 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002836 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002837 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002838 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002839
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002840 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002841 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002842
2843 return executable;
2844}
2845
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002846rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2847{
2848 return new UniformStorage11(this, storageSize);
2849}
2850
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002851VertexBuffer *Renderer11::createVertexBuffer()
2852{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002853 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002854}
2855
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002856IndexBuffer *Renderer11::createIndexBuffer()
2857{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002858 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002859}
2860
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002861BufferStorage *Renderer11::createBufferStorage()
2862{
2863 return new BufferStorage11(this);
2864}
2865
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002866QueryImpl *Renderer11::createQuery(GLenum type)
2867{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002868 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002869}
2870
2871FenceImpl *Renderer11::createFence()
2872{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002873 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002874}
2875
Geoff Lang005df412013-10-16 14:12:50 -04002876bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002877{
Jamie Madill4461f092013-10-10 15:10:39 -04002878 int clientVersion = getCurrentClientVersion();
2879
2880 // We only support buffer to texture copies in ES3
2881 if (clientVersion <= 2)
2882 {
2883 return false;
2884 }
2885
2886 // sRGB formats do not work with D3D11 buffer SRVs
2887 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2888 {
2889 return false;
2890 }
2891
2892 // We cannot support direct copies to non-color-renderable formats
2893 if (!gl::IsColorRenderingSupported(internalFormat, this))
2894 {
2895 return false;
2896 }
2897
2898 // We skip all 3-channel formats since sometimes format support is missing
2899 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2900 {
2901 return false;
2902 }
2903
2904 // We don't support formats which we can't represent without conversion
2905 if (getNativeTextureFormat(internalFormat) != internalFormat)
2906 {
2907 return false;
2908 }
2909
2910 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002911}
2912
Jamie Madilla21eea32013-09-18 14:36:25 -04002913bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2914 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2915{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002916 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002917 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2918}
2919
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002920bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002921{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002922 ASSERT(colorbuffer != NULL);
2923
2924 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2925 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002926 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002927 *subresourceIndex = renderTarget->getSubresourceIndex();
2928
2929 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2930 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002931 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002932 ID3D11Resource *textureResource = NULL;
2933 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002934
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002935 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002936 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002937 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002938 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002939
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002940 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002941 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002942 return true;
2943 }
2944 else
2945 {
2946 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2947 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002948 }
2949 }
2950 }
2951 }
2952
2953 return false;
2954}
2955
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002956bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002957 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002958{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002959 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002960 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002961 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002962
2963 if (!readBuffer)
2964 {
2965 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2966 return gl::error(GL_OUT_OF_MEMORY, false);
2967 }
2968
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002969 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002970
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002971 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002972 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002973 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2974 {
2975 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
2976
2977 if (!drawBuffer)
2978 {
2979 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2980 return gl::error(GL_OUT_OF_MEMORY, false);
2981 }
2982
2983 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2984
Geoff Lang125deab2013-08-09 13:34:16 -04002985 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002986 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002987 {
2988 return false;
2989 }
2990 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002991 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002992 }
2993
Geoff Lang685806d2013-06-12 11:16:36 -04002994 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002995 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002996 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
2997 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
2998
2999 if (!readBuffer)
3000 {
3001 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
3002 return gl::error(GL_OUT_OF_MEMORY, false);
3003 }
3004
3005 if (!drawBuffer)
3006 {
3007 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
3008 return gl::error(GL_OUT_OF_MEMORY, false);
3009 }
3010
3011 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3012 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3013
Geoff Lang125deab2013-08-09 13:34:16 -04003014 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04003015 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003016 {
3017 return false;
3018 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003019 }
3020
Geoff Lang42477a42013-09-17 17:07:02 -04003021 invalidateFramebufferSwizzles(drawTarget);
3022
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003023 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003024}
3025
3026void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3027 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3028{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003029 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003030 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003031
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003032 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3033
3034 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003035 {
3036 gl::Rectangle area;
3037 area.x = x;
3038 area.y = y;
3039 area.width = width;
3040 area.height = height;
3041
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003042 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3043 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003044
Geoff Langea228632013-07-30 15:17:12 -04003045 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003046 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003047}
3048
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003049Image *Renderer11::createImage()
3050{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003051 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003052}
3053
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003054void Renderer11::generateMipmap(Image *dest, Image *src)
3055{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003056 Image11 *dest11 = Image11::makeImage11(dest);
3057 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003058 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003059}
3060
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003061TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3062{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003063 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3064 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003065}
3066
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003067TextureStorage *Renderer11::createTextureStorage2D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003068{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003069 return new TextureStorage11_2D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003070}
3071
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003072TextureStorage *Renderer11::createTextureStorageCube(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, int size)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003073{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003074 return new TextureStorage11_Cube(this, baseLevel, maxLevel, internalformat, renderTarget, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003075}
3076
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003077TextureStorage *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 +00003078{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003079 return new TextureStorage11_3D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003080}
3081
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003082TextureStorage *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 +00003083{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003084 return new TextureStorage11_2DArray(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003085}
3086
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003087void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3088 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3089 GLint packAlignment, void *pixels)
3090{
3091 D3D11_TEXTURE2D_DESC textureDesc;
3092 texture->GetDesc(&textureDesc);
3093
3094 D3D11_TEXTURE2D_DESC stagingDesc;
3095 stagingDesc.Width = area.width;
3096 stagingDesc.Height = area.height;
3097 stagingDesc.MipLevels = 1;
3098 stagingDesc.ArraySize = 1;
3099 stagingDesc.Format = textureDesc.Format;
3100 stagingDesc.SampleDesc.Count = 1;
3101 stagingDesc.SampleDesc.Quality = 0;
3102 stagingDesc.Usage = D3D11_USAGE_STAGING;
3103 stagingDesc.BindFlags = 0;
3104 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3105 stagingDesc.MiscFlags = 0;
3106
3107 ID3D11Texture2D* stagingTex = NULL;
3108 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3109 if (FAILED(result))
3110 {
3111 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3112 return;
3113 }
3114
3115 ID3D11Texture2D* srcTex = NULL;
3116 if (textureDesc.SampleDesc.Count > 1)
3117 {
3118 D3D11_TEXTURE2D_DESC resolveDesc;
3119 resolveDesc.Width = textureDesc.Width;
3120 resolveDesc.Height = textureDesc.Height;
3121 resolveDesc.MipLevels = 1;
3122 resolveDesc.ArraySize = 1;
3123 resolveDesc.Format = textureDesc.Format;
3124 resolveDesc.SampleDesc.Count = 1;
3125 resolveDesc.SampleDesc.Quality = 0;
3126 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3127 resolveDesc.BindFlags = 0;
3128 resolveDesc.CPUAccessFlags = 0;
3129 resolveDesc.MiscFlags = 0;
3130
3131 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3132 if (FAILED(result))
3133 {
3134 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003135 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003136 return;
3137 }
3138
3139 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3140 subResource = 0;
3141 }
3142 else
3143 {
3144 srcTex = texture;
3145 srcTex->AddRef();
3146 }
3147
3148 D3D11_BOX srcBox;
3149 srcBox.left = area.x;
3150 srcBox.right = area.x + area.width;
3151 srcBox.top = area.y;
3152 srcBox.bottom = area.y + area.height;
3153 srcBox.front = 0;
3154 srcBox.back = 1;
3155
3156 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3157
Geoff Langea228632013-07-30 15:17:12 -04003158 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003159
3160 D3D11_MAPPED_SUBRESOURCE mapping;
3161 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3162
3163 unsigned char *source;
3164 int inputPitch;
3165 if (packReverseRowOrder)
3166 {
3167 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3168 inputPitch = -static_cast<int>(mapping.RowPitch);
3169 }
3170 else
3171 {
3172 source = static_cast<unsigned char*>(mapping.pData);
3173 inputPitch = static_cast<int>(mapping.RowPitch);
3174 }
3175
Geoff Lang697ad3e2013-06-04 10:11:28 -04003176 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003177
Geoff Lang005df412013-10-16 14:12:50 -04003178 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003179 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3180 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3181
3182 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3183
3184 if (sourceFormat == format && sourceType == type)
3185 {
3186 // Direct copy possible
3187 unsigned char *dest = static_cast<unsigned char*>(pixels);
3188 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003189 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003190 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003191 }
3192 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003193 else
3194 {
Geoff Lang005df412013-10-16 14:12:50 -04003195 GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003196 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3197
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003198 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003199 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003200 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003201 // Fast copy is possible through some special function
3202 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003203 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003204 for (int x = 0; x < area.width; x++)
3205 {
3206 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3207 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3208
3209 fastCopyFunc(src, dest);
3210 }
3211 }
3212 }
3213 else
3214 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003215 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003216 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3217
3218 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3219 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3220 sizeof(temp) >= sizeof(gl::ColorUI) &&
3221 sizeof(temp) >= sizeof(gl::ColorI));
3222
3223 for (int y = 0; y < area.height; y++)
3224 {
3225 for (int x = 0; x < area.width; x++)
3226 {
3227 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3228 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3229
3230 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3231 // will not allow the copy otherwise.
3232 readFunc(src, temp);
3233 writeFunc(temp, dest);
3234 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003235 }
3236 }
3237 }
3238
3239 mDeviceContext->Unmap(stagingTex, 0);
3240
Geoff Langea228632013-07-30 15:17:12 -04003241 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003242}
3243
Geoff Lang758d5b22013-06-11 11:42:50 -04003244bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003245 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3246 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003247{
Geoff Lang975af372013-06-12 11:19:22 -04003248 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3249 // it should never be the case that both color and depth/stencil need to be blitted at
3250 // at the same time.
3251 ASSERT(colorBlit != (depthBlit || stencilBlit));
3252
Geoff Langc1f51be2013-06-11 11:49:14 -04003253 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003254
Geoff Lang4d782732013-07-22 10:44:18 -04003255 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3256 if (!drawRenderTarget)
3257 {
3258 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3259 return gl::error(GL_OUT_OF_MEMORY, false);
3260 }
3261
3262 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3263 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3264 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3265 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3266
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003267 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3268 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003269 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003270 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003271 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003272 }
3273
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003274 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003275 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003276 unsigned int readSubresource = 0;
3277 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003278 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003279 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3280 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003281
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003282 if (unresolvedTexture)
3283 {
3284 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3285 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003286
Geoff Langea228632013-07-30 15:17:12 -04003287 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003288
3289 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3290 if (FAILED(result))
3291 {
Geoff Langea228632013-07-30 15:17:12 -04003292 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003293 return gl::error(GL_OUT_OF_MEMORY, false);
3294 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003295 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003296 }
3297 else
3298 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003299 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003300 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003301 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003302 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003303 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003304 }
3305
Geoff Lang4d782732013-07-22 10:44:18 -04003306 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003307 {
Geoff Lang4d782732013-07-22 10:44:18 -04003308 SafeRelease(readTexture);
3309 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003310 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003311 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003312 }
3313
Geoff Lang125deab2013-08-09 13:34:16 -04003314 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3315 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3316
3317 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3318
3319 bool wholeBufferCopy = !scissorNeeded &&
3320 readRect.x == 0 && readRect.width == readSize.width &&
3321 readRect.y == 0 && readRect.height == readSize.height &&
3322 drawRect.x == 0 && drawRect.width == drawSize.width &&
3323 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003324
Geoff Langc1f51be2013-06-11 11:49:14 -04003325 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003326
Geoff Lang125deab2013-08-09 13:34:16 -04003327 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || readRect.height < 0;
3328
3329 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3330 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3331 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3332 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3333
3334 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3335 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3336 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3337
Geoff Langc1f51be2013-06-11 11:49:14 -04003338 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003339 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3340 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003341 {
Geoff Lang125deab2013-08-09 13:34:16 -04003342 UINT dstX = drawRect.x;
3343 UINT dstY = drawRect.y;
3344
Geoff Langc1f51be2013-06-11 11:49:14 -04003345 D3D11_BOX readBox;
3346 readBox.left = readRect.x;
3347 readBox.right = readRect.x + readRect.width;
3348 readBox.top = readRect.y;
3349 readBox.bottom = readRect.y + readRect.height;
3350 readBox.front = 0;
3351 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003352
Geoff Lang125deab2013-08-09 13:34:16 -04003353 if (scissorNeeded)
3354 {
3355 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3356 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3357
3358 if (drawRect.x < scissor->x)
3359 {
3360 dstX = scissor->x;
3361 readBox.left += (scissor->x - drawRect.x);
3362 }
3363 if (drawRect.y < scissor->y)
3364 {
3365 dstY = scissor->y;
3366 readBox.top += (scissor->y - drawRect.y);
3367 }
3368 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3369 {
3370 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3371 }
3372 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3373 {
3374 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3375 }
3376 }
3377
Geoff Langc1f51be2013-06-11 11:49:14 -04003378 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3379 // We also require complete framebuffer copies for depth-stencil blit.
3380 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003381
Geoff Lang125deab2013-08-09 13:34:16 -04003382 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003383 readTexture, readSubresource, pSrcBox);
3384 result = true;
3385 }
3386 else
3387 {
3388 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003389 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003390
Geoff Lang975af372013-06-12 11:19:22 -04003391 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003392 {
Geoff Lang975af372013-06-12 11:19:22 -04003393 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003394 drawTexture, drawSubresource, drawArea, drawSize,
3395 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003396 }
3397 else if (depthBlit)
3398 {
Geoff Lang125deab2013-08-09 13:34:16 -04003399 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3400 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003401 }
3402 else if (stencilBlit)
3403 {
3404 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003405 drawTexture, drawSubresource, drawArea, drawSize,
3406 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003407 }
3408 else
3409 {
Geoff Lang685806d2013-06-12 11:16:36 -04003410 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003411 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3412 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003413 }
3414 }
3415
3416 SafeRelease(readTexture);
3417 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003418
3419 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003420}
3421
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003422ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3423{
3424 D3D11_TEXTURE2D_DESC textureDesc;
3425 source->GetDesc(&textureDesc);
3426
3427 if (textureDesc.SampleDesc.Count > 1)
3428 {
3429 D3D11_TEXTURE2D_DESC resolveDesc;
3430 resolveDesc.Width = textureDesc.Width;
3431 resolveDesc.Height = textureDesc.Height;
3432 resolveDesc.MipLevels = 1;
3433 resolveDesc.ArraySize = 1;
3434 resolveDesc.Format = textureDesc.Format;
3435 resolveDesc.SampleDesc.Count = 1;
3436 resolveDesc.SampleDesc.Quality = 0;
3437 resolveDesc.Usage = textureDesc.Usage;
3438 resolveDesc.BindFlags = textureDesc.BindFlags;
3439 resolveDesc.CPUAccessFlags = 0;
3440 resolveDesc.MiscFlags = 0;
3441
3442 ID3D11Texture2D *resolveTexture = NULL;
3443 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3444 if (FAILED(result))
3445 {
3446 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3447 return NULL;
3448 }
3449
3450 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3451 return resolveTexture;
3452 }
3453 else
3454 {
3455 source->AddRef();
3456 return source;
3457 }
3458}
3459
Geoff Lang42477a42013-09-17 17:07:02 -04003460void Renderer11::invalidateRenderbufferSwizzles(gl::Renderbuffer *renderBuffer, int mipLevel)
3461{
3462 TextureStorage *texStorage = renderBuffer->getTextureStorage();
3463 if (texStorage)
3464 {
3465 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3466 if (!texStorage11)
3467 {
3468 ERR("texture storage pointer unexpectedly null.");
3469 return;
3470 }
3471
3472 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3473 }
3474}
3475
3476void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3477{
3478 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3479 {
3480 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
3481 if (colorbuffer)
3482 {
3483 invalidateRenderbufferSwizzles(colorbuffer, framebuffer->getColorbufferMipLevel(colorAttachment));
3484 }
3485 }
3486
3487 gl::Renderbuffer *depthBuffer = framebuffer->getDepthbuffer();
3488 if (depthBuffer)
3489 {
3490 invalidateRenderbufferSwizzles(depthBuffer, framebuffer->getDepthbufferMipLevel());
3491 }
3492
3493 gl::Renderbuffer *stencilBuffer = framebuffer->getStencilbuffer();
3494 if (stencilBuffer)
3495 {
3496 invalidateRenderbufferSwizzles(stencilBuffer, framebuffer->getStencilbufferMipLevel());
3497 }
3498}
3499
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003500bool Renderer11::getLUID(LUID *adapterLuid) const
3501{
3502 adapterLuid->HighPart = 0;
3503 adapterLuid->LowPart = 0;
3504
3505 if (!mDxgiAdapter)
3506 {
3507 return false;
3508 }
3509
3510 DXGI_ADAPTER_DESC adapterDesc;
3511 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3512 {
3513 return false;
3514 }
3515
3516 *adapterLuid = adapterDesc.AdapterLuid;
3517 return true;
3518}
3519
Geoff Lang005df412013-10-16 14:12:50 -04003520GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003521{
3522 int clientVersion = getCurrentClientVersion();
3523 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3524}
3525
Jamie Madill95ffb862014-01-29 09:26:59 -05003526rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3527{
3528 return gl_d3d11::GetVertexConversionType(vertexFormat);
3529}
3530
3531GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3532{
3533 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3534}
3535
Geoff Lang61e49a52013-05-29 10:22:58 -04003536Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3537{
3538 MultisampleSupportInfo supportInfo = { 0 };
3539
3540 UINT formatSupport;
3541 HRESULT result;
3542
3543 result = mDevice->CheckFormatSupport(format, &formatSupport);
3544 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3545 {
3546 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3547 {
3548 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3549 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3550 {
3551 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3552 }
3553 else
3554 {
3555 supportInfo.qualityLevels[i - 1] = 0;
3556 }
3557 }
3558 }
3559
3560 return supportInfo;
3561}
3562
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003563}