blob: f9c84dd7b726343c4d4cc8ec070f6022a7576055 [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{
Geoff Langdad5ed32014-02-10 12:59:17 -0500116 if (!mCompiler.initialize())
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000117 {
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);
Geoff Lang0bf3a982014-02-11 09:40:48 -0500795 UINT dxStencilRef = std::min<UINT>(stencilRef, 0xFFu);
Jamie Madillec91cd32014-01-21 16:38:12 -0500796
Geoff Lang0bf3a982014-02-11 09:40:48 -0500797 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, dxStencilRef);
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
Jamie Madillc5a83002014-02-14 16:41:25 -05001406void Renderer11::applyShaders(gl::ProgramBinary *programBinary, bool rasterizerDiscard, const gl::VertexFormat inputLayout[])
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001407{
Jamie Madillc5a83002014-02-14 16:41:25 -05001408 ShaderExecutable *vertexExe = programBinary->getVertexExecutableForInputLayout(inputLayout);
Jamie Madill6246dc82014-01-29 09:26:47 -05001409 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
Geoff Lang0550d032014-01-30 11:29:07 -05001422 // Skip pixel shader if we're doing rasterizer discard.
1423 if (rasterizerDiscard)
1424 {
1425 pixelShader = NULL;
1426 }
1427
Jamie Madill6246dc82014-01-29 09:26:47 -05001428 bool dirtyUniforms = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001429
Jamie Madill6246dc82014-01-29 09:26:47 -05001430 if (vertexShader != mAppliedVertexShader)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001431 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001432 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
1433 mAppliedVertexShader = vertexShader;
1434 dirtyUniforms = true;
1435 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001436
Jamie Madill6246dc82014-01-29 09:26:47 -05001437 if (geometryShader != mAppliedGeometryShader)
1438 {
1439 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1440 mAppliedGeometryShader = geometryShader;
1441 dirtyUniforms = true;
1442 }
1443
1444 if (pixelShader != mAppliedPixelShader)
1445 {
1446 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1447 mAppliedPixelShader = pixelShader;
1448 dirtyUniforms = true;
1449 }
1450
1451 if (dirtyUniforms)
1452 {
1453 programBinary->dirtyAllUniforms();
daniel@transgaming.come4991412012-12-20 20:55:34 +00001454 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001455}
1456
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001457void Renderer11::applyUniforms(const gl::ProgramBinary &programBinary)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001458{
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001459 const gl::UniformArray &uniformArray = programBinary.getUniforms();
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001460
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001461 unsigned int totalRegisterCountVS = 0;
1462 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001463
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001464 bool vertexUniformsDirty = false;
1465 bool pixelUniformsDirty = false;
1466
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001467 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001468 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001469 const gl::Uniform &uniform = *uniformArray[uniformIndex];
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001470
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001471 if (uniform.isReferencedByVertexShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001472 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001473 totalRegisterCountVS += uniform.registerCount;
1474 vertexUniformsDirty = (vertexUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001475 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001476
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001477 if (uniform.isReferencedByFragmentShader() && !uniform.isSampler())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001478 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001479 totalRegisterCountPS += uniform.registerCount;
1480 pixelUniformsDirty = (pixelUniformsDirty || uniform.dirty);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001481 }
1482 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001483
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001484 const UniformStorage11 *vertexUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getVertexUniformStorage());
1485 const UniformStorage11 *fragmentUniformStorage = UniformStorage11::makeUniformStorage11(&programBinary.getFragmentUniformStorage());
1486 ASSERT(vertexUniformStorage);
1487 ASSERT(fragmentUniformStorage);
1488
1489 ID3D11Buffer *vertexConstantBuffer = vertexUniformStorage->getConstantBuffer();
1490 ID3D11Buffer *pixelConstantBuffer = fragmentUniformStorage->getConstantBuffer();
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001491
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001492 float (*mapVS)[4] = NULL;
1493 float (*mapPS)[4] = NULL;
1494
Shannon Woods5ab33c82013-06-26 15:31:09 -04001495 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1496 {
1497 D3D11_MAPPED_SUBRESOURCE map = {0};
1498 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1499 ASSERT(SUCCEEDED(result));
1500 mapVS = (float(*)[4])map.pData;
1501 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001502
Shannon Woods5ab33c82013-06-26 15:31:09 -04001503 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1504 {
1505 D3D11_MAPPED_SUBRESOURCE map = {0};
1506 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1507 ASSERT(SUCCEEDED(result));
1508 mapPS = (float(*)[4])map.pData;
1509 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001510
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001511 for (size_t uniformIndex = 0; uniformIndex < uniformArray.size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001512 {
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001513 gl::Uniform *uniform = uniformArray[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001514
Jamie Madill8ff21ae2014-02-04 16:04:05 -05001515 if (!uniform->isSampler())
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001516 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001517 unsigned int componentCount = (4 - uniform->registerElement);
1518
Jamie Madill71cc91f2013-09-18 12:51:22 -04001519 // 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 -04001520 // overwrite previously written regions of memory.
Jamie Madill5b085dc2013-08-30 13:21:11 -04001521
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001522 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001523 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001524 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001525 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001526
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001527 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001528 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001529 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001530 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001531 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001532 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001533
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001534 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001535 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001536 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001537 }
1538
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001539 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001540 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001541 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001542 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001543
1544 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1545 {
1546 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1547 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1548 }
1549
1550 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1551 {
1552 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1553 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1554 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001555
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001556 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001557 if (!mDriverConstantBufferVS)
1558 {
1559 D3D11_BUFFER_DESC constantBufferDescription = {0};
1560 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1561 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1562 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1563 constantBufferDescription.CPUAccessFlags = 0;
1564 constantBufferDescription.MiscFlags = 0;
1565 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001566
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001567 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001568 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001569
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001570 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1571 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001572
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001573 if (!mDriverConstantBufferPS)
1574 {
1575 D3D11_BUFFER_DESC constantBufferDescription = {0};
1576 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1577 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1578 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1579 constantBufferDescription.CPUAccessFlags = 0;
1580 constantBufferDescription.MiscFlags = 0;
1581 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001582
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001583 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001584 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001585
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001586 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1587 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001588
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001589 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1590 {
1591 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1592 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1593 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001594
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001595 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1596 {
1597 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1598 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1599 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001600
1601 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001602 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1603 {
1604 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1605 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1606 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001607}
1608
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001609void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001610{
Geoff Langda507fe2013-08-20 12:01:42 -04001611 mClear->clearFramebuffer(clearParams, frameBuffer);
Geoff Lang42477a42013-09-17 17:07:02 -04001612 invalidateFramebufferSwizzles(frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001613}
1614
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001615void Renderer11::markAllStateDirty()
1616{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001617 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1618 {
1619 mAppliedRenderTargetSerials[rtIndex] = 0;
1620 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001621 mAppliedDepthbufferSerial = 0;
1622 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001623 mDepthStencilInitialized = false;
1624 mRenderTargetDescInitialized = false;
1625
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001626 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001627 {
1628 mForceSetVertexSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001629 mCurVertexSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001630 }
1631 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1632 {
1633 mForceSetPixelSamplerStates[i] = true;
Geoff Lang91382e52014-01-07 16:16:30 -05001634 mCurPixelSRVs[i] = NULL;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001635 }
1636
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001637 mForceSetBlendState = true;
1638 mForceSetRasterState = true;
1639 mForceSetDepthStencilState = true;
1640 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001641 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001642
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001643 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001644 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001645 mAppliedIBOffset = 0;
1646
Jamie Madill6246dc82014-01-29 09:26:47 -05001647 mAppliedVertexShader = NULL;
1648 mAppliedGeometryShader = NULL;
1649 mAppliedPixelShader = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001650 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1651 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001652
1653 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001654
1655 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1656 {
1657 mCurrentConstantBufferVS[i] = -1;
1658 mCurrentConstantBufferPS[i] = -1;
1659 }
1660
1661 mCurrentVertexConstantBuffer = NULL;
1662 mCurrentPixelConstantBuffer = NULL;
1663 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001664
1665 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001666}
1667
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001668void Renderer11::releaseDeviceResources()
1669{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001670 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001671 mInputLayoutCache.clear();
1672
Geoff Langea228632013-07-30 15:17:12 -04001673 SafeDelete(mVertexDataManager);
1674 SafeDelete(mIndexDataManager);
1675 SafeDelete(mLineLoopIB);
1676 SafeDelete(mTriangleFanIB);
1677 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001678 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001679 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001680
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001681 SafeRelease(mDriverConstantBufferVS);
1682 SafeRelease(mDriverConstantBufferPS);
1683 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001684}
1685
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001686void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001687{
1688 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001689 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001690}
1691
1692bool Renderer11::isDeviceLost()
1693{
1694 return mDeviceLost;
1695}
1696
1697// set notify to true to broadcast a message to all contexts of the device loss
1698bool Renderer11::testDeviceLost(bool notify)
1699{
1700 bool isLost = false;
1701
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001702 // GetRemovedReason is used to test if the device is removed
1703 HRESULT result = mDevice->GetDeviceRemovedReason();
1704 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001705
1706 if (isLost)
1707 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001708 // Log error if this is a new device lost event
1709 if (mDeviceLost == false)
1710 {
1711 ERR("The D3D11 device was removed: 0x%08X", result);
1712 }
1713
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001714 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001715 // we'll probably get this done again by notifyDeviceLost
1716 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001717 // Note that we don't want to clear the device loss status here
1718 // -- this needs to be done by resetDevice
1719 mDeviceLost = true;
1720 if (notify)
1721 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001722 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001723 }
1724 }
1725
1726 return isLost;
1727}
1728
1729bool Renderer11::testDeviceResettable()
1730{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001731 // determine if the device is resettable by creating a dummy device
1732 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001733
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001734 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001735 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001736 return false;
1737 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001738
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001739 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001740 {
1741 D3D_FEATURE_LEVEL_11_0,
1742 D3D_FEATURE_LEVEL_10_1,
1743 D3D_FEATURE_LEVEL_10_0,
1744 };
1745
1746 ID3D11Device* dummyDevice;
1747 D3D_FEATURE_LEVEL dummyFeatureLevel;
1748 ID3D11DeviceContext* dummyContext;
1749
1750 HRESULT result = D3D11CreateDevice(NULL,
1751 D3D_DRIVER_TYPE_HARDWARE,
1752 NULL,
1753 #if defined(_DEBUG)
1754 D3D11_CREATE_DEVICE_DEBUG,
1755 #else
1756 0,
1757 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001758 featureLevels,
1759 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001760 D3D11_SDK_VERSION,
1761 &dummyDevice,
1762 &dummyFeatureLevel,
1763 &dummyContext);
1764
1765 if (!mDevice || FAILED(result))
1766 {
1767 return false;
1768 }
1769
Geoff Langea228632013-07-30 15:17:12 -04001770 SafeRelease(dummyContext);
1771 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001772
1773 return true;
1774}
1775
1776void Renderer11::release()
1777{
1778 releaseDeviceResources();
1779
Geoff Langea228632013-07-30 15:17:12 -04001780 SafeRelease(mDxgiFactory);
1781 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001782
1783 if (mDeviceContext)
1784 {
1785 mDeviceContext->ClearState();
1786 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001787 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001788 }
1789
Geoff Langea228632013-07-30 15:17:12 -04001790 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001791
1792 if (mD3d11Module)
1793 {
1794 FreeLibrary(mD3d11Module);
1795 mD3d11Module = NULL;
1796 }
1797
1798 if (mDxgiModule)
1799 {
1800 FreeLibrary(mDxgiModule);
1801 mDxgiModule = NULL;
1802 }
Geoff Langdad5ed32014-02-10 12:59:17 -05001803
1804 mCompiler.release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001805}
1806
1807bool Renderer11::resetDevice()
1808{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001809 // recreate everything
1810 release();
1811 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001812
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001813 if (result != EGL_SUCCESS)
1814 {
1815 ERR("Could not reinitialize D3D11 device: %08X", result);
1816 return false;
1817 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001818
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001819 mDeviceLost = false;
1820
1821 return true;
1822}
1823
1824DWORD Renderer11::getAdapterVendor() const
1825{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001826 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001827}
1828
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001829std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001830{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001831 std::ostringstream rendererString;
1832
1833 rendererString << mDescription;
1834 rendererString << " Direct3D11";
1835
1836 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1837 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1838
1839 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001840}
1841
1842GUID Renderer11::getAdapterIdentifier() const
1843{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001844 // Use the adapter LUID as our adapter ID
1845 // This number is local to a machine is only guaranteed to be unique between restarts
1846 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1847 GUID adapterId = {0};
1848 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1849 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001850}
1851
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001852bool Renderer11::getBGRATextureSupport() const
1853{
1854 return mBGRATextureSupport;
1855}
1856
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001857bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001858{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001859 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001860}
1861
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001862bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001863{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001864 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001865}
1866
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001867bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001868{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001869 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001870}
1871
1872bool Renderer11::getDepthTextureSupport() const
1873{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001874 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001875}
1876
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001877bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001878{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001879 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001880}
1881
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001882bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001883{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001884 return mFloat32FilterSupport;
1885}
1886
1887bool Renderer11::getFloat32TextureRenderingSupport() const
1888{
1889 return mFloat32RenderSupport;
1890}
1891
1892bool Renderer11::getFloat16TextureSupport() const
1893{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001894 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001895}
1896
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001897bool Renderer11::getFloat16TextureFilteringSupport() const
1898{
1899 return mFloat16FilterSupport;
1900}
1901
1902bool Renderer11::getFloat16TextureRenderingSupport() const
1903{
1904 return mFloat16RenderSupport;
1905}
1906
Geoff Langd42cf4e2013-06-05 16:09:17 -04001907bool Renderer11::getRGB565TextureSupport() const
1908{
1909 return false;
1910}
1911
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001912bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001913{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001914 return false;
1915}
1916
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001917bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001918{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001919 return false;
1920}
1921
Geoff Lang632192d2013-10-04 13:40:46 -04001922bool Renderer11::getRGTextureSupport() const
1923{
1924 return mRGTextureSupport;
1925}
1926
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001927bool Renderer11::getTextureFilterAnisotropySupport() const
1928{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001929 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001930}
1931
1932float Renderer11::getTextureMaxAnisotropy() const
1933{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001934 switch (mFeatureLevel)
1935 {
1936 case D3D_FEATURE_LEVEL_11_0:
1937 return D3D11_MAX_MAXANISOTROPY;
1938 case D3D_FEATURE_LEVEL_10_1:
1939 case D3D_FEATURE_LEVEL_10_0:
1940 return D3D10_MAX_MAXANISOTROPY;
1941 default: UNREACHABLE();
1942 return 0;
1943 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001944}
1945
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001946bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001947{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00001948 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001949}
1950
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001951Range Renderer11::getViewportBounds() const
1952{
1953 switch (mFeatureLevel)
1954 {
1955 case D3D_FEATURE_LEVEL_11_0:
1956 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1957 case D3D_FEATURE_LEVEL_10_1:
1958 case D3D_FEATURE_LEVEL_10_0:
1959 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1960 default: UNREACHABLE();
1961 return Range(0, 0);
1962 }
1963}
1964
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001965unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001966{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001967 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1968 switch (mFeatureLevel)
1969 {
1970 case D3D_FEATURE_LEVEL_11_0:
1971 case D3D_FEATURE_LEVEL_10_1:
1972 case D3D_FEATURE_LEVEL_10_0:
1973 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1974 default: UNREACHABLE();
1975 return 0;
1976 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001977}
1978
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001979unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1980{
1981 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1982}
1983
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001984unsigned int Renderer11::getReservedVertexUniformVectors() const
1985{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001986 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001987}
1988
1989unsigned int Renderer11::getReservedFragmentUniformVectors() const
1990{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001991 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001992}
1993
1994unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001995{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001996 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1997 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1998 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001999}
2000
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002001unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002002{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002003 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2004 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2005 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002006}
2007
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002008unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002009{
2010 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00002011 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
2012 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002013 switch (mFeatureLevel)
2014 {
2015 case D3D_FEATURE_LEVEL_11_0:
2016 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2017 case D3D_FEATURE_LEVEL_10_1:
2018 case D3D_FEATURE_LEVEL_10_0:
2019 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2020 default: UNREACHABLE();
2021 return 0;
2022 }
2023}
2024
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002025unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2026{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002027 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2028 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2029
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002030 switch (mFeatureLevel)
2031 {
2032 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002033 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002034 case D3D_FEATURE_LEVEL_10_1:
2035 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002036 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002037 default: UNREACHABLE();
2038 return 0;
2039 }
2040}
2041
2042unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2043{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002044 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2045 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2046
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002047 switch (mFeatureLevel)
2048 {
2049 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002050 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002051 case D3D_FEATURE_LEVEL_10_1:
2052 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002053 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002054 default: UNREACHABLE();
2055 return 0;
2056 }
2057}
2058
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002059unsigned int Renderer11::getReservedVertexUniformBuffers() const
2060{
2061 // we reserve one buffer for the application uniforms, and one for driver uniforms
2062 return 2;
2063}
2064
2065unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2066{
2067 // we reserve one buffer for the application uniforms, and one for driver uniforms
2068 return 2;
2069}
2070
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002071unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2072{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002073 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2074 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2075
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002076 switch (mFeatureLevel)
2077 {
2078 case D3D_FEATURE_LEVEL_11_0:
2079 return D3D11_SO_BUFFER_SLOT_COUNT;
2080 case D3D_FEATURE_LEVEL_10_1:
2081 case D3D_FEATURE_LEVEL_10_0:
2082 return D3D10_SO_BUFFER_SLOT_COUNT;
2083 default: UNREACHABLE();
2084 return 0;
2085 }
2086}
2087
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002088unsigned int Renderer11::getMaxUniformBufferSize() const
2089{
2090 // Each component is a 4-element vector of 4-byte units (floats)
2091 const unsigned int bytesPerComponent = 4 * sizeof(float);
2092
2093 switch (mFeatureLevel)
2094 {
2095 case D3D_FEATURE_LEVEL_11_0:
2096 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2097 case D3D_FEATURE_LEVEL_10_1:
2098 case D3D_FEATURE_LEVEL_10_0:
2099 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2100 default: UNREACHABLE();
2101 return 0;
2102 }
2103}
2104
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002105bool Renderer11::getNonPower2TextureSupport() const
2106{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002107 switch (mFeatureLevel)
2108 {
2109 case D3D_FEATURE_LEVEL_11_0:
2110 case D3D_FEATURE_LEVEL_10_1:
2111 case D3D_FEATURE_LEVEL_10_0:
2112 return true;
2113 default: UNREACHABLE();
2114 return false;
2115 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002116}
2117
2118bool Renderer11::getOcclusionQuerySupport() const
2119{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002120 switch (mFeatureLevel)
2121 {
2122 case D3D_FEATURE_LEVEL_11_0:
2123 case D3D_FEATURE_LEVEL_10_1:
2124 case D3D_FEATURE_LEVEL_10_0:
2125 return true;
2126 default: UNREACHABLE();
2127 return false;
2128 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002129}
2130
2131bool Renderer11::getInstancingSupport() const
2132{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002133 switch (mFeatureLevel)
2134 {
2135 case D3D_FEATURE_LEVEL_11_0:
2136 case D3D_FEATURE_LEVEL_10_1:
2137 case D3D_FEATURE_LEVEL_10_0:
2138 return true;
2139 default: UNREACHABLE();
2140 return false;
2141 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002142}
2143
2144bool Renderer11::getShareHandleSupport() const
2145{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002146 // We only currently support share handles with BGRA surfaces, because
2147 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002148 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002149 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002150}
2151
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002152bool Renderer11::getDerivativeInstructionSupport() const
2153{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002154 switch (mFeatureLevel)
2155 {
2156 case D3D_FEATURE_LEVEL_11_0:
2157 case D3D_FEATURE_LEVEL_10_1:
2158 case D3D_FEATURE_LEVEL_10_0:
2159 return true;
2160 default: UNREACHABLE();
2161 return false;
2162 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002163}
2164
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002165bool Renderer11::getPostSubBufferSupport() const
2166{
2167 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2168 return false;
2169}
2170
Jamie Madill13a2f852013-12-11 16:35:08 -05002171int Renderer11::getMaxRecommendedElementsIndices() const
2172{
2173 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2174 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2175
2176 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2177 return std::numeric_limits<GLint>::max();
2178}
2179
2180int Renderer11::getMaxRecommendedElementsVertices() const
2181{
2182 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2183 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2184
2185 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2186 return std::numeric_limits<GLint>::max();
2187}
2188
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002189int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002190{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002191 switch (mFeatureLevel)
2192 {
2193 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002194 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002195 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2196 default: UNREACHABLE(); return 0;
2197 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002198}
2199
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002200int Renderer11::getMinorShaderModel() const
2201{
2202 switch (mFeatureLevel)
2203 {
2204 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2205 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2206 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2207 default: UNREACHABLE(); return 0;
2208 }
2209}
2210
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002211float Renderer11::getMaxPointSize() const
2212{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002213 // choose a reasonable maximum. we enforce this in the shader.
2214 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2215 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002216}
2217
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002218int Renderer11::getMaxViewportDimension() const
2219{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002220 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2221 // In our case return the maximum texture size, which is the maximum render buffer size.
2222 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2223 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2224
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002225 switch (mFeatureLevel)
2226 {
2227 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002228 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002229 case D3D_FEATURE_LEVEL_10_1:
2230 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002231 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002232 default: UNREACHABLE();
2233 return 0;
2234 }
2235}
2236
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002237int Renderer11::getMaxTextureWidth() const
2238{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002239 switch (mFeatureLevel)
2240 {
2241 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2242 case D3D_FEATURE_LEVEL_10_1:
2243 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2244 default: UNREACHABLE(); return 0;
2245 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002246}
2247
2248int Renderer11::getMaxTextureHeight() const
2249{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002250 switch (mFeatureLevel)
2251 {
2252 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2253 case D3D_FEATURE_LEVEL_10_1:
2254 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2255 default: UNREACHABLE(); return 0;
2256 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002257}
2258
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002259int Renderer11::getMaxTextureDepth() const
2260{
2261 switch (mFeatureLevel)
2262 {
2263 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2264 case D3D_FEATURE_LEVEL_10_1:
2265 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2266 default: UNREACHABLE(); return 0;
2267 }
2268}
2269
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002270int Renderer11::getMaxTextureArrayLayers() const
2271{
2272 switch (mFeatureLevel)
2273 {
2274 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2275 case D3D_FEATURE_LEVEL_10_1:
2276 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2277 default: UNREACHABLE(); return 0;
2278 }
2279}
2280
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002281bool Renderer11::get32BitIndexSupport() const
2282{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002283 switch (mFeatureLevel)
2284 {
2285 case D3D_FEATURE_LEVEL_11_0:
2286 case D3D_FEATURE_LEVEL_10_1:
2287 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2288 default: UNREACHABLE(); return false;
2289 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002290}
2291
2292int Renderer11::getMinSwapInterval() const
2293{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002294 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002295}
2296
2297int Renderer11::getMaxSwapInterval() const
2298{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002299 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002300}
2301
2302int Renderer11::getMaxSupportedSamples() const
2303{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002304 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002305}
2306
Geoff Lang005df412013-10-16 14:12:50 -04002307GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002308{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002309 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002310 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2311 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2312}
2313
Geoff Lang005df412013-10-16 14:12:50 -04002314GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002315{
2316 unsigned int numCounts = 0;
2317
2318 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002319 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2320 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002321 {
2322 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2323 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2324
2325 if (iter != mMultisampleSupportMap.end())
2326 {
2327 const MultisampleSupportInfo& info = iter->second;
2328 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2329 {
2330 if (info.qualityLevels[i] > 0)
2331 {
2332 numCounts++;
2333 }
2334 }
2335 }
2336 }
2337
2338 return numCounts;
2339}
2340
Geoff Lang005df412013-10-16 14:12:50 -04002341void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002342{
2343 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002344 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2345 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2346 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002347 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002348 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002349
2350 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2351 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2352
2353 if (iter != mMultisampleSupportMap.end())
2354 {
2355 const MultisampleSupportInfo& info = iter->second;
2356 int bufPos = 0;
2357 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2358 {
2359 if (info.qualityLevels[i] > 0)
2360 {
2361 params[bufPos++] = i + 1;
2362 }
2363 }
2364 }
2365}
2366
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002367int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2368{
2369 if (requested == 0)
2370 {
2371 return 0;
2372 }
2373
2374 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2375 if (iter != mMultisampleSupportMap.end())
2376 {
2377 const MultisampleSupportInfo& info = iter->second;
2378 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2379 {
2380 if (info.qualityLevels[i] > 0)
2381 {
2382 return i + 1;
2383 }
2384 }
2385 }
2386
2387 return -1;
2388}
2389
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002390unsigned int Renderer11::getMaxRenderTargets() const
2391{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002392 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2393 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2394
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002395 switch (mFeatureLevel)
2396 {
2397 case D3D_FEATURE_LEVEL_11_0:
2398 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2399 case D3D_FEATURE_LEVEL_10_1:
2400 case D3D_FEATURE_LEVEL_10_0:
2401 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2402 default:
2403 UNREACHABLE();
2404 return 1;
2405 }
2406}
2407
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002408bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002409{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002410 if (source && dest)
2411 {
2412 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2413 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2414
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002415 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002416
2417 dest11->invalidateSwizzleCache();
2418
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002419 return true;
2420 }
2421
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002422 return false;
2423}
2424
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002425bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002426{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002427 if (source && dest)
2428 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002429 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2430 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002431
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002432 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002433
2434 dest11->invalidateSwizzleCache();
2435
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002436 return true;
2437 }
2438
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002439 return false;
2440}
2441
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002442bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2443{
2444 if (source && dest)
2445 {
2446 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2447 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2448
2449 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002450
2451 dest11->invalidateSwizzleCache();
2452
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002453 return true;
2454 }
2455
2456 return false;
2457}
2458
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002459bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2460{
2461 if (source && dest)
2462 {
2463 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2464 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2465
2466 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002467
2468 dest11->invalidateSwizzleCache();
2469
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002470 return true;
2471 }
2472
2473 return false;
2474}
2475
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002476bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002477 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002478{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002479 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002480 if (!colorbuffer)
2481 {
2482 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002483 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002484 }
2485
2486 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2487 if (!sourceRenderTarget)
2488 {
2489 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002490 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002491 }
2492
2493 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2494 if (!source)
2495 {
2496 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002497 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002498 }
2499
2500 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2501 if (!storage11)
2502 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002503 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002504 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002505 }
2506
2507 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2508 if (!destRenderTarget)
2509 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002510 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002511 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002512 }
2513
2514 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2515 if (!dest)
2516 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002517 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002518 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002519 }
2520
Geoff Langb86b9792013-06-04 16:32:05 -04002521 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2522 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002523
Geoff Langb86b9792013-06-04 16:32:05 -04002524 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2525 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002526
Geoff Langb86b9792013-06-04 16:32:05 -04002527 // Use nearest filtering because source and destination are the same size for the direct
2528 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002529 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002530 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002531
Geoff Lang42477a42013-09-17 17:07:02 -04002532 storage11->invalidateSwizzleCacheLevel(level);
2533
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002534 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002535}
2536
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002537bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002538 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002539{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002540 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002541 if (!colorbuffer)
2542 {
2543 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002544 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002545 }
2546
2547 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2548 if (!sourceRenderTarget)
2549 {
2550 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002551 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002552 }
2553
2554 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2555 if (!source)
2556 {
2557 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002558 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002559 }
2560
2561 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2562 if (!storage11)
2563 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002564 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002565 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002566 }
2567
Nicolas Capensb13f8662013-06-04 13:30:19 -04002568 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002569 if (!destRenderTarget)
2570 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002571 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002572 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002573 }
2574
2575 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2576 if (!dest)
2577 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002578 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002579 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002580 }
2581
Geoff Langb86b9792013-06-04 16:32:05 -04002582 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2583 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002584
Geoff Langb86b9792013-06-04 16:32:05 -04002585 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2586 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002587
Geoff Langb86b9792013-06-04 16:32:05 -04002588 // Use nearest filtering because source and destination are the same size for the direct
2589 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002590 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002591 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002592
Geoff Lang42477a42013-09-17 17:07:02 -04002593 storage11->invalidateSwizzleCacheLevel(level);
2594
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002595 return ret;
2596}
2597
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002598bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2599 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2600{
2601 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2602 if (!colorbuffer)
2603 {
2604 ERR("Failed to retrieve the color buffer from the frame buffer.");
2605 return gl::error(GL_OUT_OF_MEMORY, false);
2606 }
2607
2608 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2609 if (!sourceRenderTarget)
2610 {
2611 ERR("Failed to retrieve the render target from the frame buffer.");
2612 return gl::error(GL_OUT_OF_MEMORY, false);
2613 }
2614
2615 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2616 if (!source)
2617 {
2618 ERR("Failed to retrieve the render target view from the render target.");
2619 return gl::error(GL_OUT_OF_MEMORY, false);
2620 }
2621
2622 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2623 if (!storage11)
2624 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002625 ERR("Failed to retrieve the texture storage from the destination.");
2626 return gl::error(GL_OUT_OF_MEMORY, false);
2627 }
2628
2629 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2630 if (!destRenderTarget)
2631 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002632 ERR("Failed to retrieve the render target from the destination storage.");
2633 return gl::error(GL_OUT_OF_MEMORY, false);
2634 }
2635
2636 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2637 if (!dest)
2638 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002639 ERR("Failed to retrieve the render target view from the destination render target.");
2640 return gl::error(GL_OUT_OF_MEMORY, false);
2641 }
2642
Geoff Langb86b9792013-06-04 16:32:05 -04002643 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2644 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002645
Geoff Langb86b9792013-06-04 16:32:05 -04002646 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2647 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002648
Geoff Langb86b9792013-06-04 16:32:05 -04002649 // Use nearest filtering because source and destination are the same size for the direct
2650 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002651 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002652 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002653
Geoff Lang42477a42013-09-17 17:07:02 -04002654 storage11->invalidateSwizzleCacheLevel(level);
2655
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002656 return ret;
2657}
2658
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002659bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2660 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2661{
2662 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2663 if (!colorbuffer)
2664 {
2665 ERR("Failed to retrieve the color buffer from the frame buffer.");
2666 return gl::error(GL_OUT_OF_MEMORY, false);
2667 }
2668
2669 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2670 if (!sourceRenderTarget)
2671 {
2672 ERR("Failed to retrieve the render target from the frame buffer.");
2673 return gl::error(GL_OUT_OF_MEMORY, false);
2674 }
2675
2676 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2677 if (!source)
2678 {
2679 ERR("Failed to retrieve the render target view from the render target.");
2680 return gl::error(GL_OUT_OF_MEMORY, false);
2681 }
2682
2683 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2684 if (!storage11)
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 texture storage from the destination.");
2688 return gl::error(GL_OUT_OF_MEMORY, false);
2689 }
2690
2691 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2692 if (!destRenderTarget)
2693 {
Geoff Langea228632013-07-30 15:17:12 -04002694 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002695 ERR("Failed to retrieve the render target from the destination storage.");
2696 return gl::error(GL_OUT_OF_MEMORY, false);
2697 }
2698
2699 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2700 if (!dest)
2701 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002702 ERR("Failed to retrieve the render target view from the destination render target.");
2703 return gl::error(GL_OUT_OF_MEMORY, false);
2704 }
2705
Geoff Langb86b9792013-06-04 16:32:05 -04002706 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2707 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002708
Geoff Langb86b9792013-06-04 16:32:05 -04002709 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2710 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002711
Geoff Langb86b9792013-06-04 16:32:05 -04002712 // Use nearest filtering because source and destination are the same size for the direct
2713 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002714 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002715 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002716
Geoff Lang42477a42013-09-17 17:07:02 -04002717 storage11->invalidateSwizzleCacheLevel(level);
2718
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002719 return ret;
2720}
2721
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002722void Renderer11::unapplyRenderTargets()
2723{
2724 setOneTimeRenderTarget(NULL);
2725}
2726
2727void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2728{
2729 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2730
2731 rtvArray[0] = renderTargetView;
2732
2733 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2734
2735 // Do not preserve the serial for this one-time-use render target
2736 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2737 {
2738 mAppliedRenderTargetSerials[rtIndex] = 0;
2739 }
2740}
2741
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002742RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2743{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002744 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002745 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002746
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002747 if (depth)
2748 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002749 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002750 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002751 swapChain11->getDepthStencilTexture(),
2752 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002753 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002754 }
2755 else
2756 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002757 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002758 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002759 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002760 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002761 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002762 }
2763 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002764}
2765
Geoff Langa2d97f12013-06-11 11:44:02 -04002766RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002767{
Geoff Langa2d97f12013-06-11 11:44:02 -04002768 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002769 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002770}
2771
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002772ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002773{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002774 ShaderExecutable11 *executable = NULL;
2775
2776 switch (type)
2777 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002778 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002779 {
2780 ID3D11VertexShader *vshader = NULL;
2781 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2782 ASSERT(SUCCEEDED(result));
2783
2784 if (vshader)
2785 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002786 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002787 }
2788 }
2789 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002790 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002791 {
2792 ID3D11PixelShader *pshader = NULL;
2793 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2794 ASSERT(SUCCEEDED(result));
2795
2796 if (pshader)
2797 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002798 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002799 }
2800 }
2801 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002802 case rx::SHADER_GEOMETRY:
2803 {
2804 ID3D11GeometryShader *gshader = NULL;
2805 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2806 ASSERT(SUCCEEDED(result));
2807
2808 if (gshader)
2809 {
2810 executable = new ShaderExecutable11(function, length, gshader);
2811 }
2812 }
2813 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002814 default:
2815 UNREACHABLE();
2816 break;
2817 }
2818
2819 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002820}
2821
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002822ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002823{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002824 const char *profile = NULL;
2825
2826 switch (type)
2827 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002828 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002829 profile = "vs_4_0";
2830 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002831 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002832 profile = "ps_4_0";
2833 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002834 case rx::SHADER_GEOMETRY:
2835 profile = "gs_4_0";
2836 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002837 default:
2838 UNREACHABLE();
2839 return NULL;
2840 }
2841
Geoff Langdad5ed32014-02-10 12:59:17 -05002842 ID3DBlob *binary = (ID3DBlob*)mCompiler.compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002843 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002844 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002845 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002846 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002847
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002848 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002849 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002850
2851 return executable;
2852}
2853
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002854rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2855{
2856 return new UniformStorage11(this, storageSize);
2857}
2858
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002859VertexBuffer *Renderer11::createVertexBuffer()
2860{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002861 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002862}
2863
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002864IndexBuffer *Renderer11::createIndexBuffer()
2865{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002866 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002867}
2868
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002869BufferStorage *Renderer11::createBufferStorage()
2870{
2871 return new BufferStorage11(this);
2872}
2873
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002874QueryImpl *Renderer11::createQuery(GLenum type)
2875{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002876 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002877}
2878
2879FenceImpl *Renderer11::createFence()
2880{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002881 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002882}
2883
Geoff Lang005df412013-10-16 14:12:50 -04002884bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002885{
Jamie Madill4461f092013-10-10 15:10:39 -04002886 int clientVersion = getCurrentClientVersion();
2887
2888 // We only support buffer to texture copies in ES3
2889 if (clientVersion <= 2)
2890 {
2891 return false;
2892 }
2893
2894 // sRGB formats do not work with D3D11 buffer SRVs
2895 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2896 {
2897 return false;
2898 }
2899
2900 // We cannot support direct copies to non-color-renderable formats
2901 if (!gl::IsColorRenderingSupported(internalFormat, this))
2902 {
2903 return false;
2904 }
2905
2906 // We skip all 3-channel formats since sometimes format support is missing
2907 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2908 {
2909 return false;
2910 }
2911
2912 // We don't support formats which we can't represent without conversion
2913 if (getNativeTextureFormat(internalFormat) != internalFormat)
2914 {
2915 return false;
2916 }
2917
2918 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002919}
2920
Jamie Madilla21eea32013-09-18 14:36:25 -04002921bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2922 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2923{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002924 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002925 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2926}
2927
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002928bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002929{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002930 ASSERT(colorbuffer != NULL);
2931
2932 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2933 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002934 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002935 *subresourceIndex = renderTarget->getSubresourceIndex();
2936
2937 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2938 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002939 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002940 ID3D11Resource *textureResource = NULL;
2941 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002942
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002943 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002944 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002945 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002946 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002947
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002948 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002949 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002950 return true;
2951 }
2952 else
2953 {
2954 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2955 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002956 }
2957 }
2958 }
2959 }
2960
2961 return false;
2962}
2963
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002964bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002965 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002966{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002967 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002968 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002969 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002970
2971 if (!readBuffer)
2972 {
2973 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2974 return gl::error(GL_OUT_OF_MEMORY, false);
2975 }
2976
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002977 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002978
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002979 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002980 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002981 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2982 {
2983 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
2984
2985 if (!drawBuffer)
2986 {
2987 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2988 return gl::error(GL_OUT_OF_MEMORY, false);
2989 }
2990
2991 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2992
Geoff Lang125deab2013-08-09 13:34:16 -04002993 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002994 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002995 {
2996 return false;
2997 }
2998 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002999 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003000 }
3001
Geoff Lang685806d2013-06-12 11:16:36 -04003002 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003003 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003004 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
3005 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
3006
3007 if (!readBuffer)
3008 {
3009 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
3010 return gl::error(GL_OUT_OF_MEMORY, false);
3011 }
3012
3013 if (!drawBuffer)
3014 {
3015 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
3016 return gl::error(GL_OUT_OF_MEMORY, false);
3017 }
3018
3019 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3020 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3021
Geoff Lang125deab2013-08-09 13:34:16 -04003022 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04003023 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003024 {
3025 return false;
3026 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003027 }
3028
Geoff Lang42477a42013-09-17 17:07:02 -04003029 invalidateFramebufferSwizzles(drawTarget);
3030
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003031 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003032}
3033
3034void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3035 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3036{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003037 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003038 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003039
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003040 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3041
3042 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003043 {
3044 gl::Rectangle area;
3045 area.x = x;
3046 area.y = y;
3047 area.width = width;
3048 area.height = height;
3049
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003050 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3051 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003052
Geoff Langea228632013-07-30 15:17:12 -04003053 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003054 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003055}
3056
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003057Image *Renderer11::createImage()
3058{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003059 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003060}
3061
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003062void Renderer11::generateMipmap(Image *dest, Image *src)
3063{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003064 Image11 *dest11 = Image11::makeImage11(dest);
3065 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003066 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003067}
3068
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003069TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3070{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003071 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3072 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003073}
3074
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003075TextureStorage *Renderer11::createTextureStorage2D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003076{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003077 return new TextureStorage11_2D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003078}
3079
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003080TextureStorage *Renderer11::createTextureStorageCube(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, int size)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003081{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003082 return new TextureStorage11_Cube(this, baseLevel, maxLevel, internalformat, renderTarget, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003083}
3084
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003085TextureStorage *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 +00003086{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003087 return new TextureStorage11_3D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003088}
3089
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003090TextureStorage *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 +00003091{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003092 return new TextureStorage11_2DArray(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003093}
3094
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003095void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3096 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3097 GLint packAlignment, void *pixels)
3098{
3099 D3D11_TEXTURE2D_DESC textureDesc;
3100 texture->GetDesc(&textureDesc);
3101
3102 D3D11_TEXTURE2D_DESC stagingDesc;
3103 stagingDesc.Width = area.width;
3104 stagingDesc.Height = area.height;
3105 stagingDesc.MipLevels = 1;
3106 stagingDesc.ArraySize = 1;
3107 stagingDesc.Format = textureDesc.Format;
3108 stagingDesc.SampleDesc.Count = 1;
3109 stagingDesc.SampleDesc.Quality = 0;
3110 stagingDesc.Usage = D3D11_USAGE_STAGING;
3111 stagingDesc.BindFlags = 0;
3112 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3113 stagingDesc.MiscFlags = 0;
3114
3115 ID3D11Texture2D* stagingTex = NULL;
3116 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3117 if (FAILED(result))
3118 {
3119 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3120 return;
3121 }
3122
3123 ID3D11Texture2D* srcTex = NULL;
3124 if (textureDesc.SampleDesc.Count > 1)
3125 {
3126 D3D11_TEXTURE2D_DESC resolveDesc;
3127 resolveDesc.Width = textureDesc.Width;
3128 resolveDesc.Height = textureDesc.Height;
3129 resolveDesc.MipLevels = 1;
3130 resolveDesc.ArraySize = 1;
3131 resolveDesc.Format = textureDesc.Format;
3132 resolveDesc.SampleDesc.Count = 1;
3133 resolveDesc.SampleDesc.Quality = 0;
3134 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3135 resolveDesc.BindFlags = 0;
3136 resolveDesc.CPUAccessFlags = 0;
3137 resolveDesc.MiscFlags = 0;
3138
3139 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3140 if (FAILED(result))
3141 {
3142 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003143 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003144 return;
3145 }
3146
3147 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3148 subResource = 0;
3149 }
3150 else
3151 {
3152 srcTex = texture;
3153 srcTex->AddRef();
3154 }
3155
3156 D3D11_BOX srcBox;
3157 srcBox.left = area.x;
3158 srcBox.right = area.x + area.width;
3159 srcBox.top = area.y;
3160 srcBox.bottom = area.y + area.height;
3161 srcBox.front = 0;
3162 srcBox.back = 1;
3163
3164 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3165
Geoff Langea228632013-07-30 15:17:12 -04003166 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003167
3168 D3D11_MAPPED_SUBRESOURCE mapping;
3169 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3170
3171 unsigned char *source;
3172 int inputPitch;
3173 if (packReverseRowOrder)
3174 {
3175 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3176 inputPitch = -static_cast<int>(mapping.RowPitch);
3177 }
3178 else
3179 {
3180 source = static_cast<unsigned char*>(mapping.pData);
3181 inputPitch = static_cast<int>(mapping.RowPitch);
3182 }
3183
Geoff Lang697ad3e2013-06-04 10:11:28 -04003184 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003185
Geoff Lang005df412013-10-16 14:12:50 -04003186 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003187 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3188 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3189
3190 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3191
3192 if (sourceFormat == format && sourceType == type)
3193 {
3194 // Direct copy possible
3195 unsigned char *dest = static_cast<unsigned char*>(pixels);
3196 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003197 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003198 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003199 }
3200 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003201 else
3202 {
Geoff Lang005df412013-10-16 14:12:50 -04003203 GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003204 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3205
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003206 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003207 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003208 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003209 // Fast copy is possible through some special function
3210 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003211 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003212 for (int x = 0; x < area.width; x++)
3213 {
3214 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3215 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3216
3217 fastCopyFunc(src, dest);
3218 }
3219 }
3220 }
3221 else
3222 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003223 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003224 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3225
3226 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3227 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3228 sizeof(temp) >= sizeof(gl::ColorUI) &&
3229 sizeof(temp) >= sizeof(gl::ColorI));
3230
3231 for (int y = 0; y < area.height; y++)
3232 {
3233 for (int x = 0; x < area.width; x++)
3234 {
3235 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3236 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3237
3238 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3239 // will not allow the copy otherwise.
3240 readFunc(src, temp);
3241 writeFunc(temp, dest);
3242 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003243 }
3244 }
3245 }
3246
3247 mDeviceContext->Unmap(stagingTex, 0);
3248
Geoff Langea228632013-07-30 15:17:12 -04003249 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003250}
3251
Geoff Lang758d5b22013-06-11 11:42:50 -04003252bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003253 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3254 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003255{
Geoff Lang975af372013-06-12 11:19:22 -04003256 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3257 // it should never be the case that both color and depth/stencil need to be blitted at
3258 // at the same time.
3259 ASSERT(colorBlit != (depthBlit || stencilBlit));
3260
Geoff Langc1f51be2013-06-11 11:49:14 -04003261 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003262
Geoff Lang4d782732013-07-22 10:44:18 -04003263 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3264 if (!drawRenderTarget)
3265 {
3266 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3267 return gl::error(GL_OUT_OF_MEMORY, false);
3268 }
3269
3270 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3271 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3272 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3273 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3274
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003275 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3276 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003277 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003278 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003279 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003280 }
3281
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003282 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003283 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003284 unsigned int readSubresource = 0;
3285 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003286 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003287 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3288 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003289
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003290 if (unresolvedTexture)
3291 {
3292 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3293 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003294
Geoff Langea228632013-07-30 15:17:12 -04003295 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003296
3297 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3298 if (FAILED(result))
3299 {
Geoff Langea228632013-07-30 15:17:12 -04003300 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003301 return gl::error(GL_OUT_OF_MEMORY, false);
3302 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003303 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003304 }
3305 else
3306 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003307 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003308 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003309 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003310 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003311 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003312 }
3313
Geoff Lang4d782732013-07-22 10:44:18 -04003314 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003315 {
Geoff Lang4d782732013-07-22 10:44:18 -04003316 SafeRelease(readTexture);
3317 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003318 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003319 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003320 }
3321
Geoff Lang125deab2013-08-09 13:34:16 -04003322 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3323 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3324
3325 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3326
3327 bool wholeBufferCopy = !scissorNeeded &&
3328 readRect.x == 0 && readRect.width == readSize.width &&
3329 readRect.y == 0 && readRect.height == readSize.height &&
3330 drawRect.x == 0 && drawRect.width == drawSize.width &&
3331 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003332
Geoff Langc1f51be2013-06-11 11:49:14 -04003333 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003334
Geoff Lang1cd1b212014-02-11 09:42:27 -05003335 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003336
3337 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3338 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3339 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3340 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3341
3342 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3343 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3344 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3345
Geoff Langc1f51be2013-06-11 11:49:14 -04003346 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003347 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3348 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003349 {
Geoff Lang125deab2013-08-09 13:34:16 -04003350 UINT dstX = drawRect.x;
3351 UINT dstY = drawRect.y;
3352
Geoff Langc1f51be2013-06-11 11:49:14 -04003353 D3D11_BOX readBox;
3354 readBox.left = readRect.x;
3355 readBox.right = readRect.x + readRect.width;
3356 readBox.top = readRect.y;
3357 readBox.bottom = readRect.y + readRect.height;
3358 readBox.front = 0;
3359 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003360
Geoff Lang125deab2013-08-09 13:34:16 -04003361 if (scissorNeeded)
3362 {
3363 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3364 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3365
3366 if (drawRect.x < scissor->x)
3367 {
3368 dstX = scissor->x;
3369 readBox.left += (scissor->x - drawRect.x);
3370 }
3371 if (drawRect.y < scissor->y)
3372 {
3373 dstY = scissor->y;
3374 readBox.top += (scissor->y - drawRect.y);
3375 }
3376 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3377 {
3378 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3379 }
3380 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3381 {
3382 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3383 }
3384 }
3385
Geoff Langc1f51be2013-06-11 11:49:14 -04003386 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3387 // We also require complete framebuffer copies for depth-stencil blit.
3388 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003389
Geoff Lang125deab2013-08-09 13:34:16 -04003390 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003391 readTexture, readSubresource, pSrcBox);
3392 result = true;
3393 }
3394 else
3395 {
3396 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003397 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003398
Geoff Lang975af372013-06-12 11:19:22 -04003399 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003400 {
Geoff Lang975af372013-06-12 11:19:22 -04003401 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003402 drawTexture, drawSubresource, drawArea, drawSize,
3403 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003404 }
3405 else if (depthBlit)
3406 {
Geoff Lang125deab2013-08-09 13:34:16 -04003407 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3408 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003409 }
3410 else if (stencilBlit)
3411 {
3412 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003413 drawTexture, drawSubresource, drawArea, drawSize,
3414 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003415 }
3416 else
3417 {
Geoff Lang685806d2013-06-12 11:16:36 -04003418 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003419 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3420 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003421 }
3422 }
3423
3424 SafeRelease(readTexture);
3425 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003426
3427 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003428}
3429
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003430ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3431{
3432 D3D11_TEXTURE2D_DESC textureDesc;
3433 source->GetDesc(&textureDesc);
3434
3435 if (textureDesc.SampleDesc.Count > 1)
3436 {
3437 D3D11_TEXTURE2D_DESC resolveDesc;
3438 resolveDesc.Width = textureDesc.Width;
3439 resolveDesc.Height = textureDesc.Height;
3440 resolveDesc.MipLevels = 1;
3441 resolveDesc.ArraySize = 1;
3442 resolveDesc.Format = textureDesc.Format;
3443 resolveDesc.SampleDesc.Count = 1;
3444 resolveDesc.SampleDesc.Quality = 0;
3445 resolveDesc.Usage = textureDesc.Usage;
3446 resolveDesc.BindFlags = textureDesc.BindFlags;
3447 resolveDesc.CPUAccessFlags = 0;
3448 resolveDesc.MiscFlags = 0;
3449
3450 ID3D11Texture2D *resolveTexture = NULL;
3451 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3452 if (FAILED(result))
3453 {
3454 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3455 return NULL;
3456 }
3457
3458 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3459 return resolveTexture;
3460 }
3461 else
3462 {
3463 source->AddRef();
3464 return source;
3465 }
3466}
3467
Geoff Lang42477a42013-09-17 17:07:02 -04003468void Renderer11::invalidateRenderbufferSwizzles(gl::Renderbuffer *renderBuffer, int mipLevel)
3469{
3470 TextureStorage *texStorage = renderBuffer->getTextureStorage();
3471 if (texStorage)
3472 {
3473 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3474 if (!texStorage11)
3475 {
3476 ERR("texture storage pointer unexpectedly null.");
3477 return;
3478 }
3479
3480 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3481 }
3482}
3483
3484void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3485{
3486 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3487 {
3488 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
3489 if (colorbuffer)
3490 {
3491 invalidateRenderbufferSwizzles(colorbuffer, framebuffer->getColorbufferMipLevel(colorAttachment));
3492 }
3493 }
3494
3495 gl::Renderbuffer *depthBuffer = framebuffer->getDepthbuffer();
3496 if (depthBuffer)
3497 {
3498 invalidateRenderbufferSwizzles(depthBuffer, framebuffer->getDepthbufferMipLevel());
3499 }
3500
3501 gl::Renderbuffer *stencilBuffer = framebuffer->getStencilbuffer();
3502 if (stencilBuffer)
3503 {
3504 invalidateRenderbufferSwizzles(stencilBuffer, framebuffer->getStencilbufferMipLevel());
3505 }
3506}
3507
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003508bool Renderer11::getLUID(LUID *adapterLuid) const
3509{
3510 adapterLuid->HighPart = 0;
3511 adapterLuid->LowPart = 0;
3512
3513 if (!mDxgiAdapter)
3514 {
3515 return false;
3516 }
3517
3518 DXGI_ADAPTER_DESC adapterDesc;
3519 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3520 {
3521 return false;
3522 }
3523
3524 *adapterLuid = adapterDesc.AdapterLuid;
3525 return true;
3526}
3527
Geoff Lang005df412013-10-16 14:12:50 -04003528GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003529{
3530 int clientVersion = getCurrentClientVersion();
3531 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3532}
3533
Jamie Madill95ffb862014-01-29 09:26:59 -05003534rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3535{
3536 return gl_d3d11::GetVertexConversionType(vertexFormat);
3537}
3538
3539GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3540{
3541 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3542}
3543
Geoff Lang61e49a52013-05-29 10:22:58 -04003544Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3545{
3546 MultisampleSupportInfo supportInfo = { 0 };
3547
3548 UINT formatSupport;
3549 HRESULT result;
3550
3551 result = mDevice->CheckFormatSupport(format, &formatSupport);
3552 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3553 {
3554 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3555 {
3556 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3557 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3558 {
3559 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3560 }
3561 else
3562 {
3563 supportInfo.qualityLevels[i - 1] = 0;
3564 }
3565 }
3566 }
3567
3568 return supportInfo;
3569}
3570
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003571}