blob: a4037f6e3e0f1b3c3a33694bee43b901764eecbc [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002//
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00003// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// Renderer11.cpp: Implements a back-end specific class for the D3D11 renderer.
9
daniel@transgaming.com5503fd02012-11-28 19:38:57 +000010#include "libGLESv2/main.h"
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +000011#include "common/utilities.h"
daniel@transgaming.com18adad02012-11-28 21:04:03 +000012#include "libGLESv2/Buffer.h"
daniel@transgaming.com53670042012-11-28 20:55:51 +000013#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com80fc3322012-11-28 21:02:13 +000014#include "libGLESv2/Framebuffer.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000015#include "libGLESv2/RenderBuffer.h"
Geoff Langd47e0fc2013-08-29 11:40:43 -040016#include "libGLESv2/renderer/d3d11/Renderer11.h"
17#include "libGLESv2/renderer/d3d11/RenderTarget11.h"
18#include "libGLESv2/renderer/d3d11/renderer11_utils.h"
19#include "libGLESv2/renderer/d3d11/formatutils11.h"
20#include "libGLESv2/renderer/d3d11/ShaderExecutable11.h"
21#include "libGLESv2/renderer/d3d11/SwapChain11.h"
22#include "libGLESv2/renderer/d3d11/Image11.h"
23#include "libGLESv2/renderer/d3d11/VertexBuffer11.h"
24#include "libGLESv2/renderer/d3d11/IndexBuffer11.h"
25#include "libGLESv2/renderer/d3d11/BufferStorage11.h"
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000026#include "libGLESv2/renderer/VertexDataManager.h"
27#include "libGLESv2/renderer/IndexDataManager.h"
Geoff Langd47e0fc2013-08-29 11:40:43 -040028#include "libGLESv2/renderer/d3d11/TextureStorage11.h"
29#include "libGLESv2/renderer/d3d11/Query11.h"
30#include "libGLESv2/renderer/d3d11/Fence11.h"
31#include "libGLESv2/renderer/d3d11/Blit11.h"
32#include "libGLESv2/renderer/d3d11/Clear11.h"
33#include "libGLESv2/renderer/d3d11/PixelTransfer11.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000034#include "libEGL/Display.h"
35
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000036#ifdef _DEBUG
37// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
38// and conformance tests. to enable all warnings, remove this define.
39#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
40#endif
41
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000042namespace rx
43{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000044static const DXGI_FORMAT RenderTargetFormats[] =
45 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000046 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000047 DXGI_FORMAT_R8G8B8A8_UNORM
48 };
49
50static const DXGI_FORMAT DepthStencilFormats[] =
51 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000052 DXGI_FORMAT_UNKNOWN,
53 DXGI_FORMAT_D24_UNORM_S8_UINT,
54 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000055 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000056
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000057enum
58{
59 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
60};
61
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000062Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
63{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000064 mVertexDataManager = NULL;
65 mIndexDataManager = NULL;
66
daniel@transgaming.comc5114302012-12-20 21:11:36 +000067 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000068 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000069
Geoff Langb86b9792013-06-04 16:32:05 -040070 mBlit = NULL;
Jamie Madilla21eea32013-09-18 14:36:25 -040071 mPixelTransfer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000072
Geoff Langda507fe2013-08-20 12:01:42 -040073 mClear = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000074
daniel@transgaming.combdf787f2013-02-01 03:20:36 +000075 mSyncQuery = NULL;
76
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000077 mD3d11Module = NULL;
78 mDxgiModule = NULL;
79
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000080 mDeviceLost = false;
81
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +000082 mMaxSupportedSamples = 0;
83
daniel@transgaming.com25072f62012-11-28 19:31:32 +000084 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000085 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000086 mDxgiAdapter = NULL;
87 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +000088
89 mDriverConstantBufferVS = NULL;
90 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +000091
92 mBGRATextureSupport = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +000093
Jamie Madill6246dc82014-01-29 09:26:47 -050094 mAppliedVertexShader = NULL;
95 mAppliedGeometryShader = NULL;
96 mAppliedPixelShader = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000097}
98
99Renderer11::~Renderer11()
100{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000101 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000102}
103
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000104Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
105{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000106 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000107 return static_cast<rx::Renderer11*>(renderer);
108}
109
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000110#ifndef __d3d11_1_h__
111#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
112#endif
113
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000114EGLint Renderer11::initialize()
115{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000116 if (!initializeCompiler())
117 {
118 return EGL_NOT_INITIALIZED;
119 }
120
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000121 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
122 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000123
124 if (mD3d11Module == NULL || mDxgiModule == NULL)
125 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000126 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000127 return EGL_NOT_INITIALIZED;
128 }
129
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000130 // create the D3D11 device
131 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000132 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000133
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000134 if (D3D11CreateDevice == NULL)
135 {
136 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
137 return EGL_NOT_INITIALIZED;
138 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000139
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000140 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000141 {
142 D3D_FEATURE_LEVEL_11_0,
143 D3D_FEATURE_LEVEL_10_1,
144 D3D_FEATURE_LEVEL_10_0,
145 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000146
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000147 HRESULT result = S_OK;
148
149#ifdef _DEBUG
150 result = D3D11CreateDevice(NULL,
151 D3D_DRIVER_TYPE_HARDWARE,
152 NULL,
153 D3D11_CREATE_DEVICE_DEBUG,
154 featureLevels,
155 ArraySize(featureLevels),
156 D3D11_SDK_VERSION,
157 &mDevice,
158 &mFeatureLevel,
159 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000160
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000161 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000162 {
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000163 ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n");
164 }
165
166 if (!mDevice || FAILED(result))
167#endif
168 {
169 result = D3D11CreateDevice(NULL,
170 D3D_DRIVER_TYPE_HARDWARE,
171 NULL,
172 0,
173 featureLevels,
174 ArraySize(featureLevels),
175 D3D11_SDK_VERSION,
176 &mDevice,
177 &mFeatureLevel,
178 &mDeviceContext);
179
180 if (!mDevice || FAILED(result))
181 {
182 ERR("Could not create D3D11 device - aborting!\n");
183 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
184 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000185 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000186
187 IDXGIDevice *dxgiDevice = NULL;
188 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
189
190 if (FAILED(result))
191 {
192 ERR("Could not query DXGI device - aborting!\n");
193 return EGL_NOT_INITIALIZED;
194 }
195
196 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
197
198 if (FAILED(result))
199 {
200 ERR("Could not retrieve DXGI adapter - aborting!\n");
201 return EGL_NOT_INITIALIZED;
202 }
203
Geoff Langea228632013-07-30 15:17:12 -0400204 SafeRelease(dxgiDevice);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000205
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000206 mDxgiAdapter->GetDesc(&mAdapterDescription);
207 memset(mDescription, 0, sizeof(mDescription));
208 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
209
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000210 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
211
212 if (!mDxgiFactory || FAILED(result))
213 {
214 ERR("Could not create DXGI factory - aborting!\n");
215 return EGL_NOT_INITIALIZED;
216 }
217
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000218 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
219#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
220 ID3D11InfoQueue *infoQueue;
221 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
222
223 if (SUCCEEDED(result))
224 {
225 D3D11_MESSAGE_ID hideMessages[] =
226 {
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000227 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000228 };
229
230 D3D11_INFO_QUEUE_FILTER filter = {0};
231 filter.DenyList.NumIDs = ArraySize(hideMessages);
232 filter.DenyList.pIDList = hideMessages;
233
234 infoQueue->AddStorageFilterEntries(&filter);
Geoff Langea228632013-07-30 15:17:12 -0400235 SafeRelease(infoQueue);
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000236 }
237#endif
238
Geoff Lang61e49a52013-05-29 10:22:58 -0400239 mMaxSupportedSamples = 0;
240
241 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
242 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000243 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400244 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
245 mMultisampleSupportMap.insert(std::make_pair(*i, support));
246 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000247 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000248
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000249 initializeDevice();
250
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000251 // BGRA texture support is optional in feature levels 10 and 10_1
252 UINT formatSupport;
253 result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport);
254 if (FAILED(result))
255 {
256 ERR("Error checking BGRA format support: 0x%08X", result);
257 }
258 else
259 {
260 const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET);
261 mBGRATextureSupport = (formatSupport & flags) == flags;
262 }
263
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000264 // Check floating point texture support
265 static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE;
266 static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET;
267 static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE;
268
269 DXGI_FORMAT float16Formats[] =
270 {
271 DXGI_FORMAT_R16_FLOAT,
272 DXGI_FORMAT_R16G16_FLOAT,
273 DXGI_FORMAT_R16G16B16A16_FLOAT,
274 };
275
276 DXGI_FORMAT float32Formats[] =
277 {
278 DXGI_FORMAT_R32_FLOAT,
279 DXGI_FORMAT_R32G32_FLOAT,
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000280 DXGI_FORMAT_R32G32B32A32_FLOAT,
281 };
282
283 mFloat16TextureSupport = true;
284 mFloat16FilterSupport = true;
285 mFloat16RenderSupport = true;
286 for (unsigned int i = 0; i < ArraySize(float16Formats); i++)
287 {
288 if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport)))
289 {
290 mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
291 mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
292 mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
293 }
294 else
295 {
296 mFloat16TextureSupport = false;
297 mFloat16RenderSupport = false;
298 mFloat16FilterSupport = false;
299 }
300 }
301
302 mFloat32TextureSupport = true;
303 mFloat32FilterSupport = true;
304 mFloat32RenderSupport = true;
305 for (unsigned int i = 0; i < ArraySize(float32Formats); i++)
306 {
307 if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport)))
308 {
309 mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
310 mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
311 mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
312 }
313 else
314 {
315 mFloat32TextureSupport = false;
316 mFloat32FilterSupport = false;
317 mFloat32RenderSupport = false;
318 }
319 }
320
Geoff Lang632192d2013-10-04 13:40:46 -0400321 DXGI_FORMAT rgTextureFormats[] =
322 {
323 DXGI_FORMAT_R8_UNORM,
324 DXGI_FORMAT_R8G8_UNORM,
325 DXGI_FORMAT_R16_FLOAT,
326 DXGI_FORMAT_R16G16_FLOAT,
327 DXGI_FORMAT_R32_FLOAT,
328 DXGI_FORMAT_R32G32_FLOAT,
329 };
330
331 mRGTextureSupport = true;
332 for (unsigned int i = 0; i < ArraySize(rgTextureFormats); i++)
333 {
334 if (SUCCEEDED(mDevice->CheckFormatSupport(rgTextureFormats[i], &formatSupport)))
335 {
336 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
337 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
338 mRGTextureSupport = mRGTextureSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
339 }
340 else
341 {
342 mRGTextureSupport = false;
343 }
344 }
345
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +0000346 // Check compressed texture support
347 const unsigned int requiredCompressedTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D;
348
349 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &formatSupport)))
350 {
351 mDXT1TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
352 }
353 else
354 {
355 mDXT1TextureSupport = false;
356 }
357
358 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport)))
359 {
360 mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
361 }
362 else
363 {
364 mDXT3TextureSupport = false;
365 }
366
367 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC5_UNORM, &formatSupport)))
368 {
369 mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
370 }
371 else
372 {
373 mDXT5TextureSupport = false;
374 }
375
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +0000376 // Check depth texture support
377 DXGI_FORMAT depthTextureFormats[] =
378 {
379 DXGI_FORMAT_D16_UNORM,
380 DXGI_FORMAT_D24_UNORM_S8_UINT,
381 };
382
383 static const unsigned int requiredDepthTextureFlags = D3D11_FORMAT_SUPPORT_DEPTH_STENCIL |
384 D3D11_FORMAT_SUPPORT_TEXTURE2D;
385
386 mDepthTextureSupport = true;
387 for (unsigned int i = 0; i < ArraySize(depthTextureFormats); i++)
388 {
389 if (SUCCEEDED(mDevice->CheckFormatSupport(depthTextureFormats[i], &formatSupport)))
390 {
391 mDepthTextureSupport = mDepthTextureSupport && ((formatSupport & requiredDepthTextureFlags) == requiredDepthTextureFlags);
392 }
393 else
394 {
395 mDepthTextureSupport = false;
396 }
397 }
398
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000399 return EGL_SUCCESS;
400}
401
402// do any one-time device initialization
403// NOTE: this is also needed after a device lost/reset
404// to reset the scene status and ensure the default states are reset.
405void Renderer11::initializeDevice()
406{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000407 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000408 mInputLayoutCache.initialize(mDevice, mDeviceContext);
409
410 ASSERT(!mVertexDataManager && !mIndexDataManager);
411 mVertexDataManager = new VertexDataManager(this);
412 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000413
Geoff Langb86b9792013-06-04 16:32:05 -0400414 ASSERT(!mBlit);
415 mBlit = new Blit11(this);
416
Geoff Langda507fe2013-08-20 12:01:42 -0400417 ASSERT(!mClear);
418 mClear = new Clear11(this);
419
Jamie Madilla21eea32013-09-18 14:36:25 -0400420 ASSERT(!mPixelTransfer);
421 mPixelTransfer = new PixelTransfer11(this);
422
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000423 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000424}
425
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000426int Renderer11::generateConfigs(ConfigDesc **configDescList)
427{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000428 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
429 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000430 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
431 int numConfigs = 0;
432
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000433 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000434 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000435 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000436 {
437 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
438
439 UINT formatSupport = 0;
440 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000441
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000442 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
443 {
444 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
445
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000446 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000447
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000448 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
449 {
450 UINT formatSupport = 0;
451 result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
452 depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
453 }
454
455 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000456 {
Jamie Madilld6cb2442013-07-10 15:13:38 -0400457 // FIXME: parse types from context version
458 ASSERT(d3d11_gl::GetInternalFormat(renderTargetFormat, 2) == d3d11_gl::GetInternalFormat(renderTargetFormat, 3));
459 ASSERT(d3d11_gl::GetInternalFormat(depthStencilFormat, 2) == d3d11_gl::GetInternalFormat(depthStencilFormat, 3));
460
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000461 ConfigDesc newConfig;
Jamie Madilld6cb2442013-07-10 15:13:38 -0400462 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat, getCurrentClientVersion());
463 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat, getCurrentClientVersion());
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000464 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
465 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000466 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000467
468 (*configDescList)[numConfigs++] = newConfig;
469 }
470 }
471 }
472 }
473
474 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000475}
476
477void Renderer11::deleteConfigs(ConfigDesc *configDescList)
478{
479 delete [] (configDescList);
480}
481
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000482void Renderer11::sync(bool block)
483{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000484 if (block)
485 {
486 HRESULT result;
487
488 if (!mSyncQuery)
489 {
490 D3D11_QUERY_DESC queryDesc;
491 queryDesc.Query = D3D11_QUERY_EVENT;
492 queryDesc.MiscFlags = 0;
493
494 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
495 ASSERT(SUCCEEDED(result));
496 }
497
498 mDeviceContext->End(mSyncQuery);
499 mDeviceContext->Flush();
500
501 do
502 {
503 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
504
505 // Keep polling, but allow other threads to do something useful first
506 Sleep(0);
507
508 if (testDeviceLost(true))
509 {
510 return;
511 }
512 }
513 while (result == S_FALSE);
514 }
515 else
516 {
517 mDeviceContext->Flush();
518 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000519}
520
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000521SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
522{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000523 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000524}
525
Geoff Lange2e0ce02013-09-17 17:05:08 -0400526void Renderer11::generateSwizzle(gl::Texture *texture)
527{
Geoff Lang42477a42013-09-17 17:07:02 -0400528 if (texture)
529 {
530 TextureStorageInterface *texStorage = texture->getNativeTexture();
531 if (texStorage)
532 {
533 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
534
535 storage11->generateSwizzles(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
536 texture->getSwizzleAlpha());
537 }
538 }
Geoff Lange2e0ce02013-09-17 17:05:08 -0400539}
540
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000541void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
542{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000543 if (type == gl::SAMPLER_PIXEL)
544 {
545 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
546 {
547 ERR("Pixel shader sampler index %i is not valid.", index);
548 return;
549 }
550
551 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
552 {
553 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
554
555 if (!dxSamplerState)
556 {
557 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
558 "sampler state for pixel shaders at slot %i.", index);
559 }
560
561 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
562
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000563 mCurPixelSamplerStates[index] = samplerState;
564 }
565
566 mForceSetPixelSamplerStates[index] = false;
567 }
568 else if (type == gl::SAMPLER_VERTEX)
569 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000570 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000571 {
572 ERR("Vertex shader sampler index %i is not valid.", index);
573 return;
574 }
575
576 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
577 {
578 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
579
580 if (!dxSamplerState)
581 {
582 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
583 "sampler state for vertex shaders at slot %i.", index);
584 }
585
586 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
587
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000588 mCurVertexSamplerStates[index] = samplerState;
589 }
590
591 mForceSetVertexSamplerStates[index] = false;
592 }
593 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000594}
595
596void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
597{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000598 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000599 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000600
601 if (texture)
602 {
603 TextureStorageInterface *texStorage = texture->getNativeTexture();
604 if (texStorage)
605 {
606 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
Geoff Lang644bbf22013-09-17 17:02:43 -0400607 textureSRV = storage11->getSRV(texture->getSwizzleRed(), texture->getSwizzleGreen(), texture->getSwizzleBlue(),
608 texture->getSwizzleAlpha());
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000609 }
610
611 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
612 // missing the shader resource view
613 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000614
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000615 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000616 }
617
618 if (type == gl::SAMPLER_PIXEL)
619 {
620 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
621 {
622 ERR("Pixel shader sampler index %i is not valid.", index);
623 return;
624 }
625
Geoff Lang91382e52014-01-07 16:16:30 -0500626 if (forceSetTexture || mCurPixelSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000627 {
628 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
629 }
630
Geoff Lang91382e52014-01-07 16:16:30 -0500631 mCurPixelSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000632 }
633 else if (type == gl::SAMPLER_VERTEX)
634 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000635 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000636 {
637 ERR("Vertex shader sampler index %i is not valid.", index);
638 return;
639 }
640
Geoff Lang91382e52014-01-07 16:16:30 -0500641 if (forceSetTexture || mCurVertexSRVs[index] != textureSRV)
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000642 {
643 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
644 }
645
Geoff Lang91382e52014-01-07 16:16:30 -0500646 mCurVertexSRVs[index] = textureSRV;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000647 }
648 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000649}
650
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000651bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
652{
653 // convert buffers to ID3D11Buffer*
654 ID3D11Buffer *vertexConstantBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = { NULL };
655 ID3D11Buffer *pixelConstantBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = { NULL };
656
657 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
658 {
659 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
660 if (uniformBuffer)
661 {
662 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500663 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000664
665 if (!constantBuffer)
666 {
667 return false;
668 }
669
Geoff Langc6354ee2013-07-22 10:40:07 -0400670 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
671 {
672 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
673 1, &constantBuffer);
674 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
675 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000676 }
677 }
678
679 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
680 {
681 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
682 if (uniformBuffer)
683 {
684 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
Geoff Lang9c53f1e2014-01-08 13:41:47 -0500685 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(BUFFER_USAGE_UNIFORM);
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000686
687 if (!constantBuffer)
688 {
689 return false;
690 }
691
Geoff Langc6354ee2013-07-22 10:40:07 -0400692 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
693 {
694 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
695 1, &constantBuffer);
696 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
697 }
698
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000699 pixelConstantBuffers[uniformBufferIndex] = constantBuffer;
700 }
701 }
702
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000703 return true;
704}
705
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000706void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000707{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000708 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000709 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000710 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
711 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000712 if (!dxRasterState)
713 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000714 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000715 "rasterizer state.");
716 }
717
718 mDeviceContext->RSSetState(dxRasterState);
719
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000720 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000721 }
722
723 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000724}
725
Geoff Langc142e9d2013-09-30 15:19:47 -0400726void Renderer11::setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000727 unsigned int sampleMask)
728{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000729 if (mForceSetBlendState ||
730 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400731 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000732 sampleMask != mCurSampleMask)
733 {
Geoff Langc142e9d2013-09-30 15:19:47 -0400734 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(framebuffer, blendState);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000735 if (!dxBlendState)
736 {
737 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
738 "blend state.");
739 }
740
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000741 float blendColors[4] = {0.0f};
742 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
743 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
744 {
745 blendColors[0] = blendColor.red;
746 blendColors[1] = blendColor.green;
747 blendColors[2] = blendColor.blue;
748 blendColors[3] = blendColor.alpha;
749 }
750 else
751 {
752 blendColors[0] = blendColor.alpha;
753 blendColors[1] = blendColor.alpha;
754 blendColors[2] = blendColor.alpha;
755 blendColors[3] = blendColor.alpha;
756 }
757
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000758 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
759
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000760 mCurBlendState = blendState;
761 mCurBlendColor = blendColor;
762 mCurSampleMask = sampleMask;
763 }
764
765 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000766}
767
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000768void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000769 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000770{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000771 if (mForceSetDepthStencilState ||
772 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
773 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
774 {
775 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
776 stencilRef != stencilBackRef ||
777 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
778 {
779 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
780 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000781 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000782 }
783
784 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
785 if (!dxDepthStencilState)
786 {
787 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
788 "setting the default depth stencil state.");
789 }
790
Jamie Madillec91cd32014-01-21 16:38:12 -0500791 // Max D3D11 stencil reference value is 0xFF, corresponding to the max 8 bits in a stencil buffer
792 // GL specifies we should clamp the ref value to the nearest bit depth when doing stencil ops
793 META_ASSERT(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF);
794 META_ASSERT(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF);
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
Geoff Lang0550d032014-01-30 11:29:07 -05001406void Renderer11::applyShaders(gl::ProgramBinary *programBinary, bool rasterizerDiscard)
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001407{
Jamie Madill6246dc82014-01-29 09:26:47 -05001408 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1409 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
1410 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001411
Jamie Madill6246dc82014-01-29 09:26:47 -05001412 ID3D11VertexShader *vertexShader = (vertexExe ? ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader() : NULL);
1413 ID3D11PixelShader *pixelShader = (pixelExe ? ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader() : NULL);
1414 ID3D11GeometryShader *geometryShader = (geometryExe ? ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader() : NULL);
1415
1416 // Skip GS if we aren't drawing points
1417 if (!mCurRasterState.pointDrawMode)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001418 {
Jamie Madill6246dc82014-01-29 09:26:47 -05001419 geometryShader = NULL;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001420 }
1421
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 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001803}
1804
1805bool Renderer11::resetDevice()
1806{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001807 // recreate everything
1808 release();
1809 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001810
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001811 if (result != EGL_SUCCESS)
1812 {
1813 ERR("Could not reinitialize D3D11 device: %08X", result);
1814 return false;
1815 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001816
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001817 mDeviceLost = false;
1818
1819 return true;
1820}
1821
1822DWORD Renderer11::getAdapterVendor() const
1823{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001824 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001825}
1826
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001827std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001828{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001829 std::ostringstream rendererString;
1830
1831 rendererString << mDescription;
1832 rendererString << " Direct3D11";
1833
1834 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1835 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1836
1837 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001838}
1839
1840GUID Renderer11::getAdapterIdentifier() const
1841{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001842 // Use the adapter LUID as our adapter ID
1843 // This number is local to a machine is only guaranteed to be unique between restarts
1844 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1845 GUID adapterId = {0};
1846 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1847 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001848}
1849
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001850bool Renderer11::getBGRATextureSupport() const
1851{
1852 return mBGRATextureSupport;
1853}
1854
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001855bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001856{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001857 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001858}
1859
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001860bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001861{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001862 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001863}
1864
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001865bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001866{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001867 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001868}
1869
1870bool Renderer11::getDepthTextureSupport() const
1871{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001872 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001873}
1874
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001875bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001876{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001877 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001878}
1879
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001880bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001881{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001882 return mFloat32FilterSupport;
1883}
1884
1885bool Renderer11::getFloat32TextureRenderingSupport() const
1886{
1887 return mFloat32RenderSupport;
1888}
1889
1890bool Renderer11::getFloat16TextureSupport() const
1891{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001892 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001893}
1894
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001895bool Renderer11::getFloat16TextureFilteringSupport() const
1896{
1897 return mFloat16FilterSupport;
1898}
1899
1900bool Renderer11::getFloat16TextureRenderingSupport() const
1901{
1902 return mFloat16RenderSupport;
1903}
1904
Geoff Langd42cf4e2013-06-05 16:09:17 -04001905bool Renderer11::getRGB565TextureSupport() const
1906{
1907 return false;
1908}
1909
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001910bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001911{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001912 return false;
1913}
1914
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001915bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001916{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001917 return false;
1918}
1919
Geoff Lang632192d2013-10-04 13:40:46 -04001920bool Renderer11::getRGTextureSupport() const
1921{
1922 return mRGTextureSupport;
1923}
1924
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001925bool Renderer11::getTextureFilterAnisotropySupport() const
1926{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001927 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001928}
1929
1930float Renderer11::getTextureMaxAnisotropy() const
1931{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001932 switch (mFeatureLevel)
1933 {
1934 case D3D_FEATURE_LEVEL_11_0:
1935 return D3D11_MAX_MAXANISOTROPY;
1936 case D3D_FEATURE_LEVEL_10_1:
1937 case D3D_FEATURE_LEVEL_10_0:
1938 return D3D10_MAX_MAXANISOTROPY;
1939 default: UNREACHABLE();
1940 return 0;
1941 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001942}
1943
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001944bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001945{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00001946 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001947}
1948
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001949Range Renderer11::getViewportBounds() const
1950{
1951 switch (mFeatureLevel)
1952 {
1953 case D3D_FEATURE_LEVEL_11_0:
1954 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1955 case D3D_FEATURE_LEVEL_10_1:
1956 case D3D_FEATURE_LEVEL_10_0:
1957 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1958 default: UNREACHABLE();
1959 return Range(0, 0);
1960 }
1961}
1962
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001963unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001964{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001965 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1966 switch (mFeatureLevel)
1967 {
1968 case D3D_FEATURE_LEVEL_11_0:
1969 case D3D_FEATURE_LEVEL_10_1:
1970 case D3D_FEATURE_LEVEL_10_0:
1971 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1972 default: UNREACHABLE();
1973 return 0;
1974 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001975}
1976
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001977unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1978{
1979 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1980}
1981
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001982unsigned int Renderer11::getReservedVertexUniformVectors() const
1983{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001984 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001985}
1986
1987unsigned int Renderer11::getReservedFragmentUniformVectors() const
1988{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001989 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001990}
1991
1992unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001993{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001994 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1995 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1996 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001997}
1998
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001999unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002000{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002001 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2002 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2003 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002004}
2005
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002006unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002007{
2008 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00002009 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
2010 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002011 switch (mFeatureLevel)
2012 {
2013 case D3D_FEATURE_LEVEL_11_0:
2014 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2015 case D3D_FEATURE_LEVEL_10_1:
2016 case D3D_FEATURE_LEVEL_10_0:
2017 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2018 default: UNREACHABLE();
2019 return 0;
2020 }
2021}
2022
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002023unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2024{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002025 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2026 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2027
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002028 switch (mFeatureLevel)
2029 {
2030 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002031 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002032 case D3D_FEATURE_LEVEL_10_1:
2033 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002034 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002035 default: UNREACHABLE();
2036 return 0;
2037 }
2038}
2039
2040unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2041{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002042 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2043 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2044
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002045 switch (mFeatureLevel)
2046 {
2047 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002048 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002049 case D3D_FEATURE_LEVEL_10_1:
2050 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002051 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002052 default: UNREACHABLE();
2053 return 0;
2054 }
2055}
2056
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002057unsigned int Renderer11::getReservedVertexUniformBuffers() const
2058{
2059 // we reserve one buffer for the application uniforms, and one for driver uniforms
2060 return 2;
2061}
2062
2063unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2064{
2065 // we reserve one buffer for the application uniforms, and one for driver uniforms
2066 return 2;
2067}
2068
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002069unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2070{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002071 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2072 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2073
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002074 switch (mFeatureLevel)
2075 {
2076 case D3D_FEATURE_LEVEL_11_0:
2077 return D3D11_SO_BUFFER_SLOT_COUNT;
2078 case D3D_FEATURE_LEVEL_10_1:
2079 case D3D_FEATURE_LEVEL_10_0:
2080 return D3D10_SO_BUFFER_SLOT_COUNT;
2081 default: UNREACHABLE();
2082 return 0;
2083 }
2084}
2085
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002086unsigned int Renderer11::getMaxUniformBufferSize() const
2087{
2088 // Each component is a 4-element vector of 4-byte units (floats)
2089 const unsigned int bytesPerComponent = 4 * sizeof(float);
2090
2091 switch (mFeatureLevel)
2092 {
2093 case D3D_FEATURE_LEVEL_11_0:
2094 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2095 case D3D_FEATURE_LEVEL_10_1:
2096 case D3D_FEATURE_LEVEL_10_0:
2097 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2098 default: UNREACHABLE();
2099 return 0;
2100 }
2101}
2102
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002103bool Renderer11::getNonPower2TextureSupport() const
2104{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002105 switch (mFeatureLevel)
2106 {
2107 case D3D_FEATURE_LEVEL_11_0:
2108 case D3D_FEATURE_LEVEL_10_1:
2109 case D3D_FEATURE_LEVEL_10_0:
2110 return true;
2111 default: UNREACHABLE();
2112 return false;
2113 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002114}
2115
2116bool Renderer11::getOcclusionQuerySupport() const
2117{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002118 switch (mFeatureLevel)
2119 {
2120 case D3D_FEATURE_LEVEL_11_0:
2121 case D3D_FEATURE_LEVEL_10_1:
2122 case D3D_FEATURE_LEVEL_10_0:
2123 return true;
2124 default: UNREACHABLE();
2125 return false;
2126 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002127}
2128
2129bool Renderer11::getInstancingSupport() const
2130{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002131 switch (mFeatureLevel)
2132 {
2133 case D3D_FEATURE_LEVEL_11_0:
2134 case D3D_FEATURE_LEVEL_10_1:
2135 case D3D_FEATURE_LEVEL_10_0:
2136 return true;
2137 default: UNREACHABLE();
2138 return false;
2139 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002140}
2141
2142bool Renderer11::getShareHandleSupport() const
2143{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002144 // We only currently support share handles with BGRA surfaces, because
2145 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002146 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002147 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002148}
2149
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002150bool Renderer11::getDerivativeInstructionSupport() const
2151{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002152 switch (mFeatureLevel)
2153 {
2154 case D3D_FEATURE_LEVEL_11_0:
2155 case D3D_FEATURE_LEVEL_10_1:
2156 case D3D_FEATURE_LEVEL_10_0:
2157 return true;
2158 default: UNREACHABLE();
2159 return false;
2160 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002161}
2162
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002163bool Renderer11::getPostSubBufferSupport() const
2164{
2165 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2166 return false;
2167}
2168
Jamie Madill13a2f852013-12-11 16:35:08 -05002169int Renderer11::getMaxRecommendedElementsIndices() const
2170{
2171 META_ASSERT(D3D11_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2172 META_ASSERT(D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP == 32);
2173
2174 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2175 return std::numeric_limits<GLint>::max();
2176}
2177
2178int Renderer11::getMaxRecommendedElementsVertices() const
2179{
2180 META_ASSERT(D3D11_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2181 META_ASSERT(D3D10_REQ_DRAW_VERTEX_COUNT_2_TO_EXP == 32);
2182
2183 // D3D11 allows up to 2^32 elements, but we report max signed int for convenience.
2184 return std::numeric_limits<GLint>::max();
2185}
2186
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002187int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002188{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002189 switch (mFeatureLevel)
2190 {
2191 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002192 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002193 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2194 default: UNREACHABLE(); return 0;
2195 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002196}
2197
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002198int Renderer11::getMinorShaderModel() const
2199{
2200 switch (mFeatureLevel)
2201 {
2202 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2203 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2204 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2205 default: UNREACHABLE(); return 0;
2206 }
2207}
2208
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002209float Renderer11::getMaxPointSize() const
2210{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002211 // choose a reasonable maximum. we enforce this in the shader.
2212 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2213 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002214}
2215
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002216int Renderer11::getMaxViewportDimension() const
2217{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002218 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2219 // In our case return the maximum texture size, which is the maximum render buffer size.
2220 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2221 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2222
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002223 switch (mFeatureLevel)
2224 {
2225 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002226 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002227 case D3D_FEATURE_LEVEL_10_1:
2228 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002229 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002230 default: UNREACHABLE();
2231 return 0;
2232 }
2233}
2234
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002235int Renderer11::getMaxTextureWidth() const
2236{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002237 switch (mFeatureLevel)
2238 {
2239 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2240 case D3D_FEATURE_LEVEL_10_1:
2241 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2242 default: UNREACHABLE(); return 0;
2243 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002244}
2245
2246int Renderer11::getMaxTextureHeight() const
2247{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002248 switch (mFeatureLevel)
2249 {
2250 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2251 case D3D_FEATURE_LEVEL_10_1:
2252 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2253 default: UNREACHABLE(); return 0;
2254 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002255}
2256
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002257int Renderer11::getMaxTextureDepth() const
2258{
2259 switch (mFeatureLevel)
2260 {
2261 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2262 case D3D_FEATURE_LEVEL_10_1:
2263 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2264 default: UNREACHABLE(); return 0;
2265 }
2266}
2267
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002268int Renderer11::getMaxTextureArrayLayers() const
2269{
2270 switch (mFeatureLevel)
2271 {
2272 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2273 case D3D_FEATURE_LEVEL_10_1:
2274 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2275 default: UNREACHABLE(); return 0;
2276 }
2277}
2278
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002279bool Renderer11::get32BitIndexSupport() const
2280{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002281 switch (mFeatureLevel)
2282 {
2283 case D3D_FEATURE_LEVEL_11_0:
2284 case D3D_FEATURE_LEVEL_10_1:
2285 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2286 default: UNREACHABLE(); return false;
2287 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002288}
2289
2290int Renderer11::getMinSwapInterval() const
2291{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002292 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002293}
2294
2295int Renderer11::getMaxSwapInterval() const
2296{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002297 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002298}
2299
2300int Renderer11::getMaxSupportedSamples() const
2301{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002302 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002303}
2304
Geoff Lang005df412013-10-16 14:12:50 -04002305GLsizei Renderer11::getMaxSupportedFormatSamples(GLenum internalFormat) const
Geoff Lang0e120e32013-05-29 10:23:55 -04002306{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002307 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002308 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2309 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2310}
2311
Geoff Lang005df412013-10-16 14:12:50 -04002312GLsizei Renderer11::getNumSampleCounts(GLenum internalFormat) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002313{
2314 unsigned int numCounts = 0;
2315
2316 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002317 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2318 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002319 {
2320 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2321 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2322
2323 if (iter != mMultisampleSupportMap.end())
2324 {
2325 const MultisampleSupportInfo& info = iter->second;
2326 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2327 {
2328 if (info.qualityLevels[i] > 0)
2329 {
2330 numCounts++;
2331 }
2332 }
2333 }
2334 }
2335
2336 return numCounts;
2337}
2338
Geoff Lang005df412013-10-16 14:12:50 -04002339void Renderer11::getSampleCounts(GLenum internalFormat, GLsizei bufSize, GLint *params) const
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002340{
2341 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002342 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2343 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2344 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002345 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002346 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002347
2348 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2349 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2350
2351 if (iter != mMultisampleSupportMap.end())
2352 {
2353 const MultisampleSupportInfo& info = iter->second;
2354 int bufPos = 0;
2355 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2356 {
2357 if (info.qualityLevels[i] > 0)
2358 {
2359 params[bufPos++] = i + 1;
2360 }
2361 }
2362 }
2363}
2364
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002365int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2366{
2367 if (requested == 0)
2368 {
2369 return 0;
2370 }
2371
2372 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2373 if (iter != mMultisampleSupportMap.end())
2374 {
2375 const MultisampleSupportInfo& info = iter->second;
2376 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2377 {
2378 if (info.qualityLevels[i] > 0)
2379 {
2380 return i + 1;
2381 }
2382 }
2383 }
2384
2385 return -1;
2386}
2387
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002388unsigned int Renderer11::getMaxRenderTargets() const
2389{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002390 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2391 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2392
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002393 switch (mFeatureLevel)
2394 {
2395 case D3D_FEATURE_LEVEL_11_0:
2396 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2397 case D3D_FEATURE_LEVEL_10_1:
2398 case D3D_FEATURE_LEVEL_10_0:
2399 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2400 default:
2401 UNREACHABLE();
2402 return 1;
2403 }
2404}
2405
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002406bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002407{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002408 if (source && dest)
2409 {
2410 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2411 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2412
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002413 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002414
2415 dest11->invalidateSwizzleCache();
2416
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002417 return true;
2418 }
2419
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002420 return false;
2421}
2422
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002423bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002424{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002425 if (source && dest)
2426 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002427 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2428 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002429
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002430 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002431
2432 dest11->invalidateSwizzleCache();
2433
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002434 return true;
2435 }
2436
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002437 return false;
2438}
2439
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002440bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2441{
2442 if (source && dest)
2443 {
2444 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2445 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2446
2447 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002448
2449 dest11->invalidateSwizzleCache();
2450
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002451 return true;
2452 }
2453
2454 return false;
2455}
2456
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002457bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2458{
2459 if (source && dest)
2460 {
2461 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2462 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2463
2464 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
Geoff Lang42477a42013-09-17 17:07:02 -04002465
2466 dest11->invalidateSwizzleCache();
2467
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002468 return true;
2469 }
2470
2471 return false;
2472}
2473
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002474bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002475 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002476{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002477 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002478 if (!colorbuffer)
2479 {
2480 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002481 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002482 }
2483
2484 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2485 if (!sourceRenderTarget)
2486 {
2487 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002488 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002489 }
2490
2491 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2492 if (!source)
2493 {
2494 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002495 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002496 }
2497
2498 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2499 if (!storage11)
2500 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002501 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002502 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002503 }
2504
2505 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2506 if (!destRenderTarget)
2507 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002508 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002509 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002510 }
2511
2512 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2513 if (!dest)
2514 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002515 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002516 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002517 }
2518
Geoff Langb86b9792013-06-04 16:32:05 -04002519 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2520 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002521
Geoff Langb86b9792013-06-04 16:32:05 -04002522 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2523 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002524
Geoff Langb86b9792013-06-04 16:32:05 -04002525 // Use nearest filtering because source and destination are the same size for the direct
2526 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002527 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002528 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002529
Geoff Lang42477a42013-09-17 17:07:02 -04002530 storage11->invalidateSwizzleCacheLevel(level);
2531
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002532 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002533}
2534
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002535bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002536 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002537{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002538 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002539 if (!colorbuffer)
2540 {
2541 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002542 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002543 }
2544
2545 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2546 if (!sourceRenderTarget)
2547 {
2548 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002549 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002550 }
2551
2552 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2553 if (!source)
2554 {
2555 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002556 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002557 }
2558
2559 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2560 if (!storage11)
2561 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002562 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002563 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002564 }
2565
Nicolas Capensb13f8662013-06-04 13:30:19 -04002566 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002567 if (!destRenderTarget)
2568 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002569 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002570 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002571 }
2572
2573 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2574 if (!dest)
2575 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002576 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002577 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002578 }
2579
Geoff Langb86b9792013-06-04 16:32:05 -04002580 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2581 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002582
Geoff Langb86b9792013-06-04 16:32:05 -04002583 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2584 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002585
Geoff Langb86b9792013-06-04 16:32:05 -04002586 // Use nearest filtering because source and destination are the same size for the direct
2587 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002588 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002589 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002590
Geoff Lang42477a42013-09-17 17:07:02 -04002591 storage11->invalidateSwizzleCacheLevel(level);
2592
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002593 return ret;
2594}
2595
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002596bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2597 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2598{
2599 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2600 if (!colorbuffer)
2601 {
2602 ERR("Failed to retrieve the color buffer from the frame buffer.");
2603 return gl::error(GL_OUT_OF_MEMORY, false);
2604 }
2605
2606 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2607 if (!sourceRenderTarget)
2608 {
2609 ERR("Failed to retrieve the render target from the frame buffer.");
2610 return gl::error(GL_OUT_OF_MEMORY, false);
2611 }
2612
2613 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2614 if (!source)
2615 {
2616 ERR("Failed to retrieve the render target view from the render target.");
2617 return gl::error(GL_OUT_OF_MEMORY, false);
2618 }
2619
2620 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2621 if (!storage11)
2622 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002623 ERR("Failed to retrieve the texture storage from the destination.");
2624 return gl::error(GL_OUT_OF_MEMORY, false);
2625 }
2626
2627 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2628 if (!destRenderTarget)
2629 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002630 ERR("Failed to retrieve the render target from the destination storage.");
2631 return gl::error(GL_OUT_OF_MEMORY, false);
2632 }
2633
2634 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2635 if (!dest)
2636 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002637 ERR("Failed to retrieve the render target view from the destination render target.");
2638 return gl::error(GL_OUT_OF_MEMORY, false);
2639 }
2640
Geoff Langb86b9792013-06-04 16:32:05 -04002641 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2642 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002643
Geoff Langb86b9792013-06-04 16:32:05 -04002644 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2645 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002646
Geoff Langb86b9792013-06-04 16:32:05 -04002647 // Use nearest filtering because source and destination are the same size for the direct
2648 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002649 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002650 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002651
Geoff Lang42477a42013-09-17 17:07:02 -04002652 storage11->invalidateSwizzleCacheLevel(level);
2653
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002654 return ret;
2655}
2656
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002657bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2658 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2659{
2660 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2661 if (!colorbuffer)
2662 {
2663 ERR("Failed to retrieve the color buffer from the frame buffer.");
2664 return gl::error(GL_OUT_OF_MEMORY, false);
2665 }
2666
2667 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2668 if (!sourceRenderTarget)
2669 {
2670 ERR("Failed to retrieve the render target from the frame buffer.");
2671 return gl::error(GL_OUT_OF_MEMORY, false);
2672 }
2673
2674 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2675 if (!source)
2676 {
2677 ERR("Failed to retrieve the render target view from the render target.");
2678 return gl::error(GL_OUT_OF_MEMORY, false);
2679 }
2680
2681 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2682 if (!storage11)
2683 {
Geoff Langea228632013-07-30 15:17:12 -04002684 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002685 ERR("Failed to retrieve the texture storage from the destination.");
2686 return gl::error(GL_OUT_OF_MEMORY, false);
2687 }
2688
2689 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2690 if (!destRenderTarget)
2691 {
Geoff Langea228632013-07-30 15:17:12 -04002692 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002693 ERR("Failed to retrieve the render target from the destination storage.");
2694 return gl::error(GL_OUT_OF_MEMORY, false);
2695 }
2696
2697 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2698 if (!dest)
2699 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002700 ERR("Failed to retrieve the render target view from the destination render target.");
2701 return gl::error(GL_OUT_OF_MEMORY, false);
2702 }
2703
Geoff Langb86b9792013-06-04 16:32:05 -04002704 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2705 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002706
Geoff Langb86b9792013-06-04 16:32:05 -04002707 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2708 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002709
Geoff Langb86b9792013-06-04 16:32:05 -04002710 // Use nearest filtering because source and destination are the same size for the direct
2711 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002712 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002713 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002714
Geoff Lang42477a42013-09-17 17:07:02 -04002715 storage11->invalidateSwizzleCacheLevel(level);
2716
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002717 return ret;
2718}
2719
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002720void Renderer11::unapplyRenderTargets()
2721{
2722 setOneTimeRenderTarget(NULL);
2723}
2724
2725void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2726{
2727 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2728
2729 rtvArray[0] = renderTargetView;
2730
2731 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2732
2733 // Do not preserve the serial for this one-time-use render target
2734 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2735 {
2736 mAppliedRenderTargetSerials[rtIndex] = 0;
2737 }
2738}
2739
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002740RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2741{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002742 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002743 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002744
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002745 if (depth)
2746 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002747 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002748 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
Geoff Lang88f9cbf2013-10-18 14:33:37 -04002749 swapChain11->getDepthStencilTexture(),
2750 swapChain11->getDepthStencilShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002751 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002752 }
2753 else
2754 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002755 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002756 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002757 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002758 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002759 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002760 }
2761 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002762}
2763
Geoff Langa2d97f12013-06-11 11:44:02 -04002764RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002765{
Geoff Langa2d97f12013-06-11 11:44:02 -04002766 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002767 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002768}
2769
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002770ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002771{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002772 ShaderExecutable11 *executable = NULL;
2773
2774 switch (type)
2775 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002776 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002777 {
2778 ID3D11VertexShader *vshader = NULL;
2779 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2780 ASSERT(SUCCEEDED(result));
2781
2782 if (vshader)
2783 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002784 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002785 }
2786 }
2787 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002788 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002789 {
2790 ID3D11PixelShader *pshader = NULL;
2791 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2792 ASSERT(SUCCEEDED(result));
2793
2794 if (pshader)
2795 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002796 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002797 }
2798 }
2799 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002800 case rx::SHADER_GEOMETRY:
2801 {
2802 ID3D11GeometryShader *gshader = NULL;
2803 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2804 ASSERT(SUCCEEDED(result));
2805
2806 if (gshader)
2807 {
2808 executable = new ShaderExecutable11(function, length, gshader);
2809 }
2810 }
2811 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002812 default:
2813 UNREACHABLE();
2814 break;
2815 }
2816
2817 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002818}
2819
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002820ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002821{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002822 const char *profile = NULL;
2823
2824 switch (type)
2825 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002826 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002827 profile = "vs_4_0";
2828 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002829 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002830 profile = "ps_4_0";
2831 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002832 case rx::SHADER_GEOMETRY:
2833 profile = "gs_4_0";
2834 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002835 default:
2836 UNREACHABLE();
2837 return NULL;
2838 }
2839
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00002840 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002841 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002842 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002843 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002844 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002845
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002846 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002847 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002848
2849 return executable;
2850}
2851
Jamie Madill8ff21ae2014-02-04 16:04:05 -05002852rx::UniformStorage *Renderer11::createUniformStorage(size_t storageSize)
2853{
2854 return new UniformStorage11(this, storageSize);
2855}
2856
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002857VertexBuffer *Renderer11::createVertexBuffer()
2858{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002859 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002860}
2861
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002862IndexBuffer *Renderer11::createIndexBuffer()
2863{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002864 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002865}
2866
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002867BufferStorage *Renderer11::createBufferStorage()
2868{
2869 return new BufferStorage11(this);
2870}
2871
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002872QueryImpl *Renderer11::createQuery(GLenum type)
2873{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002874 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002875}
2876
2877FenceImpl *Renderer11::createFence()
2878{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002879 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002880}
2881
Geoff Lang005df412013-10-16 14:12:50 -04002882bool Renderer11::supportsFastCopyBufferToTexture(GLenum internalFormat) const
Jamie Madill0e0510f2013-10-10 15:46:23 -04002883{
Jamie Madill4461f092013-10-10 15:10:39 -04002884 int clientVersion = getCurrentClientVersion();
2885
2886 // We only support buffer to texture copies in ES3
2887 if (clientVersion <= 2)
2888 {
2889 return false;
2890 }
2891
2892 // sRGB formats do not work with D3D11 buffer SRVs
2893 if (gl::GetColorEncoding(internalFormat, clientVersion) == GL_SRGB)
2894 {
2895 return false;
2896 }
2897
2898 // We cannot support direct copies to non-color-renderable formats
2899 if (!gl::IsColorRenderingSupported(internalFormat, this))
2900 {
2901 return false;
2902 }
2903
2904 // We skip all 3-channel formats since sometimes format support is missing
2905 if (gl::GetComponentCount(internalFormat, clientVersion) == 3)
2906 {
2907 return false;
2908 }
2909
2910 // We don't support formats which we can't represent without conversion
2911 if (getNativeTextureFormat(internalFormat) != internalFormat)
2912 {
2913 return false;
2914 }
2915
2916 return true;
Jamie Madill0e0510f2013-10-10 15:46:23 -04002917}
2918
Jamie Madilla21eea32013-09-18 14:36:25 -04002919bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2920 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2921{
Jamie Madill0e0510f2013-10-10 15:46:23 -04002922 ASSERT(supportsFastCopyBufferToTexture(destinationFormat));
Jamie Madilla21eea32013-09-18 14:36:25 -04002923 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2924}
2925
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002926bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002927{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002928 ASSERT(colorbuffer != NULL);
2929
2930 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2931 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002932 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002933 *subresourceIndex = renderTarget->getSubresourceIndex();
2934
2935 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2936 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002937 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002938 ID3D11Resource *textureResource = NULL;
2939 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002940
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002941 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002942 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002943 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002944 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002945
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002946 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002947 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002948 return true;
2949 }
2950 else
2951 {
2952 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2953 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002954 }
2955 }
2956 }
2957 }
2958
2959 return false;
2960}
2961
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002962bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002963 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002964{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002965 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002966 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002967 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002968
2969 if (!readBuffer)
2970 {
2971 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2972 return gl::error(GL_OUT_OF_MEMORY, false);
2973 }
2974
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002975 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002976
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002977 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002978 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002979 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2980 {
2981 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
2982
2983 if (!drawBuffer)
2984 {
2985 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2986 return gl::error(GL_OUT_OF_MEMORY, false);
2987 }
2988
2989 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2990
Geoff Lang125deab2013-08-09 13:34:16 -04002991 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002992 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002993 {
2994 return false;
2995 }
2996 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002997 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002998 }
2999
Geoff Lang685806d2013-06-12 11:16:36 -04003000 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003001 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003002 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
3003 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
3004
3005 if (!readBuffer)
3006 {
3007 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
3008 return gl::error(GL_OUT_OF_MEMORY, false);
3009 }
3010
3011 if (!drawBuffer)
3012 {
3013 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
3014 return gl::error(GL_OUT_OF_MEMORY, false);
3015 }
3016
3017 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3018 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3019
Geoff Lang125deab2013-08-09 13:34:16 -04003020 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04003021 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003022 {
3023 return false;
3024 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003025 }
3026
Geoff Lang42477a42013-09-17 17:07:02 -04003027 invalidateFramebufferSwizzles(drawTarget);
3028
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003029 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003030}
3031
3032void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3033 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3034{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003035 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003036 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003037
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003038 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3039
3040 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003041 {
3042 gl::Rectangle area;
3043 area.x = x;
3044 area.y = y;
3045 area.width = width;
3046 area.height = height;
3047
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003048 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3049 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003050
Geoff Langea228632013-07-30 15:17:12 -04003051 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003052 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003053}
3054
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003055Image *Renderer11::createImage()
3056{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003057 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003058}
3059
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003060void Renderer11::generateMipmap(Image *dest, Image *src)
3061{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003062 Image11 *dest11 = Image11::makeImage11(dest);
3063 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003064 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003065}
3066
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003067TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3068{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003069 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3070 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003071}
3072
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003073TextureStorage *Renderer11::createTextureStorage2D(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003074{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003075 return new TextureStorage11_2D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003076}
3077
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003078TextureStorage *Renderer11::createTextureStorageCube(int baseLevel, int maxLevel, GLenum internalformat, bool renderTarget, int size)
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003079{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003080 return new TextureStorage11_Cube(this, baseLevel, maxLevel, internalformat, renderTarget, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003081}
3082
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003083TextureStorage *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 +00003084{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003085 return new TextureStorage11_3D(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003086}
3087
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003088TextureStorage *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 +00003089{
Jamie Madill4cfff5f2013-10-24 17:49:46 -04003090 return new TextureStorage11_2DArray(this, baseLevel, maxLevel, internalformat, renderTarget, width, height, depth);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003091}
3092
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003093void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3094 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3095 GLint packAlignment, void *pixels)
3096{
3097 D3D11_TEXTURE2D_DESC textureDesc;
3098 texture->GetDesc(&textureDesc);
3099
3100 D3D11_TEXTURE2D_DESC stagingDesc;
3101 stagingDesc.Width = area.width;
3102 stagingDesc.Height = area.height;
3103 stagingDesc.MipLevels = 1;
3104 stagingDesc.ArraySize = 1;
3105 stagingDesc.Format = textureDesc.Format;
3106 stagingDesc.SampleDesc.Count = 1;
3107 stagingDesc.SampleDesc.Quality = 0;
3108 stagingDesc.Usage = D3D11_USAGE_STAGING;
3109 stagingDesc.BindFlags = 0;
3110 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3111 stagingDesc.MiscFlags = 0;
3112
3113 ID3D11Texture2D* stagingTex = NULL;
3114 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3115 if (FAILED(result))
3116 {
3117 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3118 return;
3119 }
3120
3121 ID3D11Texture2D* srcTex = NULL;
3122 if (textureDesc.SampleDesc.Count > 1)
3123 {
3124 D3D11_TEXTURE2D_DESC resolveDesc;
3125 resolveDesc.Width = textureDesc.Width;
3126 resolveDesc.Height = textureDesc.Height;
3127 resolveDesc.MipLevels = 1;
3128 resolveDesc.ArraySize = 1;
3129 resolveDesc.Format = textureDesc.Format;
3130 resolveDesc.SampleDesc.Count = 1;
3131 resolveDesc.SampleDesc.Quality = 0;
3132 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3133 resolveDesc.BindFlags = 0;
3134 resolveDesc.CPUAccessFlags = 0;
3135 resolveDesc.MiscFlags = 0;
3136
3137 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3138 if (FAILED(result))
3139 {
3140 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04003141 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003142 return;
3143 }
3144
3145 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3146 subResource = 0;
3147 }
3148 else
3149 {
3150 srcTex = texture;
3151 srcTex->AddRef();
3152 }
3153
3154 D3D11_BOX srcBox;
3155 srcBox.left = area.x;
3156 srcBox.right = area.x + area.width;
3157 srcBox.top = area.y;
3158 srcBox.bottom = area.y + area.height;
3159 srcBox.front = 0;
3160 srcBox.back = 1;
3161
3162 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3163
Geoff Langea228632013-07-30 15:17:12 -04003164 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003165
3166 D3D11_MAPPED_SUBRESOURCE mapping;
3167 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3168
3169 unsigned char *source;
3170 int inputPitch;
3171 if (packReverseRowOrder)
3172 {
3173 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3174 inputPitch = -static_cast<int>(mapping.RowPitch);
3175 }
3176 else
3177 {
3178 source = static_cast<unsigned char*>(mapping.pData);
3179 inputPitch = static_cast<int>(mapping.RowPitch);
3180 }
3181
Geoff Lang697ad3e2013-06-04 10:11:28 -04003182 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003183
Geoff Lang005df412013-10-16 14:12:50 -04003184 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003185 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3186 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3187
3188 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3189
3190 if (sourceFormat == format && sourceType == type)
3191 {
3192 // Direct copy possible
3193 unsigned char *dest = static_cast<unsigned char*>(pixels);
3194 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003195 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003196 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003197 }
3198 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003199 else
3200 {
Geoff Lang005df412013-10-16 14:12:50 -04003201 GLenum destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003202 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3203
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003204 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003205 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003206 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003207 // Fast copy is possible through some special function
3208 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003209 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003210 for (int x = 0; x < area.width; x++)
3211 {
3212 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3213 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3214
3215 fastCopyFunc(src, dest);
3216 }
3217 }
3218 }
3219 else
3220 {
Jamie Madill9eeecfc2014-01-29 09:26:48 -05003221 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003222 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3223
3224 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3225 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3226 sizeof(temp) >= sizeof(gl::ColorUI) &&
3227 sizeof(temp) >= sizeof(gl::ColorI));
3228
3229 for (int y = 0; y < area.height; y++)
3230 {
3231 for (int x = 0; x < area.width; x++)
3232 {
3233 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3234 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3235
3236 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3237 // will not allow the copy otherwise.
3238 readFunc(src, temp);
3239 writeFunc(temp, dest);
3240 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003241 }
3242 }
3243 }
3244
3245 mDeviceContext->Unmap(stagingTex, 0);
3246
Geoff Langea228632013-07-30 15:17:12 -04003247 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003248}
3249
Geoff Lang758d5b22013-06-11 11:42:50 -04003250bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003251 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3252 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003253{
Geoff Lang975af372013-06-12 11:19:22 -04003254 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3255 // it should never be the case that both color and depth/stencil need to be blitted at
3256 // at the same time.
3257 ASSERT(colorBlit != (depthBlit || stencilBlit));
3258
Geoff Langc1f51be2013-06-11 11:49:14 -04003259 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003260
Geoff Lang4d782732013-07-22 10:44:18 -04003261 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3262 if (!drawRenderTarget)
3263 {
3264 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3265 return gl::error(GL_OUT_OF_MEMORY, false);
3266 }
3267
3268 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3269 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3270 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3271 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3272
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003273 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3274 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003275 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003276 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003277 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003278 }
3279
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003280 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003281 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003282 unsigned int readSubresource = 0;
3283 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003284 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003285 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3286 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003287
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003288 if (unresolvedTexture)
3289 {
3290 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3291 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003292
Geoff Langea228632013-07-30 15:17:12 -04003293 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003294
3295 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3296 if (FAILED(result))
3297 {
Geoff Langea228632013-07-30 15:17:12 -04003298 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003299 return gl::error(GL_OUT_OF_MEMORY, false);
3300 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003301 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003302 }
3303 else
3304 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003305 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003306 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003307 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003308 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003309 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003310 }
3311
Geoff Lang4d782732013-07-22 10:44:18 -04003312 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003313 {
Geoff Lang4d782732013-07-22 10:44:18 -04003314 SafeRelease(readTexture);
3315 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003316 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003317 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003318 }
3319
Geoff Lang125deab2013-08-09 13:34:16 -04003320 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3321 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3322
3323 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3324
3325 bool wholeBufferCopy = !scissorNeeded &&
3326 readRect.x == 0 && readRect.width == readSize.width &&
3327 readRect.y == 0 && readRect.height == readSize.height &&
3328 drawRect.x == 0 && drawRect.width == drawSize.width &&
3329 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003330
Geoff Langc1f51be2013-06-11 11:49:14 -04003331 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003332
Geoff Lang1cd1b212014-02-11 09:42:27 -05003333 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || drawRect.height < 0;
Geoff Lang125deab2013-08-09 13:34:16 -04003334
3335 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3336 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3337 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3338 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3339
3340 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3341 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3342 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3343
Geoff Langc1f51be2013-06-11 11:49:14 -04003344 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003345 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3346 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003347 {
Geoff Lang125deab2013-08-09 13:34:16 -04003348 UINT dstX = drawRect.x;
3349 UINT dstY = drawRect.y;
3350
Geoff Langc1f51be2013-06-11 11:49:14 -04003351 D3D11_BOX readBox;
3352 readBox.left = readRect.x;
3353 readBox.right = readRect.x + readRect.width;
3354 readBox.top = readRect.y;
3355 readBox.bottom = readRect.y + readRect.height;
3356 readBox.front = 0;
3357 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003358
Geoff Lang125deab2013-08-09 13:34:16 -04003359 if (scissorNeeded)
3360 {
3361 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3362 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3363
3364 if (drawRect.x < scissor->x)
3365 {
3366 dstX = scissor->x;
3367 readBox.left += (scissor->x - drawRect.x);
3368 }
3369 if (drawRect.y < scissor->y)
3370 {
3371 dstY = scissor->y;
3372 readBox.top += (scissor->y - drawRect.y);
3373 }
3374 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3375 {
3376 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3377 }
3378 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3379 {
3380 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3381 }
3382 }
3383
Geoff Langc1f51be2013-06-11 11:49:14 -04003384 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3385 // We also require complete framebuffer copies for depth-stencil blit.
3386 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003387
Geoff Lang125deab2013-08-09 13:34:16 -04003388 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003389 readTexture, readSubresource, pSrcBox);
3390 result = true;
3391 }
3392 else
3393 {
3394 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003395 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003396
Geoff Lang975af372013-06-12 11:19:22 -04003397 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003398 {
Geoff Lang975af372013-06-12 11:19:22 -04003399 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003400 drawTexture, drawSubresource, drawArea, drawSize,
3401 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003402 }
3403 else if (depthBlit)
3404 {
Geoff Lang125deab2013-08-09 13:34:16 -04003405 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3406 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003407 }
3408 else if (stencilBlit)
3409 {
3410 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003411 drawTexture, drawSubresource, drawArea, drawSize,
3412 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003413 }
3414 else
3415 {
Geoff Lang685806d2013-06-12 11:16:36 -04003416 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003417 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3418 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003419 }
3420 }
3421
3422 SafeRelease(readTexture);
3423 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003424
3425 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003426}
3427
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003428ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3429{
3430 D3D11_TEXTURE2D_DESC textureDesc;
3431 source->GetDesc(&textureDesc);
3432
3433 if (textureDesc.SampleDesc.Count > 1)
3434 {
3435 D3D11_TEXTURE2D_DESC resolveDesc;
3436 resolveDesc.Width = textureDesc.Width;
3437 resolveDesc.Height = textureDesc.Height;
3438 resolveDesc.MipLevels = 1;
3439 resolveDesc.ArraySize = 1;
3440 resolveDesc.Format = textureDesc.Format;
3441 resolveDesc.SampleDesc.Count = 1;
3442 resolveDesc.SampleDesc.Quality = 0;
3443 resolveDesc.Usage = textureDesc.Usage;
3444 resolveDesc.BindFlags = textureDesc.BindFlags;
3445 resolveDesc.CPUAccessFlags = 0;
3446 resolveDesc.MiscFlags = 0;
3447
3448 ID3D11Texture2D *resolveTexture = NULL;
3449 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3450 if (FAILED(result))
3451 {
3452 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3453 return NULL;
3454 }
3455
3456 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3457 return resolveTexture;
3458 }
3459 else
3460 {
3461 source->AddRef();
3462 return source;
3463 }
3464}
3465
Geoff Lang42477a42013-09-17 17:07:02 -04003466void Renderer11::invalidateRenderbufferSwizzles(gl::Renderbuffer *renderBuffer, int mipLevel)
3467{
3468 TextureStorage *texStorage = renderBuffer->getTextureStorage();
3469 if (texStorage)
3470 {
3471 TextureStorage11 *texStorage11 = TextureStorage11::makeTextureStorage11(texStorage);
3472 if (!texStorage11)
3473 {
3474 ERR("texture storage pointer unexpectedly null.");
3475 return;
3476 }
3477
3478 texStorage11->invalidateSwizzleCacheLevel(mipLevel);
3479 }
3480}
3481
3482void Renderer11::invalidateFramebufferSwizzles(gl::Framebuffer *framebuffer)
3483{
3484 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
3485 {
3486 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
3487 if (colorbuffer)
3488 {
3489 invalidateRenderbufferSwizzles(colorbuffer, framebuffer->getColorbufferMipLevel(colorAttachment));
3490 }
3491 }
3492
3493 gl::Renderbuffer *depthBuffer = framebuffer->getDepthbuffer();
3494 if (depthBuffer)
3495 {
3496 invalidateRenderbufferSwizzles(depthBuffer, framebuffer->getDepthbufferMipLevel());
3497 }
3498
3499 gl::Renderbuffer *stencilBuffer = framebuffer->getStencilbuffer();
3500 if (stencilBuffer)
3501 {
3502 invalidateRenderbufferSwizzles(stencilBuffer, framebuffer->getStencilbufferMipLevel());
3503 }
3504}
3505
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003506bool Renderer11::getLUID(LUID *adapterLuid) const
3507{
3508 adapterLuid->HighPart = 0;
3509 adapterLuid->LowPart = 0;
3510
3511 if (!mDxgiAdapter)
3512 {
3513 return false;
3514 }
3515
3516 DXGI_ADAPTER_DESC adapterDesc;
3517 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3518 {
3519 return false;
3520 }
3521
3522 *adapterLuid = adapterDesc.AdapterLuid;
3523 return true;
3524}
3525
Geoff Lang005df412013-10-16 14:12:50 -04003526GLenum Renderer11::getNativeTextureFormat(GLenum internalFormat) const
Jamie Madillc8c102b2013-10-10 15:10:24 -04003527{
3528 int clientVersion = getCurrentClientVersion();
3529 return d3d11_gl::GetInternalFormat(gl_d3d11::GetTexFormat(internalFormat, clientVersion), clientVersion);
3530}
3531
Jamie Madill95ffb862014-01-29 09:26:59 -05003532rx::VertexConversionType Renderer11::getVertexConversionType(const gl::VertexFormat &vertexFormat) const
3533{
3534 return gl_d3d11::GetVertexConversionType(vertexFormat);
3535}
3536
3537GLenum Renderer11::getVertexComponentType(const gl::VertexFormat &vertexFormat) const
3538{
3539 return d3d11::GetComponentType(gl_d3d11::GetNativeVertexFormat(vertexFormat));
3540}
3541
Geoff Lang61e49a52013-05-29 10:22:58 -04003542Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3543{
3544 MultisampleSupportInfo supportInfo = { 0 };
3545
3546 UINT formatSupport;
3547 HRESULT result;
3548
3549 result = mDevice->CheckFormatSupport(format, &formatSupport);
3550 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3551 {
3552 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3553 {
3554 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3555 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3556 {
3557 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3558 }
3559 else
3560 {
3561 supportInfo.qualityLevels[i - 1] = 0;
3562 }
3563 }
3564 }
3565
3566 return supportInfo;
3567}
3568
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003569}