blob: c56aa1f6ddfe9588e18f5b4b23963e1d5f0f3ba1 [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"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000016#include "libGLESv2/renderer/Renderer11.h"
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +000017#include "libGLESv2/renderer/RenderTarget11.h"
daniel@transgaming.com65e65372012-11-28 19:33:50 +000018#include "libGLESv2/renderer/renderer11_utils.h"
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +000019#include "libGLESv2/renderer/formatutils11.h"
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +000020#include "libGLESv2/renderer/ShaderExecutable11.h"
daniel@transgaming.coma60160b2012-11-28 19:41:15 +000021#include "libGLESv2/renderer/SwapChain11.h"
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000022#include "libGLESv2/renderer/Image11.h"
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +000023#include "libGLESv2/renderer/VertexBuffer11.h"
daniel@transgaming.com11c2af52012-12-20 21:10:01 +000024#include "libGLESv2/renderer/IndexBuffer11.h"
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +000025#include "libGLESv2/renderer/BufferStorage11.h"
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000026#include "libGLESv2/renderer/VertexDataManager.h"
27#include "libGLESv2/renderer/IndexDataManager.h"
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +000028#include "libGLESv2/renderer/TextureStorage11.h"
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +000029#include "libGLESv2/renderer/Query11.h"
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +000030#include "libGLESv2/renderer/Fence11.h"
Geoff Langb86b9792013-06-04 16:32:05 -040031#include "libGLESv2/renderer/Blit11.h"
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +000032
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000033#include "libGLESv2/renderer/shaders/compiled/clear11vs.h"
shannonwoods@chromium.org98949842013-05-30 00:01:51 +000034#include "libGLESv2/renderer/shaders/compiled/clearsingle11ps.h"
35#include "libGLESv2/renderer/shaders/compiled/clearmultiple11ps.h"
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000036
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000037#include "libEGL/Display.h"
38
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000039#ifdef _DEBUG
40// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
41// and conformance tests. to enable all warnings, remove this define.
42#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
43#endif
44
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000045namespace rx
46{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000047static const DXGI_FORMAT RenderTargetFormats[] =
48 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000049 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000050 DXGI_FORMAT_R8G8B8A8_UNORM
51 };
52
53static const DXGI_FORMAT DepthStencilFormats[] =
54 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000055 DXGI_FORMAT_UNKNOWN,
56 DXGI_FORMAT_D24_UNORM_S8_UINT,
57 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000058 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000059
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000060enum
61{
62 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
63};
64
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000065Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
66{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000067 mVertexDataManager = NULL;
68 mIndexDataManager = NULL;
69
daniel@transgaming.comc5114302012-12-20 21:11:36 +000070 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000071 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000072
Geoff Langb86b9792013-06-04 16:32:05 -040073 mBlit = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000074
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000075 mClearResourcesInitialized = false;
76 mClearVB = NULL;
77 mClearIL = NULL;
78 mClearVS = NULL;
shannonwoods@chromium.org98949842013-05-30 00:01:51 +000079 mClearSinglePS = NULL;
80 mClearMultiplePS = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000081 mClearScissorRS = NULL;
82 mClearNoScissorRS = NULL;
83
daniel@transgaming.combdf787f2013-02-01 03:20:36 +000084 mSyncQuery = NULL;
85
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000086 mD3d11Module = NULL;
87 mDxgiModule = NULL;
88
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000089 mDeviceLost = false;
90
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +000091 mMaxSupportedSamples = 0;
92
daniel@transgaming.com25072f62012-11-28 19:31:32 +000093 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000094 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000095 mDxgiAdapter = NULL;
96 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +000097
98 mDriverConstantBufferVS = NULL;
99 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000100
101 mBGRATextureSupport = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +0000102
103 mIsGeometryShaderActive = false;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000104}
105
106Renderer11::~Renderer11()
107{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000108 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000109}
110
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000111Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
112{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000113 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000114 return static_cast<rx::Renderer11*>(renderer);
115}
116
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000117#ifndef __d3d11_1_h__
118#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
119#endif
120
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000121EGLint Renderer11::initialize()
122{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000123 if (!initializeCompiler())
124 {
125 return EGL_NOT_INITIALIZED;
126 }
127
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000128 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
129 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000130
131 if (mD3d11Module == NULL || mDxgiModule == NULL)
132 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000133 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000134 return EGL_NOT_INITIALIZED;
135 }
136
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000137 // create the D3D11 device
138 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000139 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000140
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000141 if (D3D11CreateDevice == NULL)
142 {
143 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
144 return EGL_NOT_INITIALIZED;
145 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000146
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000147 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000148 {
149 D3D_FEATURE_LEVEL_11_0,
150 D3D_FEATURE_LEVEL_10_1,
151 D3D_FEATURE_LEVEL_10_0,
152 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000153
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000154 HRESULT result = S_OK;
155
156#ifdef _DEBUG
157 result = D3D11CreateDevice(NULL,
158 D3D_DRIVER_TYPE_HARDWARE,
159 NULL,
160 D3D11_CREATE_DEVICE_DEBUG,
161 featureLevels,
162 ArraySize(featureLevels),
163 D3D11_SDK_VERSION,
164 &mDevice,
165 &mFeatureLevel,
166 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000167
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000168 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000169 {
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000170 ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n");
171 }
172
173 if (!mDevice || FAILED(result))
174#endif
175 {
176 result = D3D11CreateDevice(NULL,
177 D3D_DRIVER_TYPE_HARDWARE,
178 NULL,
179 0,
180 featureLevels,
181 ArraySize(featureLevels),
182 D3D11_SDK_VERSION,
183 &mDevice,
184 &mFeatureLevel,
185 &mDeviceContext);
186
187 if (!mDevice || FAILED(result))
188 {
189 ERR("Could not create D3D11 device - aborting!\n");
190 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
191 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000192 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000193
194 IDXGIDevice *dxgiDevice = NULL;
195 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
196
197 if (FAILED(result))
198 {
199 ERR("Could not query DXGI device - aborting!\n");
200 return EGL_NOT_INITIALIZED;
201 }
202
203 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
204
205 if (FAILED(result))
206 {
207 ERR("Could not retrieve DXGI adapter - aborting!\n");
208 return EGL_NOT_INITIALIZED;
209 }
210
211 dxgiDevice->Release();
212
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000213 mDxgiAdapter->GetDesc(&mAdapterDescription);
214 memset(mDescription, 0, sizeof(mDescription));
215 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
216
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000217 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
218
219 if (!mDxgiFactory || FAILED(result))
220 {
221 ERR("Could not create DXGI factory - aborting!\n");
222 return EGL_NOT_INITIALIZED;
223 }
224
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000225 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
226#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
227 ID3D11InfoQueue *infoQueue;
228 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
229
230 if (SUCCEEDED(result))
231 {
232 D3D11_MESSAGE_ID hideMessages[] =
233 {
234 D3D11_MESSAGE_ID_DEVICE_OMSETRENDERTARGETS_HAZARD,
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000235 D3D11_MESSAGE_ID_DEVICE_PSSETSHADERRESOURCES_HAZARD,
236 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000237 };
238
239 D3D11_INFO_QUEUE_FILTER filter = {0};
240 filter.DenyList.NumIDs = ArraySize(hideMessages);
241 filter.DenyList.pIDList = hideMessages;
242
243 infoQueue->AddStorageFilterEntries(&filter);
244
245 infoQueue->Release();
246 }
247#endif
248
Geoff Lang61e49a52013-05-29 10:22:58 -0400249 mMaxSupportedSamples = 0;
250
251 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
252 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000253 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400254 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
255 mMultisampleSupportMap.insert(std::make_pair(*i, support));
256 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000257 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000258
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000259 initializeDevice();
260
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000261 // BGRA texture support is optional in feature levels 10 and 10_1
262 UINT formatSupport;
263 result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport);
264 if (FAILED(result))
265 {
266 ERR("Error checking BGRA format support: 0x%08X", result);
267 }
268 else
269 {
270 const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET);
271 mBGRATextureSupport = (formatSupport & flags) == flags;
272 }
273
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000274 // Check floating point texture support
275 static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE;
276 static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET;
277 static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE;
278
279 DXGI_FORMAT float16Formats[] =
280 {
281 DXGI_FORMAT_R16_FLOAT,
282 DXGI_FORMAT_R16G16_FLOAT,
283 DXGI_FORMAT_R16G16B16A16_FLOAT,
284 };
285
286 DXGI_FORMAT float32Formats[] =
287 {
288 DXGI_FORMAT_R32_FLOAT,
289 DXGI_FORMAT_R32G32_FLOAT,
290 DXGI_FORMAT_R32G32B32_FLOAT,
291 DXGI_FORMAT_R32G32B32A32_FLOAT,
292 };
293
294 mFloat16TextureSupport = true;
295 mFloat16FilterSupport = true;
296 mFloat16RenderSupport = true;
297 for (unsigned int i = 0; i < ArraySize(float16Formats); i++)
298 {
299 if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport)))
300 {
301 mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
302 mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
303 mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
304 }
305 else
306 {
307 mFloat16TextureSupport = false;
308 mFloat16RenderSupport = false;
309 mFloat16FilterSupport = false;
310 }
311 }
312
313 mFloat32TextureSupport = true;
314 mFloat32FilterSupport = true;
315 mFloat32RenderSupport = true;
316 for (unsigned int i = 0; i < ArraySize(float32Formats); i++)
317 {
318 if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport)))
319 {
320 mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
321 mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
322 mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
323 }
324 else
325 {
326 mFloat32TextureSupport = false;
327 mFloat32FilterSupport = false;
328 mFloat32RenderSupport = false;
329 }
330 }
331
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +0000332 // Check compressed texture support
333 const unsigned int requiredCompressedTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D;
334
335 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &formatSupport)))
336 {
337 mDXT1TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
338 }
339 else
340 {
341 mDXT1TextureSupport = false;
342 }
343
344 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport)))
345 {
346 mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
347 }
348 else
349 {
350 mDXT3TextureSupport = false;
351 }
352
353 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC5_UNORM, &formatSupport)))
354 {
355 mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
356 }
357 else
358 {
359 mDXT5TextureSupport = false;
360 }
361
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +0000362 // Check depth texture support
363 DXGI_FORMAT depthTextureFormats[] =
364 {
365 DXGI_FORMAT_D16_UNORM,
366 DXGI_FORMAT_D24_UNORM_S8_UINT,
367 };
368
369 static const unsigned int requiredDepthTextureFlags = D3D11_FORMAT_SUPPORT_DEPTH_STENCIL |
370 D3D11_FORMAT_SUPPORT_TEXTURE2D;
371
372 mDepthTextureSupport = true;
373 for (unsigned int i = 0; i < ArraySize(depthTextureFormats); i++)
374 {
375 if (SUCCEEDED(mDevice->CheckFormatSupport(depthTextureFormats[i], &formatSupport)))
376 {
377 mDepthTextureSupport = mDepthTextureSupport && ((formatSupport & requiredDepthTextureFlags) == requiredDepthTextureFlags);
378 }
379 else
380 {
381 mDepthTextureSupport = false;
382 }
383 }
384
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000385 return EGL_SUCCESS;
386}
387
388// do any one-time device initialization
389// NOTE: this is also needed after a device lost/reset
390// to reset the scene status and ensure the default states are reset.
391void Renderer11::initializeDevice()
392{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000393 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000394 mInputLayoutCache.initialize(mDevice, mDeviceContext);
395
396 ASSERT(!mVertexDataManager && !mIndexDataManager);
397 mVertexDataManager = new VertexDataManager(this);
398 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000399
Geoff Langb86b9792013-06-04 16:32:05 -0400400 ASSERT(!mBlit);
401 mBlit = new Blit11(this);
402
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000403 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000404}
405
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000406int Renderer11::generateConfigs(ConfigDesc **configDescList)
407{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000408 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
409 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000410 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
411 int numConfigs = 0;
412
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000413 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000414 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000415 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000416 {
417 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
418
419 UINT formatSupport = 0;
420 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000421
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000422 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
423 {
424 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
425
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000426 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000427
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000428 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
429 {
430 UINT formatSupport = 0;
431 result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
432 depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
433 }
434
435 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000436 {
437 ConfigDesc newConfig;
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +0000438 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat);
439 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000440 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
441 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000442 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000443
444 (*configDescList)[numConfigs++] = newConfig;
445 }
446 }
447 }
448 }
449
450 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000451}
452
453void Renderer11::deleteConfigs(ConfigDesc *configDescList)
454{
455 delete [] (configDescList);
456}
457
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000458void Renderer11::sync(bool block)
459{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000460 if (block)
461 {
462 HRESULT result;
463
464 if (!mSyncQuery)
465 {
466 D3D11_QUERY_DESC queryDesc;
467 queryDesc.Query = D3D11_QUERY_EVENT;
468 queryDesc.MiscFlags = 0;
469
470 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
471 ASSERT(SUCCEEDED(result));
472 }
473
474 mDeviceContext->End(mSyncQuery);
475 mDeviceContext->Flush();
476
477 do
478 {
479 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
480
481 // Keep polling, but allow other threads to do something useful first
482 Sleep(0);
483
484 if (testDeviceLost(true))
485 {
486 return;
487 }
488 }
489 while (result == S_FALSE);
490 }
491 else
492 {
493 mDeviceContext->Flush();
494 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000495}
496
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000497SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
498{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000499 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000500}
501
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000502void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
503{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000504 if (type == gl::SAMPLER_PIXEL)
505 {
506 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
507 {
508 ERR("Pixel shader sampler index %i is not valid.", index);
509 return;
510 }
511
512 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
513 {
514 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
515
516 if (!dxSamplerState)
517 {
518 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
519 "sampler state for pixel shaders at slot %i.", index);
520 }
521
522 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
523
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000524 mCurPixelSamplerStates[index] = samplerState;
525 }
526
527 mForceSetPixelSamplerStates[index] = false;
528 }
529 else if (type == gl::SAMPLER_VERTEX)
530 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000531 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000532 {
533 ERR("Vertex shader sampler index %i is not valid.", index);
534 return;
535 }
536
537 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
538 {
539 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
540
541 if (!dxSamplerState)
542 {
543 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
544 "sampler state for vertex shaders at slot %i.", index);
545 }
546
547 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
548
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000549 mCurVertexSamplerStates[index] = samplerState;
550 }
551
552 mForceSetVertexSamplerStates[index] = false;
553 }
554 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000555}
556
557void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
558{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000559 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000560 unsigned int serial = 0;
561 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000562
563 if (texture)
564 {
565 TextureStorageInterface *texStorage = texture->getNativeTexture();
566 if (texStorage)
567 {
568 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
569 textureSRV = storage11->getSRV();
570 }
571
572 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
573 // missing the shader resource view
574 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000575
576 serial = texture->getTextureSerial();
577 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000578 }
579
580 if (type == gl::SAMPLER_PIXEL)
581 {
582 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
583 {
584 ERR("Pixel shader sampler index %i is not valid.", index);
585 return;
586 }
587
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000588 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
589 {
590 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
591 }
592
593 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000594 }
595 else if (type == gl::SAMPLER_VERTEX)
596 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000597 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000598 {
599 ERR("Vertex shader sampler index %i is not valid.", index);
600 return;
601 }
602
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000603 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
604 {
605 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
606 }
607
608 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000609 }
610 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000611}
612
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000613bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
614{
615 // convert buffers to ID3D11Buffer*
616 ID3D11Buffer *vertexConstantBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = { NULL };
617 ID3D11Buffer *pixelConstantBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = { NULL };
618
619 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
620 {
621 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
622 if (uniformBuffer)
623 {
624 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
625 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(GL_UNIFORM_BUFFER);
626
627 if (!constantBuffer)
628 {
629 return false;
630 }
631
632 vertexConstantBuffers[uniformBufferIndex] = constantBuffer;
633 }
634 }
635
636 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
637 {
638 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
639 if (uniformBuffer)
640 {
641 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
642 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(GL_UNIFORM_BUFFER);
643
644 if (!constantBuffer)
645 {
646 return false;
647 }
648
649 pixelConstantBuffers[uniformBufferIndex] = constantBuffer;
650 }
651 }
652
653 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers(), getMaxVertexShaderUniformBuffers(), vertexConstantBuffers);
654 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers(), getMaxFragmentShaderUniformBuffers(), pixelConstantBuffers);
655
656 return true;
657}
658
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000659void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000660{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000661 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000662 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000663 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
664 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000665 if (!dxRasterState)
666 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000667 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000668 "rasterizer state.");
669 }
670
671 mDeviceContext->RSSetState(dxRasterState);
672
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000673 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000674 }
675
676 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000677}
678
Geoff Lang2a64ee42013-05-31 11:22:40 -0400679void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000680 unsigned int sampleMask)
681{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000682 if (mForceSetBlendState ||
683 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400684 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000685 sampleMask != mCurSampleMask)
686 {
687 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
688 if (!dxBlendState)
689 {
690 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
691 "blend state.");
692 }
693
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000694 float blendColors[4] = {0.0f};
695 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
696 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
697 {
698 blendColors[0] = blendColor.red;
699 blendColors[1] = blendColor.green;
700 blendColors[2] = blendColor.blue;
701 blendColors[3] = blendColor.alpha;
702 }
703 else
704 {
705 blendColors[0] = blendColor.alpha;
706 blendColors[1] = blendColor.alpha;
707 blendColors[2] = blendColor.alpha;
708 blendColors[3] = blendColor.alpha;
709 }
710
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000711 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
712
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000713 mCurBlendState = blendState;
714 mCurBlendColor = blendColor;
715 mCurSampleMask = sampleMask;
716 }
717
718 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000719}
720
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000721void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000722 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000723{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000724 if (mForceSetDepthStencilState ||
725 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
726 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
727 {
728 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
729 stencilRef != stencilBackRef ||
730 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
731 {
732 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
733 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000734 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000735 }
736
737 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
738 if (!dxDepthStencilState)
739 {
740 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
741 "setting the default depth stencil state.");
742 }
743
744 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
745
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000746 mCurDepthStencilState = depthStencilState;
747 mCurStencilRef = stencilRef;
748 mCurStencilBackRef = stencilBackRef;
749 }
750
751 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000752}
753
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000754void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000755{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000756 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
757 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000758 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000759 if (enabled)
760 {
761 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000762 rect.left = std::max(0, scissor.x);
763 rect.top = std::max(0, scissor.y);
764 rect.right = scissor.x + std::max(0, scissor.width);
765 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000766
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000767 mDeviceContext->RSSetScissorRects(1, &rect);
768 }
769
770 if (enabled != mScissorEnabled)
771 {
772 mForceSetRasterState = true;
773 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000774
775 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000776 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000777 }
778
779 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000780}
781
daniel@transgaming.com12985182012-12-20 20:56:31 +0000782bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000783 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000784{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000785 gl::Rectangle actualViewport = viewport;
786 float actualZNear = gl::clamp01(zNear);
787 float actualZFar = gl::clamp01(zFar);
788 if (ignoreViewport)
789 {
790 actualViewport.x = 0;
791 actualViewport.y = 0;
792 actualViewport.width = mRenderTargetDesc.width;
793 actualViewport.height = mRenderTargetDesc.height;
794 actualZNear = 0.0f;
795 actualZFar = 1.0f;
796 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000797
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000798 // Get D3D viewport bounds, which depends on the feature level
799 const Range& viewportBounds = getViewportBounds();
800
801 // 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 +0000802 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000803 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
804 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
805 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
806 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
807 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
808 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000809 dxViewport.MinDepth = actualZNear;
810 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000811
812 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
813 {
814 return false; // Nothing to render
815 }
816
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000817 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
818 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000819
daniel@transgaming.com53670042012-11-28 20:55:51 +0000820 if (viewportChanged)
821 {
822 mDeviceContext->RSSetViewports(1, &dxViewport);
823
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000824 mCurViewport = actualViewport;
825 mCurNear = actualZNear;
826 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000827
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000828 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
829 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
830 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
831 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000832
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000833 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
834 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000835
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000836 mVertexConstants.depthRange[0] = actualZNear;
837 mVertexConstants.depthRange[1] = actualZFar;
838 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000839
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000840 mPixelConstants.depthRange[0] = actualZNear;
841 mPixelConstants.depthRange[1] = actualZFar;
842 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000843 }
844
845 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000846 return true;
847}
848
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000849bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
850{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000851 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000852
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000853 switch (mode)
854 {
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000855 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break;
856 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break;
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000857 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000858 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
859 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
860 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000861 // emulate fans via rewriting index buffer
Shannon Woods5ab33c82013-06-26 15:31:09 -0400862 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000863 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000864 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000865 }
866
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000867 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000868
869 return count > 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000870}
871
872bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000873{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000874 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000875 // Also extract the render target dimensions and view
876 unsigned int renderTargetWidth = 0;
877 unsigned int renderTargetHeight = 0;
878 GLenum renderTargetFormat = 0;
879 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
880 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
881 bool missingColorRenderTarget = true;
882
883 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000884 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000885 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
886
887 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000888 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000889 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
890 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
891
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000892 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000893
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000894 if (!colorbuffer)
895 {
896 ERR("render target pointer unexpectedly null.");
897 return false;
898 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000899
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000900 // check for zero-sized default framebuffer, which is a special case.
901 // in this case we do not wish to modify any state and just silently return false.
902 // this will not report any gl error but will cause the calling method to return.
903 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
904 {
905 return false;
906 }
907
908 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
909
910 // Extract the render target dimensions and view
911 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
912 if (!renderTarget)
913 {
914 ERR("render target pointer unexpectedly null.");
915 return false;
916 }
917
918 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
919 if (!framebufferRTVs[colorAttachment])
920 {
921 ERR("render target view pointer unexpectedly null.");
922 return false;
923 }
924
925 if (missingColorRenderTarget)
926 {
927 renderTargetWidth = colorbuffer->getWidth();
928 renderTargetHeight = colorbuffer->getHeight();
929 renderTargetFormat = colorbuffer->getActualFormat();
930 missingColorRenderTarget = false;
931 }
932 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000933 }
934
935 // Get the depth stencil render buffer and serials
936 gl::Renderbuffer *depthStencil = NULL;
937 unsigned int depthbufferSerial = 0;
938 unsigned int stencilbufferSerial = 0;
939 if (framebuffer->getDepthbufferType() != GL_NONE)
940 {
941 depthStencil = framebuffer->getDepthbuffer();
942 if (!depthStencil)
943 {
944 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000945 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000946 return false;
947 }
948
949 depthbufferSerial = depthStencil->getSerial();
950 }
951 else if (framebuffer->getStencilbufferType() != GL_NONE)
952 {
953 depthStencil = framebuffer->getStencilbuffer();
954 if (!depthStencil)
955 {
956 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000957 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000958 return false;
959 }
960
961 stencilbufferSerial = depthStencil->getSerial();
962 }
963
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000964 // Extract the depth stencil sizes and view
965 unsigned int depthSize = 0;
966 unsigned int stencilSize = 0;
967 ID3D11DepthStencilView* framebufferDSV = NULL;
968 if (depthStencil)
969 {
970 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
971 if (!depthStencilRenderTarget)
972 {
973 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000974 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000975 return false;
976 }
977
978 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
979 if (!framebufferDSV)
980 {
981 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000982 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000983 return false;
984 }
985
986 // If there is no render buffer, the width, height and format values come from
987 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000988 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000989 {
990 renderTargetWidth = depthStencil->getWidth();
991 renderTargetHeight = depthStencil->getHeight();
992 renderTargetFormat = depthStencil->getActualFormat();
993 }
994
995 depthSize = depthStencil->getDepthSize();
996 stencilSize = depthStencil->getStencilSize();
997 }
998
999 // Apply the render target and depth stencil
1000 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001001 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001002 depthbufferSerial != mAppliedDepthbufferSerial ||
1003 stencilbufferSerial != mAppliedStencilbufferSerial)
1004 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001005 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001006
1007 mRenderTargetDesc.width = renderTargetWidth;
1008 mRenderTargetDesc.height = renderTargetHeight;
1009 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001010 mForceSetViewport = true;
1011 mForceSetScissor = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001012
1013 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1014 {
1015 mCurDepthSize = depthSize;
1016 mForceSetRasterState = true;
1017 }
1018
1019 mCurStencilSize = stencilSize;
1020
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001021 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1022 {
1023 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
1024 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001025 mAppliedDepthbufferSerial = depthbufferSerial;
1026 mAppliedStencilbufferSerial = stencilbufferSerial;
1027 mRenderTargetDescInitialized = true;
1028 mDepthStencilInitialized = true;
1029 }
1030
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001031 SafeRelease(framebufferRTVs);
1032 SafeRelease(framebufferDSV);
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001033
1034 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001035}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001036
Jamie Madill57a89722013-07-02 11:57:03 -04001037GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001038 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001039{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001040 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001041 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001042 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001043 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001044 return err;
1045 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001046
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001047 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001048}
1049
daniel@transgaming.com31240482012-11-28 21:06:41 +00001050GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001051{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001052 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001053
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001054 if (err == GL_NO_ERROR)
1055 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001056 if (indexInfo->storage)
1057 {
1058 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1059 {
1060 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1061 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1062
shannonwoods@chromium.org675526e2013-05-30 00:04:49 +00001063 mDeviceContext->IASetIndexBuffer(storage->getBuffer(GL_ELEMENT_ARRAY_BUFFER), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001064
1065 mAppliedIBSerial = 0;
1066 mAppliedStorageIBSerial = storage->getSerial();
1067 mAppliedIBOffset = indexInfo->startOffset;
1068 }
1069 }
1070 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001071 {
1072 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1073
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001074 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001075
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001076 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001077 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001078 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001079 }
1080 }
1081
1082 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001083}
1084
1085void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1086{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001087 if (mode == GL_LINE_LOOP)
1088 {
1089 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1090 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001091 else if (mode == GL_TRIANGLE_FAN)
1092 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001093 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001094 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001095 else if (instances > 0)
1096 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001097 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001098 }
1099 else
1100 {
1101 mDeviceContext->Draw(count, 0);
1102 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001103}
1104
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001105void 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 +00001106{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001107 if (mode == GL_LINE_LOOP)
1108 {
1109 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1110 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001111 else if (mode == GL_TRIANGLE_FAN)
1112 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001113 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1114 }
1115 else if (instances > 0)
1116 {
1117 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001118 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001119 else
1120 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001121 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001122 }
1123}
1124
1125void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1126{
1127 // Get the raw indices for an indexed draw
1128 if (type != GL_NONE && elementArrayBuffer)
1129 {
1130 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001131 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001132 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001133 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001134 }
1135
1136 if (!mLineLoopIB)
1137 {
1138 mLineLoopIB = new StreamingIndexBufferInterface(this);
1139 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1140 {
1141 delete mLineLoopIB;
1142 mLineLoopIB = NULL;
1143
1144 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001145 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001146 }
1147 }
1148
Geoff Langeadfd572013-07-09 15:55:07 -04001149 if (static_cast<unsigned int>(count + 1) > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
1150 {
1151 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1152 return gl::error(GL_OUT_OF_MEMORY);
1153 }
1154
1155 const unsigned int spaceNeeded = (count + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001156 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1157 {
1158 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001159 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001160 }
1161
1162 void* mappedMemory = NULL;
1163 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1164 if (offset == -1 || mappedMemory == NULL)
1165 {
1166 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001167 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001168 }
1169
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001170 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001171 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001172
1173 switch (type)
1174 {
1175 case GL_NONE: // Non-indexed draw
1176 for (int i = 0; i < count; i++)
1177 {
1178 data[i] = i;
1179 }
1180 data[count] = 0;
1181 break;
1182 case GL_UNSIGNED_BYTE:
1183 for (int i = 0; i < count; i++)
1184 {
1185 data[i] = static_cast<const GLubyte*>(indices)[i];
1186 }
1187 data[count] = static_cast<const GLubyte*>(indices)[0];
1188 break;
1189 case GL_UNSIGNED_SHORT:
1190 for (int i = 0; i < count; i++)
1191 {
1192 data[i] = static_cast<const GLushort*>(indices)[i];
1193 }
1194 data[count] = static_cast<const GLushort*>(indices)[0];
1195 break;
1196 case GL_UNSIGNED_INT:
1197 for (int i = 0; i < count; i++)
1198 {
1199 data[i] = static_cast<const GLuint*>(indices)[i];
1200 }
1201 data[count] = static_cast<const GLuint*>(indices)[0];
1202 break;
1203 default: UNREACHABLE();
1204 }
1205
1206 if (!mLineLoopIB->unmapBuffer())
1207 {
1208 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001209 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001210 }
1211
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001212 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001213 {
1214 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1215
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001216 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001217 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001218 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001219 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001220 }
1221
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001222 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001223}
1224
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001225void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001226{
1227 // Get the raw indices for an indexed draw
1228 if (type != GL_NONE && elementArrayBuffer)
1229 {
1230 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001231 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001232 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001233 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001234 }
1235
1236 if (!mTriangleFanIB)
1237 {
1238 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1239 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1240 {
1241 delete mTriangleFanIB;
1242 mTriangleFanIB = NULL;
1243
1244 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001245 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001246 }
1247 }
1248
Geoff Langeadfd572013-07-09 15:55:07 -04001249 const unsigned int numTris = count - 2;
1250
1251 if (numTris * 3 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
1252 {
1253 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1254 return gl::error(GL_OUT_OF_MEMORY);
1255 }
1256
1257 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001258 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1259 {
1260 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001261 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001262 }
1263
1264 void* mappedMemory = NULL;
1265 int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory);
1266 if (offset == -1 || mappedMemory == NULL)
1267 {
1268 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001269 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001270 }
1271
1272 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1273 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
1274
1275 switch (type)
1276 {
1277 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001278 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001279 {
1280 data[i*3 + 0] = 0;
1281 data[i*3 + 1] = i + 1;
1282 data[i*3 + 2] = i + 2;
1283 }
1284 break;
1285 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001286 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001287 {
1288 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1289 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1290 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1291 }
1292 break;
1293 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001294 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001295 {
1296 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1297 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1298 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1299 }
1300 break;
1301 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001302 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001303 {
1304 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1305 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1306 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1307 }
1308 break;
1309 default: UNREACHABLE();
1310 }
1311
1312 if (!mTriangleFanIB->unmapBuffer())
1313 {
1314 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001315 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001316 }
1317
1318 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1319 {
1320 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1321
1322 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1323 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001324 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001325 mAppliedIBOffset = indexBufferOffset;
1326 }
1327
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001328 if (instances > 0)
1329 {
1330 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1331 }
1332 else
1333 {
1334 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1335 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001336}
1337
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001338void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1339{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001340 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001341 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1342
1343 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001344 {
1345 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1346 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001347
daniel@transgaming.come4991412012-12-20 20:55:34 +00001348 ID3D11VertexShader *vertexShader = NULL;
1349 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001350
daniel@transgaming.come4991412012-12-20 20:55:34 +00001351 ID3D11PixelShader *pixelShader = NULL;
1352 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001353
daniel@transgaming.come4991412012-12-20 20:55:34 +00001354 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1355 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001356
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001357 programBinary->dirtyAllUniforms();
1358
1359 mAppliedProgramBinarySerial = programBinarySerial;
1360 }
1361
1362 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001363 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001364
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001365 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001366 {
1367 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001368 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001369 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1370 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001371 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1372 }
1373 else
1374 {
1375 mDeviceContext->GSSetShader(NULL, NULL, 0);
1376 }
1377
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001378 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001379 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001380}
1381
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001382void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001383{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001384 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1385 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001386
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001387 unsigned int totalRegisterCountVS = 0;
1388 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001389
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001390 bool vertexUniformsDirty = false;
1391 bool pixelUniformsDirty = false;
1392
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001393 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1394 {
1395 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001396
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001397 if (uniform->isReferencedByVertexShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001398 {
1399 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001400 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001401 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001402
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001403 if (uniform->isReferencedByFragmentShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001404 {
1405 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001406 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001407 }
1408 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001409
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001410 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1411 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1412
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001413 float (*mapVS)[4] = NULL;
1414 float (*mapPS)[4] = NULL;
1415
Shannon Woods5ab33c82013-06-26 15:31:09 -04001416 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1417 {
1418 D3D11_MAPPED_SUBRESOURCE map = {0};
1419 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1420 ASSERT(SUCCEEDED(result));
1421 mapVS = (float(*)[4])map.pData;
1422 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001423
Shannon Woods5ab33c82013-06-26 15:31:09 -04001424 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1425 {
1426 D3D11_MAPPED_SUBRESOURCE map = {0};
1427 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1428 ASSERT(SUCCEEDED(result));
1429 mapPS = (float(*)[4])map.pData;
1430 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001431
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001432 for (gl::UniformArray::iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001433 {
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001434 gl::Uniform *uniform = *uniform_iterator;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001435
Nicolas Capense6050882013-07-08 10:43:10 -04001436 if (!gl::IsSampler(uniform->type))
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001437 {
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001438 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001439 {
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001440 memcpy(mapVS + uniform->vsRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4]));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001441 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001442
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001443 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001444 {
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001445 memcpy(mapPS + uniform->psRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4]));
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001446 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001447 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001448
1449 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001450 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001451
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001452 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001453 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001454 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001455 }
1456
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001457 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001458 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001459 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001460 }
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001461
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001462 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1463 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001464
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001465 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001466 if (!mDriverConstantBufferVS)
1467 {
1468 D3D11_BUFFER_DESC constantBufferDescription = {0};
1469 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1470 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1471 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1472 constantBufferDescription.CPUAccessFlags = 0;
1473 constantBufferDescription.MiscFlags = 0;
1474 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001475
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001476 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001477 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001478
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001479 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1480 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001481
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001482 if (!mDriverConstantBufferPS)
1483 {
1484 D3D11_BUFFER_DESC constantBufferDescription = {0};
1485 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1486 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1487 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1488 constantBufferDescription.CPUAccessFlags = 0;
1489 constantBufferDescription.MiscFlags = 0;
1490 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001491
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001492 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001493 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001494
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001495 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1496 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001497
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001498 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1499 {
1500 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1501 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1502 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001503
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001504 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1505 {
1506 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1507 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1508 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001509
1510 // needed for the point sprite geometry shader
1511 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001512}
1513
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001514void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001515{
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001516 bool alphaUnmasked = gl::GetAlphaBits(mRenderTargetDesc.format, getCurrentClientVersion()) == 0 ||
1517 clearParams.colorMaskAlpha;
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001518 bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1519 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1520 clearParams.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001521
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001522 unsigned int stencilUnmasked = 0x0;
1523 if (frameBuffer->hasStencil())
1524 {
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001525 unsigned int stencilSize = gl::GetStencilBits(frameBuffer->getStencilbuffer()->getActualFormat(),
1526 getCurrentClientVersion());
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001527 stencilUnmasked = (0x1 << stencilSize) - 1;
1528 }
1529 bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1530 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001531
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001532 bool needScissoredClear = mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1533 mCurScissor.x + mCurScissor.width < mRenderTargetDesc.width ||
1534 mCurScissor.y + mCurScissor.height < mRenderTargetDesc.height);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001535
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001536 if (needMaskedColorClear || needMaskedStencilClear || needScissoredClear)
1537 {
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001538 maskedClear(clearParams, frameBuffer->usingExtendedDrawBuffers());
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001539 }
1540 else
1541 {
1542 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1543 {
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001544 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001545 {
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001546 if (frameBuffer->isEnabledColorAttachment(colorAttachment))
1547 {
1548 gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer(colorAttachment);
1549 if (renderbufferObject)
1550 {
1551 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
1552 if (!renderTarget)
1553 {
1554 ERR("render target pointer unexpectedly null.");
1555 return;
1556 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001557
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001558 ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView();
1559 if (!framebufferRTV)
1560 {
1561 ERR("render target view pointer unexpectedly null.");
1562 return;
1563 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001564
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001565 const float clearValues[4] = { clearParams.colorClearValue.red,
1566 clearParams.colorClearValue.green,
1567 clearParams.colorClearValue.blue,
1568 clearParams.colorClearValue.alpha };
1569 mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001570
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001571 framebufferRTV->Release();
1572 }
1573 }
1574 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001575 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001576 if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001577 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001578 gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer();
1579 if (renderbufferObject)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001580 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001581 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil());
1582 if (!renderTarget)
1583 {
1584 ERR("render target pointer unexpectedly null.");
1585 return;
1586 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001587
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001588 ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView();
1589 if (!framebufferDSV)
1590 {
1591 ERR("depth stencil view pointer unexpectedly null.");
1592 return;
1593 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001594
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001595 UINT clearFlags = 0;
1596 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1597 {
1598 clearFlags |= D3D11_CLEAR_DEPTH;
1599 }
1600 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1601 {
1602 clearFlags |= D3D11_CLEAR_STENCIL;
1603 }
1604
shannon.woods@transgaming.combe211b32013-02-28 23:17:16 +00001605 float depthClear = gl::clamp01(clearParams.depthClearValue);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001606 UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF;
1607
1608 mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001609
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001610 framebufferDSV->Release();
1611 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001612 }
1613 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001614}
1615
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001616void Renderer11::maskedClear(const gl::ClearParameters &clearParams, bool usingExtendedDrawBuffers)
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001617{
1618 HRESULT result;
1619
1620 if (!mClearResourcesInitialized)
1621 {
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001622 ASSERT(!mClearVB && !mClearVS && !mClearSinglePS && !mClearMultiplePS && !mClearScissorRS && !mClearNoScissorRS);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001623
1624 D3D11_BUFFER_DESC vbDesc;
1625 vbDesc.ByteWidth = sizeof(d3d11::PositionDepthColorVertex) * 4;
1626 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
1627 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
1628 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1629 vbDesc.MiscFlags = 0;
1630 vbDesc.StructureByteStride = 0;
1631
1632 result = mDevice->CreateBuffer(&vbDesc, NULL, &mClearVB);
1633 ASSERT(SUCCEEDED(result));
1634 d3d11::SetDebugName(mClearVB, "Renderer11 masked clear vertex buffer");
1635
1636 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
1637 {
1638 { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1639 { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1640 };
1641
1642 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Clear, sizeof(g_VS_Clear), &mClearIL);
1643 ASSERT(SUCCEEDED(result));
1644 d3d11::SetDebugName(mClearIL, "Renderer11 masked clear input layout");
1645
1646 result = mDevice->CreateVertexShader(g_VS_Clear, sizeof(g_VS_Clear), NULL, &mClearVS);
1647 ASSERT(SUCCEEDED(result));
1648 d3d11::SetDebugName(mClearVS, "Renderer11 masked clear vertex shader");
1649
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001650 result = mDevice->CreatePixelShader(g_PS_ClearSingle, sizeof(g_PS_ClearSingle), NULL, &mClearSinglePS);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001651 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001652 d3d11::SetDebugName(mClearSinglePS, "Renderer11 masked clear pixel shader (1 RT)");
1653
1654 result = mDevice->CreatePixelShader(g_PS_ClearMultiple, sizeof(g_PS_ClearMultiple), NULL, &mClearMultiplePS);
1655 ASSERT(SUCCEEDED(result));
1656 d3d11::SetDebugName(mClearMultiplePS, "Renderer11 masked clear pixel shader (MRT)");
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001657
1658 D3D11_RASTERIZER_DESC rsScissorDesc;
1659 rsScissorDesc.FillMode = D3D11_FILL_SOLID;
1660 rsScissorDesc.CullMode = D3D11_CULL_NONE;
1661 rsScissorDesc.FrontCounterClockwise = FALSE;
1662 rsScissorDesc.DepthBias = 0;
1663 rsScissorDesc.DepthBiasClamp = 0.0f;
1664 rsScissorDesc.SlopeScaledDepthBias = 0.0f;
1665 rsScissorDesc.DepthClipEnable = FALSE;
1666 rsScissorDesc.ScissorEnable = TRUE;
1667 rsScissorDesc.MultisampleEnable = FALSE;
1668 rsScissorDesc.AntialiasedLineEnable = FALSE;
1669
1670 result = mDevice->CreateRasterizerState(&rsScissorDesc, &mClearScissorRS);
1671 ASSERT(SUCCEEDED(result));
1672 d3d11::SetDebugName(mClearScissorRS, "Renderer11 masked clear scissor rasterizer state");
1673
1674 D3D11_RASTERIZER_DESC rsNoScissorDesc;
1675 rsNoScissorDesc.FillMode = D3D11_FILL_SOLID;
1676 rsNoScissorDesc.CullMode = D3D11_CULL_NONE;
1677 rsNoScissorDesc.FrontCounterClockwise = FALSE;
1678 rsNoScissorDesc.DepthBias = 0;
1679 rsNoScissorDesc.DepthBiasClamp = 0.0f;
1680 rsNoScissorDesc.SlopeScaledDepthBias = 0.0f;
1681 rsNoScissorDesc.DepthClipEnable = FALSE;
1682 rsNoScissorDesc.ScissorEnable = FALSE;
1683 rsNoScissorDesc.MultisampleEnable = FALSE;
1684 rsNoScissorDesc.AntialiasedLineEnable = FALSE;
1685
1686 result = mDevice->CreateRasterizerState(&rsNoScissorDesc, &mClearNoScissorRS);
1687 ASSERT(SUCCEEDED(result));
1688 d3d11::SetDebugName(mClearNoScissorRS, "Renderer11 masked clear no scissor rasterizer state");
1689
1690 mClearResourcesInitialized = true;
1691 }
1692
1693 // Prepare the depth stencil state to write depth values if the depth should be cleared
1694 // and stencil values if the stencil should be cleared
1695 gl::DepthStencilState glDSState;
1696 glDSState.depthTest = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1697 glDSState.depthFunc = GL_ALWAYS;
1698 glDSState.depthMask = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1699 glDSState.stencilTest = (clearParams.mask & GL_STENCIL_BUFFER_BIT) != 0;
1700 glDSState.stencilFunc = GL_ALWAYS;
1701 glDSState.stencilMask = 0;
1702 glDSState.stencilFail = GL_REPLACE;
1703 glDSState.stencilPassDepthFail = GL_REPLACE;
1704 glDSState.stencilPassDepthPass = GL_REPLACE;
1705 glDSState.stencilWritemask = clearParams.stencilWriteMask;
1706 glDSState.stencilBackFunc = GL_ALWAYS;
1707 glDSState.stencilBackMask = 0;
1708 glDSState.stencilBackFail = GL_REPLACE;
1709 glDSState.stencilBackPassDepthFail = GL_REPLACE;
1710 glDSState.stencilBackPassDepthPass = GL_REPLACE;
1711 glDSState.stencilBackWritemask = clearParams.stencilWriteMask;
1712
1713 int stencilClear = clearParams.stencilClearValue & 0x000000FF;
1714
1715 ID3D11DepthStencilState *dsState = mStateCache.getDepthStencilState(glDSState);
1716
1717 // Prepare the blend state to use a write mask if the color buffer should be cleared
1718 gl::BlendState glBlendState;
1719 glBlendState.blend = false;
1720 glBlendState.sourceBlendRGB = GL_ONE;
1721 glBlendState.destBlendRGB = GL_ZERO;
1722 glBlendState.sourceBlendAlpha = GL_ONE;
1723 glBlendState.destBlendAlpha = GL_ZERO;
1724 glBlendState.blendEquationRGB = GL_FUNC_ADD;
1725 glBlendState.blendEquationAlpha = GL_FUNC_ADD;
1726 glBlendState.colorMaskRed = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskRed : false;
1727 glBlendState.colorMaskGreen = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskGreen : false;
1728 glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false;
shannon.woods@transgaming.com50ea9932013-02-28 23:13:40 +00001729 glBlendState.colorMaskAlpha = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskAlpha : false;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001730 glBlendState.sampleAlphaToCoverage = false;
1731 glBlendState.dither = false;
1732
1733 static const float blendFactors[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
1734 static const UINT sampleMask = 0xFFFFFFFF;
1735
1736 ID3D11BlendState *blendState = mStateCache.getBlendState(glBlendState);
1737
1738 // Set the vertices
1739 D3D11_MAPPED_SUBRESOURCE mappedResource;
1740 result = mDeviceContext->Map(mClearVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
1741 if (FAILED(result))
1742 {
1743 ERR("Failed to map masked clear vertex buffer, HRESULT: 0x%X.", result);
1744 return;
1745 }
1746
1747 d3d11::PositionDepthColorVertex *vertices = reinterpret_cast<d3d11::PositionDepthColorVertex*>(mappedResource.pData);
1748
1749 float depthClear = gl::clamp01(clearParams.depthClearValue);
1750 d3d11::SetPositionDepthColorVertex(&vertices[0], -1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1751 d3d11::SetPositionDepthColorVertex(&vertices[1], -1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1752 d3d11::SetPositionDepthColorVertex(&vertices[2], 1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1753 d3d11::SetPositionDepthColorVertex(&vertices[3], 1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1754
1755 mDeviceContext->Unmap(mClearVB, 0);
1756
1757 // Apply state
1758 mDeviceContext->OMSetBlendState(blendState, blendFactors, sampleMask);
1759 mDeviceContext->OMSetDepthStencilState(dsState, stencilClear);
1760 mDeviceContext->RSSetState(mScissorEnabled ? mClearScissorRS : mClearNoScissorRS);
1761
1762 // Apply shaders
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001763 ID3D11PixelShader *pixelShader = usingExtendedDrawBuffers ? mClearMultiplePS : mClearSinglePS;
1764
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001765 mDeviceContext->IASetInputLayout(mClearIL);
1766 mDeviceContext->VSSetShader(mClearVS, NULL, 0);
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001767 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
shannon.woods@transgaming.com2c53e472013-02-28 23:13:56 +00001768 mDeviceContext->GSSetShader(NULL, NULL, 0);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001769
1770 // Apply vertex buffer
1771 static UINT stride = sizeof(d3d11::PositionDepthColorVertex);
1772 static UINT startIdx = 0;
1773 mDeviceContext->IASetVertexBuffers(0, 1, &mClearVB, &stride, &startIdx);
1774 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
1775
1776 // Draw the clear quad
1777 mDeviceContext->Draw(4, 0);
1778
1779 // Clean up
1780 markAllStateDirty();
1781}
1782
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001783void Renderer11::markAllStateDirty()
1784{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001785 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1786 {
1787 mAppliedRenderTargetSerials[rtIndex] = 0;
1788 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001789 mAppliedDepthbufferSerial = 0;
1790 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001791 mDepthStencilInitialized = false;
1792 mRenderTargetDescInitialized = false;
1793
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001794 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001795 {
1796 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001797 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001798 }
1799 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1800 {
1801 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001802 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001803 }
1804
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001805 mForceSetBlendState = true;
1806 mForceSetRasterState = true;
1807 mForceSetDepthStencilState = true;
1808 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001809 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001810
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001811 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001812 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001813 mAppliedIBOffset = 0;
1814
daniel@transgaming.come4991412012-12-20 20:55:34 +00001815 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001816 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1817 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001818}
1819
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001820void Renderer11::releaseDeviceResources()
1821{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001822 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001823 mInputLayoutCache.clear();
1824
1825 delete mVertexDataManager;
1826 mVertexDataManager = NULL;
1827
1828 delete mIndexDataManager;
1829 mIndexDataManager = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001830
1831 delete mLineLoopIB;
1832 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001833
1834 delete mTriangleFanIB;
1835 mTriangleFanIB = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001836
Geoff Langb86b9792013-06-04 16:32:05 -04001837 delete mBlit;
1838 mBlit = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001839
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001840 SafeRelease(mClearVB);
1841 SafeRelease(mClearIL);
1842 SafeRelease(mClearVS);
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001843 SafeRelease(mClearSinglePS);
1844 SafeRelease(mClearMultiplePS);
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001845 SafeRelease(mClearScissorRS);
1846 SafeRelease(mClearNoScissorRS);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001847
1848 mClearResourcesInitialized = false;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001849
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001850 SafeRelease(mDriverConstantBufferVS);
1851 SafeRelease(mDriverConstantBufferPS);
1852 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001853}
1854
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001855void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001856{
1857 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001858 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001859}
1860
1861bool Renderer11::isDeviceLost()
1862{
1863 return mDeviceLost;
1864}
1865
1866// set notify to true to broadcast a message to all contexts of the device loss
1867bool Renderer11::testDeviceLost(bool notify)
1868{
1869 bool isLost = false;
1870
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001871 // GetRemovedReason is used to test if the device is removed
1872 HRESULT result = mDevice->GetDeviceRemovedReason();
1873 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001874
1875 if (isLost)
1876 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001877 // Log error if this is a new device lost event
1878 if (mDeviceLost == false)
1879 {
1880 ERR("The D3D11 device was removed: 0x%08X", result);
1881 }
1882
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001883 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001884 // we'll probably get this done again by notifyDeviceLost
1885 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001886 // Note that we don't want to clear the device loss status here
1887 // -- this needs to be done by resetDevice
1888 mDeviceLost = true;
1889 if (notify)
1890 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001891 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001892 }
1893 }
1894
1895 return isLost;
1896}
1897
1898bool Renderer11::testDeviceResettable()
1899{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001900 // determine if the device is resettable by creating a dummy device
1901 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001902
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001903 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001904 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001905 return false;
1906 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001907
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001908 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001909 {
1910 D3D_FEATURE_LEVEL_11_0,
1911 D3D_FEATURE_LEVEL_10_1,
1912 D3D_FEATURE_LEVEL_10_0,
1913 };
1914
1915 ID3D11Device* dummyDevice;
1916 D3D_FEATURE_LEVEL dummyFeatureLevel;
1917 ID3D11DeviceContext* dummyContext;
1918
1919 HRESULT result = D3D11CreateDevice(NULL,
1920 D3D_DRIVER_TYPE_HARDWARE,
1921 NULL,
1922 #if defined(_DEBUG)
1923 D3D11_CREATE_DEVICE_DEBUG,
1924 #else
1925 0,
1926 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001927 featureLevels,
1928 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001929 D3D11_SDK_VERSION,
1930 &dummyDevice,
1931 &dummyFeatureLevel,
1932 &dummyContext);
1933
1934 if (!mDevice || FAILED(result))
1935 {
1936 return false;
1937 }
1938
1939 dummyContext->Release();
1940 dummyDevice->Release();
1941
1942 return true;
1943}
1944
1945void Renderer11::release()
1946{
1947 releaseDeviceResources();
1948
1949 if (mDxgiFactory)
1950 {
1951 mDxgiFactory->Release();
1952 mDxgiFactory = NULL;
1953 }
1954
1955 if (mDxgiAdapter)
1956 {
1957 mDxgiAdapter->Release();
1958 mDxgiAdapter = NULL;
1959 }
1960
1961 if (mDeviceContext)
1962 {
1963 mDeviceContext->ClearState();
1964 mDeviceContext->Flush();
1965 mDeviceContext->Release();
1966 mDeviceContext = NULL;
1967 }
1968
1969 if (mDevice)
1970 {
1971 mDevice->Release();
1972 mDevice = NULL;
1973 }
1974
1975 if (mD3d11Module)
1976 {
1977 FreeLibrary(mD3d11Module);
1978 mD3d11Module = NULL;
1979 }
1980
1981 if (mDxgiModule)
1982 {
1983 FreeLibrary(mDxgiModule);
1984 mDxgiModule = NULL;
1985 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001986}
1987
1988bool Renderer11::resetDevice()
1989{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001990 // recreate everything
1991 release();
1992 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001993
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001994 if (result != EGL_SUCCESS)
1995 {
1996 ERR("Could not reinitialize D3D11 device: %08X", result);
1997 return false;
1998 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001999
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002000 mDeviceLost = false;
2001
2002 return true;
2003}
2004
2005DWORD Renderer11::getAdapterVendor() const
2006{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00002007 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002008}
2009
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002010std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002011{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002012 std::ostringstream rendererString;
2013
2014 rendererString << mDescription;
2015 rendererString << " Direct3D11";
2016
2017 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
2018 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
2019
2020 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002021}
2022
2023GUID Renderer11::getAdapterIdentifier() const
2024{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00002025 // Use the adapter LUID as our adapter ID
2026 // This number is local to a machine is only guaranteed to be unique between restarts
2027 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
2028 GUID adapterId = {0};
2029 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
2030 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002031}
2032
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002033bool Renderer11::getBGRATextureSupport() const
2034{
2035 return mBGRATextureSupport;
2036}
2037
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002038bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002039{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002040 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002041}
2042
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002043bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002044{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002045 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002046}
2047
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002048bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002049{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002050 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002051}
2052
2053bool Renderer11::getDepthTextureSupport() const
2054{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00002055 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002056}
2057
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002058bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002059{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00002060 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002061}
2062
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002063bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002064{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002065 return mFloat32FilterSupport;
2066}
2067
2068bool Renderer11::getFloat32TextureRenderingSupport() const
2069{
2070 return mFloat32RenderSupport;
2071}
2072
2073bool Renderer11::getFloat16TextureSupport() const
2074{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00002075 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002076}
2077
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002078bool Renderer11::getFloat16TextureFilteringSupport() const
2079{
2080 return mFloat16FilterSupport;
2081}
2082
2083bool Renderer11::getFloat16TextureRenderingSupport() const
2084{
2085 return mFloat16RenderSupport;
2086}
2087
Geoff Langd42cf4e2013-06-05 16:09:17 -04002088bool Renderer11::getRGB565TextureSupport() const
2089{
2090 return false;
2091}
2092
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002093bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002094{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002095 return false;
2096}
2097
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002098bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002099{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002100 return false;
2101}
2102
2103bool Renderer11::getTextureFilterAnisotropySupport() const
2104{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002105 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002106}
2107
2108float Renderer11::getTextureMaxAnisotropy() const
2109{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002110 switch (mFeatureLevel)
2111 {
2112 case D3D_FEATURE_LEVEL_11_0:
2113 return D3D11_MAX_MAXANISOTROPY;
2114 case D3D_FEATURE_LEVEL_10_1:
2115 case D3D_FEATURE_LEVEL_10_0:
2116 return D3D10_MAX_MAXANISOTROPY;
2117 default: UNREACHABLE();
2118 return 0;
2119 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002120}
2121
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002122bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002123{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002124 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002125}
2126
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002127Range Renderer11::getViewportBounds() const
2128{
2129 switch (mFeatureLevel)
2130 {
2131 case D3D_FEATURE_LEVEL_11_0:
2132 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
2133 case D3D_FEATURE_LEVEL_10_1:
2134 case D3D_FEATURE_LEVEL_10_0:
2135 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
2136 default: UNREACHABLE();
2137 return Range(0, 0);
2138 }
2139}
2140
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002141unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002142{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002143 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2144 switch (mFeatureLevel)
2145 {
2146 case D3D_FEATURE_LEVEL_11_0:
2147 case D3D_FEATURE_LEVEL_10_1:
2148 case D3D_FEATURE_LEVEL_10_0:
2149 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
2150 default: UNREACHABLE();
2151 return 0;
2152 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002153}
2154
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002155unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
2156{
2157 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2158}
2159
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002160unsigned int Renderer11::getReservedVertexUniformVectors() const
2161{
Shannon Woods5ab33c82013-06-26 15:31:09 -04002162 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002163}
2164
2165unsigned int Renderer11::getReservedFragmentUniformVectors() const
2166{
Shannon Woods5ab33c82013-06-26 15:31:09 -04002167 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002168}
2169
2170unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002171{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002172 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2173 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2174 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002175}
2176
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002177unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002178{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002179 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2180 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2181 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002182}
2183
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002184unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002185{
2186 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00002187 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
2188 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002189 switch (mFeatureLevel)
2190 {
2191 case D3D_FEATURE_LEVEL_11_0:
2192 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2193 case D3D_FEATURE_LEVEL_10_1:
2194 case D3D_FEATURE_LEVEL_10_0:
2195 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2196 default: UNREACHABLE();
2197 return 0;
2198 }
2199}
2200
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002201unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2202{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002203 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2204 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2205
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002206 switch (mFeatureLevel)
2207 {
2208 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002209 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002210 case D3D_FEATURE_LEVEL_10_1:
2211 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002212 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002213 default: UNREACHABLE();
2214 return 0;
2215 }
2216}
2217
2218unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2219{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002220 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2221 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2222
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002223 switch (mFeatureLevel)
2224 {
2225 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002226 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002227 case D3D_FEATURE_LEVEL_10_1:
2228 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002229 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002230 default: UNREACHABLE();
2231 return 0;
2232 }
2233}
2234
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002235unsigned int Renderer11::getReservedVertexUniformBuffers() const
2236{
2237 // we reserve one buffer for the application uniforms, and one for driver uniforms
2238 return 2;
2239}
2240
2241unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2242{
2243 // we reserve one buffer for the application uniforms, and one for driver uniforms
2244 return 2;
2245}
2246
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002247unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2248{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002249 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2250 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2251
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002252 switch (mFeatureLevel)
2253 {
2254 case D3D_FEATURE_LEVEL_11_0:
2255 return D3D11_SO_BUFFER_SLOT_COUNT;
2256 case D3D_FEATURE_LEVEL_10_1:
2257 case D3D_FEATURE_LEVEL_10_0:
2258 return D3D10_SO_BUFFER_SLOT_COUNT;
2259 default: UNREACHABLE();
2260 return 0;
2261 }
2262}
2263
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002264unsigned int Renderer11::getMaxUniformBufferSize() const
2265{
2266 // Each component is a 4-element vector of 4-byte units (floats)
2267 const unsigned int bytesPerComponent = 4 * sizeof(float);
2268
2269 switch (mFeatureLevel)
2270 {
2271 case D3D_FEATURE_LEVEL_11_0:
2272 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2273 case D3D_FEATURE_LEVEL_10_1:
2274 case D3D_FEATURE_LEVEL_10_0:
2275 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2276 default: UNREACHABLE();
2277 return 0;
2278 }
2279}
2280
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002281bool Renderer11::getNonPower2TextureSupport() const
2282{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002283 switch (mFeatureLevel)
2284 {
2285 case D3D_FEATURE_LEVEL_11_0:
2286 case D3D_FEATURE_LEVEL_10_1:
2287 case D3D_FEATURE_LEVEL_10_0:
2288 return true;
2289 default: UNREACHABLE();
2290 return false;
2291 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002292}
2293
2294bool Renderer11::getOcclusionQuerySupport() const
2295{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002296 switch (mFeatureLevel)
2297 {
2298 case D3D_FEATURE_LEVEL_11_0:
2299 case D3D_FEATURE_LEVEL_10_1:
2300 case D3D_FEATURE_LEVEL_10_0:
2301 return true;
2302 default: UNREACHABLE();
2303 return false;
2304 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002305}
2306
2307bool Renderer11::getInstancingSupport() const
2308{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002309 switch (mFeatureLevel)
2310 {
2311 case D3D_FEATURE_LEVEL_11_0:
2312 case D3D_FEATURE_LEVEL_10_1:
2313 case D3D_FEATURE_LEVEL_10_0:
2314 return true;
2315 default: UNREACHABLE();
2316 return false;
2317 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002318}
2319
2320bool Renderer11::getShareHandleSupport() const
2321{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002322 // We only currently support share handles with BGRA surfaces, because
2323 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002324 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002325 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002326}
2327
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002328bool Renderer11::getDerivativeInstructionSupport() const
2329{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002330 switch (mFeatureLevel)
2331 {
2332 case D3D_FEATURE_LEVEL_11_0:
2333 case D3D_FEATURE_LEVEL_10_1:
2334 case D3D_FEATURE_LEVEL_10_0:
2335 return true;
2336 default: UNREACHABLE();
2337 return false;
2338 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002339}
2340
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002341bool Renderer11::getPostSubBufferSupport() const
2342{
2343 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2344 return false;
2345}
2346
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002347int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002348{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002349 switch (mFeatureLevel)
2350 {
2351 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002352 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002353 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2354 default: UNREACHABLE(); return 0;
2355 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002356}
2357
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002358int Renderer11::getMinorShaderModel() const
2359{
2360 switch (mFeatureLevel)
2361 {
2362 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2363 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2364 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2365 default: UNREACHABLE(); return 0;
2366 }
2367}
2368
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002369float Renderer11::getMaxPointSize() const
2370{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002371 // choose a reasonable maximum. we enforce this in the shader.
2372 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2373 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002374}
2375
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002376int Renderer11::getMaxViewportDimension() const
2377{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002378 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2379 // In our case return the maximum texture size, which is the maximum render buffer size.
2380 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2381 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2382
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002383 switch (mFeatureLevel)
2384 {
2385 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002386 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002387 case D3D_FEATURE_LEVEL_10_1:
2388 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002389 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002390 default: UNREACHABLE();
2391 return 0;
2392 }
2393}
2394
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002395int Renderer11::getMaxTextureWidth() const
2396{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002397 switch (mFeatureLevel)
2398 {
2399 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2400 case D3D_FEATURE_LEVEL_10_1:
2401 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2402 default: UNREACHABLE(); return 0;
2403 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002404}
2405
2406int Renderer11::getMaxTextureHeight() const
2407{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002408 switch (mFeatureLevel)
2409 {
2410 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2411 case D3D_FEATURE_LEVEL_10_1:
2412 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2413 default: UNREACHABLE(); return 0;
2414 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002415}
2416
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002417int Renderer11::getMaxTextureDepth() const
2418{
2419 switch (mFeatureLevel)
2420 {
2421 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2422 case D3D_FEATURE_LEVEL_10_1:
2423 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2424 default: UNREACHABLE(); return 0;
2425 }
2426}
2427
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002428int Renderer11::getMaxTextureArrayLayers() const
2429{
2430 switch (mFeatureLevel)
2431 {
2432 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2433 case D3D_FEATURE_LEVEL_10_1:
2434 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2435 default: UNREACHABLE(); return 0;
2436 }
2437}
2438
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002439bool Renderer11::get32BitIndexSupport() const
2440{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002441 switch (mFeatureLevel)
2442 {
2443 case D3D_FEATURE_LEVEL_11_0:
2444 case D3D_FEATURE_LEVEL_10_1:
2445 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2446 default: UNREACHABLE(); return false;
2447 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002448}
2449
2450int Renderer11::getMinSwapInterval() const
2451{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002452 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002453}
2454
2455int Renderer11::getMaxSwapInterval() const
2456{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002457 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002458}
2459
2460int Renderer11::getMaxSupportedSamples() const
2461{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002462 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002463}
2464
Geoff Lang0e120e32013-05-29 10:23:55 -04002465GLsizei Renderer11::getMaxSupportedFormatSamples(GLint internalFormat) const
2466{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002467 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002468 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2469 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2470}
2471
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002472GLsizei Renderer11::getNumSampleCounts(GLint internalFormat) const
2473{
2474 unsigned int numCounts = 0;
2475
2476 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
2477 if (!gl::IsIntegerFormat(internalFormat, getCurrentClientVersion()))
2478 {
2479 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2480 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2481
2482 if (iter != mMultisampleSupportMap.end())
2483 {
2484 const MultisampleSupportInfo& info = iter->second;
2485 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2486 {
2487 if (info.qualityLevels[i] > 0)
2488 {
2489 numCounts++;
2490 }
2491 }
2492 }
2493 }
2494
2495 return numCounts;
2496}
2497
2498void Renderer11::getSampleCounts(GLint internalFormat, GLsizei bufSize, GLint *params) const
2499{
2500 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
2501 if (gl::IsIntegerFormat(internalFormat, getCurrentClientVersion()))
2502 return;
2503
2504 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2505 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2506
2507 if (iter != mMultisampleSupportMap.end())
2508 {
2509 const MultisampleSupportInfo& info = iter->second;
2510 int bufPos = 0;
2511 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2512 {
2513 if (info.qualityLevels[i] > 0)
2514 {
2515 params[bufPos++] = i + 1;
2516 }
2517 }
2518 }
2519}
2520
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002521int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2522{
2523 if (requested == 0)
2524 {
2525 return 0;
2526 }
2527
2528 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2529 if (iter != mMultisampleSupportMap.end())
2530 {
2531 const MultisampleSupportInfo& info = iter->second;
2532 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2533 {
2534 if (info.qualityLevels[i] > 0)
2535 {
2536 return i + 1;
2537 }
2538 }
2539 }
2540
2541 return -1;
2542}
2543
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002544unsigned int Renderer11::getMaxRenderTargets() const
2545{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002546 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2547 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2548
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002549 switch (mFeatureLevel)
2550 {
2551 case D3D_FEATURE_LEVEL_11_0:
2552 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2553 case D3D_FEATURE_LEVEL_10_1:
2554 case D3D_FEATURE_LEVEL_10_0:
2555 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2556 default:
2557 UNREACHABLE();
2558 return 1;
2559 }
2560}
2561
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002562bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002563{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002564 if (source && dest)
2565 {
2566 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2567 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2568
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002569 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002570 return true;
2571 }
2572
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002573 return false;
2574}
2575
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002576bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002577{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002578 if (source && dest)
2579 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002580 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2581 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002582
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002583 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002584 return true;
2585 }
2586
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002587 return false;
2588}
2589
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002590bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2591{
2592 if (source && dest)
2593 {
2594 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2595 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2596
2597 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2598 return true;
2599 }
2600
2601 return false;
2602}
2603
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002604bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2605{
2606 if (source && dest)
2607 {
2608 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2609 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2610
2611 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2612 return true;
2613 }
2614
2615 return false;
2616}
2617
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002618bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002619 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002620{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002621 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002622 if (!colorbuffer)
2623 {
2624 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002625 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002626 }
2627
2628 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2629 if (!sourceRenderTarget)
2630 {
2631 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002632 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002633 }
2634
2635 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2636 if (!source)
2637 {
2638 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002639 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002640 }
2641
2642 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2643 if (!storage11)
2644 {
2645 source->Release();
2646 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002647 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002648 }
2649
2650 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2651 if (!destRenderTarget)
2652 {
2653 source->Release();
2654 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002655 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002656 }
2657
2658 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2659 if (!dest)
2660 {
2661 source->Release();
2662 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002663 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002664 }
2665
Geoff Langb86b9792013-06-04 16:32:05 -04002666 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2667 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002668
Geoff Langb86b9792013-06-04 16:32:05 -04002669 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2670 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002671
Geoff Langb86b9792013-06-04 16:32:05 -04002672 // Use nearest filtering because source and destination are the same size for the direct
2673 // copy
2674 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize,
2675 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002676
2677 source->Release();
2678 dest->Release();
2679
2680 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002681}
2682
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002683bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002684 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002685{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002686 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002687 if (!colorbuffer)
2688 {
2689 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002690 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002691 }
2692
2693 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2694 if (!sourceRenderTarget)
2695 {
2696 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002697 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002698 }
2699
2700 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2701 if (!source)
2702 {
2703 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002704 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002705 }
2706
2707 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2708 if (!storage11)
2709 {
2710 source->Release();
2711 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002712 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002713 }
2714
Nicolas Capensb13f8662013-06-04 13:30:19 -04002715 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002716 if (!destRenderTarget)
2717 {
2718 source->Release();
2719 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002720 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002721 }
2722
2723 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2724 if (!dest)
2725 {
2726 source->Release();
2727 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002728 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002729 }
2730
Geoff Langb86b9792013-06-04 16:32:05 -04002731 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2732 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002733
Geoff Langb86b9792013-06-04 16:32:05 -04002734 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2735 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002736
Geoff Langb86b9792013-06-04 16:32:05 -04002737 // Use nearest filtering because source and destination are the same size for the direct
2738 // copy
2739 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize,
2740 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002741
2742 source->Release();
2743 dest->Release();
2744
2745 return ret;
2746}
2747
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002748bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2749 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2750{
2751 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2752 if (!colorbuffer)
2753 {
2754 ERR("Failed to retrieve the color buffer from the frame buffer.");
2755 return gl::error(GL_OUT_OF_MEMORY, false);
2756 }
2757
2758 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2759 if (!sourceRenderTarget)
2760 {
2761 ERR("Failed to retrieve the render target from the frame buffer.");
2762 return gl::error(GL_OUT_OF_MEMORY, false);
2763 }
2764
2765 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2766 if (!source)
2767 {
2768 ERR("Failed to retrieve the render target view from the render target.");
2769 return gl::error(GL_OUT_OF_MEMORY, false);
2770 }
2771
2772 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2773 if (!storage11)
2774 {
2775 source->Release();
2776 ERR("Failed to retrieve the texture storage from the destination.");
2777 return gl::error(GL_OUT_OF_MEMORY, false);
2778 }
2779
2780 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2781 if (!destRenderTarget)
2782 {
2783 source->Release();
2784 ERR("Failed to retrieve the render target from the destination storage.");
2785 return gl::error(GL_OUT_OF_MEMORY, false);
2786 }
2787
2788 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2789 if (!dest)
2790 {
2791 source->Release();
2792 ERR("Failed to retrieve the render target view from the destination render target.");
2793 return gl::error(GL_OUT_OF_MEMORY, false);
2794 }
2795
Geoff Langb86b9792013-06-04 16:32:05 -04002796 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2797 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002798
Geoff Langb86b9792013-06-04 16:32:05 -04002799 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2800 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002801
Geoff Langb86b9792013-06-04 16:32:05 -04002802 // Use nearest filtering because source and destination are the same size for the direct
2803 // copy
2804 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize,
2805 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002806
2807 source->Release();
2808 dest->Release();
2809
2810 return ret;
2811}
2812
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002813bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2814 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2815{
2816 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2817 if (!colorbuffer)
2818 {
2819 ERR("Failed to retrieve the color buffer from the frame buffer.");
2820 return gl::error(GL_OUT_OF_MEMORY, false);
2821 }
2822
2823 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2824 if (!sourceRenderTarget)
2825 {
2826 ERR("Failed to retrieve the render target from the frame buffer.");
2827 return gl::error(GL_OUT_OF_MEMORY, false);
2828 }
2829
2830 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2831 if (!source)
2832 {
2833 ERR("Failed to retrieve the render target view from the render target.");
2834 return gl::error(GL_OUT_OF_MEMORY, false);
2835 }
2836
2837 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2838 if (!storage11)
2839 {
2840 source->Release();
2841 ERR("Failed to retrieve the texture storage from the destination.");
2842 return gl::error(GL_OUT_OF_MEMORY, false);
2843 }
2844
2845 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2846 if (!destRenderTarget)
2847 {
2848 source->Release();
2849 ERR("Failed to retrieve the render target from the destination storage.");
2850 return gl::error(GL_OUT_OF_MEMORY, false);
2851 }
2852
2853 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2854 if (!dest)
2855 {
2856 source->Release();
2857 ERR("Failed to retrieve the render target view from the destination render target.");
2858 return gl::error(GL_OUT_OF_MEMORY, false);
2859 }
2860
Geoff Langb86b9792013-06-04 16:32:05 -04002861 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2862 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002863
Geoff Langb86b9792013-06-04 16:32:05 -04002864 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2865 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002866
Geoff Langb86b9792013-06-04 16:32:05 -04002867 // Use nearest filtering because source and destination are the same size for the direct
2868 // copy
2869 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize,
2870 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002871
2872 source->Release();
2873 dest->Release();
2874
2875 return ret;
2876}
2877
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002878void Renderer11::unapplyRenderTargets()
2879{
2880 setOneTimeRenderTarget(NULL);
2881}
2882
2883void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2884{
2885 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2886
2887 rtvArray[0] = renderTargetView;
2888
2889 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2890
2891 // Do not preserve the serial for this one-time-use render target
2892 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2893 {
2894 mAppliedRenderTargetSerials[rtIndex] = 0;
2895 }
2896}
2897
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002898RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2899{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002900 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002901 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002902
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002903 if (depth)
2904 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002905 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002906 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
2907 swapChain11->getDepthStencilTexture(), NULL,
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002908 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002909 }
2910 else
2911 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002912 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002913 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002914 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002915 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002916 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002917 }
2918 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002919}
2920
Geoff Langa2d97f12013-06-11 11:44:02 -04002921RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002922{
Geoff Langa2d97f12013-06-11 11:44:02 -04002923 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002924 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002925}
2926
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002927ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002928{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002929 ShaderExecutable11 *executable = NULL;
2930
2931 switch (type)
2932 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002933 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002934 {
2935 ID3D11VertexShader *vshader = NULL;
2936 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2937 ASSERT(SUCCEEDED(result));
2938
2939 if (vshader)
2940 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002941 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002942 }
2943 }
2944 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002945 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002946 {
2947 ID3D11PixelShader *pshader = NULL;
2948 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2949 ASSERT(SUCCEEDED(result));
2950
2951 if (pshader)
2952 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002953 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002954 }
2955 }
2956 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002957 case rx::SHADER_GEOMETRY:
2958 {
2959 ID3D11GeometryShader *gshader = NULL;
2960 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2961 ASSERT(SUCCEEDED(result));
2962
2963 if (gshader)
2964 {
2965 executable = new ShaderExecutable11(function, length, gshader);
2966 }
2967 }
2968 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002969 default:
2970 UNREACHABLE();
2971 break;
2972 }
2973
2974 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002975}
2976
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002977ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002978{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002979 const char *profile = NULL;
2980
2981 switch (type)
2982 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002983 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002984 profile = "vs_4_0";
2985 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002986 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002987 profile = "ps_4_0";
2988 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002989 case rx::SHADER_GEOMETRY:
2990 profile = "gs_4_0";
2991 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002992 default:
2993 UNREACHABLE();
2994 return NULL;
2995 }
2996
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00002997 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002998 if (!binary)
2999 return NULL;
3000
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003001 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003002 binary->Release();
3003
3004 return executable;
3005}
3006
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00003007VertexBuffer *Renderer11::createVertexBuffer()
3008{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00003009 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00003010}
3011
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00003012IndexBuffer *Renderer11::createIndexBuffer()
3013{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00003014 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003015}
3016
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00003017BufferStorage *Renderer11::createBufferStorage()
3018{
3019 return new BufferStorage11(this);
3020}
3021
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003022QueryImpl *Renderer11::createQuery(GLenum type)
3023{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00003024 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003025}
3026
3027FenceImpl *Renderer11::createFence()
3028{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00003029 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00003030}
3031
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003032bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003033{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003034 ASSERT(colorbuffer != NULL);
3035
3036 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
3037 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003038 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003039 *subresourceIndex = renderTarget->getSubresourceIndex();
3040
3041 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
3042 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003043 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003044 ID3D11Resource *textureResource = NULL;
3045 colorBufferRTV->GetResource(&textureResource);
3046 colorBufferRTV->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003047
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003048 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003049 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003050 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
3051 textureResource->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003052
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003053 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003054 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003055 return true;
3056 }
3057 else
3058 {
3059 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
3060 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003061 }
3062 }
3063 }
3064 }
3065
3066 return false;
3067}
3068
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003069bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang685806d2013-06-12 11:16:36 -04003070 bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003071{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003072 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003073 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003074 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003075
3076 if (!readBuffer)
3077 {
3078 ERR("Failed to retrieve the read buffer from the read framebuffer.");
3079 return gl::error(GL_OUT_OF_MEMORY, false);
3080 }
3081
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003082 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003083
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003084 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003085 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003086 if (drawTarget->isEnabledColorAttachment(colorAttachment))
3087 {
3088 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
3089
3090 if (!drawBuffer)
3091 {
3092 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
3093 return gl::error(GL_OUT_OF_MEMORY, false);
3094 }
3095
3096 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
3097
Geoff Lang685806d2013-06-12 11:16:36 -04003098 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter,
3099 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003100 {
3101 return false;
3102 }
3103 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003104 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003105 }
3106
Geoff Lang685806d2013-06-12 11:16:36 -04003107 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003108 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003109 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
3110 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
3111
3112 if (!readBuffer)
3113 {
3114 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
3115 return gl::error(GL_OUT_OF_MEMORY, false);
3116 }
3117
3118 if (!drawBuffer)
3119 {
3120 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
3121 return gl::error(GL_OUT_OF_MEMORY, false);
3122 }
3123
3124 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3125 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3126
Geoff Lang685806d2013-06-12 11:16:36 -04003127 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter,
3128 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003129 {
3130 return false;
3131 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003132 }
3133
3134 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003135}
3136
3137void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3138 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3139{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003140 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003141 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003142
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003143 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3144
3145 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003146 {
3147 gl::Rectangle area;
3148 area.x = x;
3149 area.y = y;
3150 area.width = width;
3151 area.height = height;
3152
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003153 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3154 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003155
3156 colorBufferTexture->Release();
3157 colorBufferTexture = NULL;
3158 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003159}
3160
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003161Image *Renderer11::createImage()
3162{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003163 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003164}
3165
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003166void Renderer11::generateMipmap(Image *dest, Image *src)
3167{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003168 Image11 *dest11 = Image11::makeImage11(dest);
3169 Image11 *src11 = Image11::makeImage11(src);
3170 Image11::generateMipmap(dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003171}
3172
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003173TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3174{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003175 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3176 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003177}
3178
3179TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3180{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003181 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003182}
3183
3184TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3185{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003186 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003187}
3188
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003189TextureStorage *Renderer11::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3190{
3191 return new TextureStorage11_3D(this, levels, internalformat, usage, width, height, depth);
3192}
3193
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003194TextureStorage *Renderer11::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3195{
3196 return new TextureStorage11_2DArray(this, levels, internalformat, usage, width, height, depth);
3197}
3198
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003199void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3200 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3201 GLint packAlignment, void *pixels)
3202{
3203 D3D11_TEXTURE2D_DESC textureDesc;
3204 texture->GetDesc(&textureDesc);
3205
3206 D3D11_TEXTURE2D_DESC stagingDesc;
3207 stagingDesc.Width = area.width;
3208 stagingDesc.Height = area.height;
3209 stagingDesc.MipLevels = 1;
3210 stagingDesc.ArraySize = 1;
3211 stagingDesc.Format = textureDesc.Format;
3212 stagingDesc.SampleDesc.Count = 1;
3213 stagingDesc.SampleDesc.Quality = 0;
3214 stagingDesc.Usage = D3D11_USAGE_STAGING;
3215 stagingDesc.BindFlags = 0;
3216 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3217 stagingDesc.MiscFlags = 0;
3218
3219 ID3D11Texture2D* stagingTex = NULL;
3220 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3221 if (FAILED(result))
3222 {
3223 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3224 return;
3225 }
3226
3227 ID3D11Texture2D* srcTex = NULL;
3228 if (textureDesc.SampleDesc.Count > 1)
3229 {
3230 D3D11_TEXTURE2D_DESC resolveDesc;
3231 resolveDesc.Width = textureDesc.Width;
3232 resolveDesc.Height = textureDesc.Height;
3233 resolveDesc.MipLevels = 1;
3234 resolveDesc.ArraySize = 1;
3235 resolveDesc.Format = textureDesc.Format;
3236 resolveDesc.SampleDesc.Count = 1;
3237 resolveDesc.SampleDesc.Quality = 0;
3238 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3239 resolveDesc.BindFlags = 0;
3240 resolveDesc.CPUAccessFlags = 0;
3241 resolveDesc.MiscFlags = 0;
3242
3243 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3244 if (FAILED(result))
3245 {
3246 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
3247 stagingTex->Release();
3248 return;
3249 }
3250
3251 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3252 subResource = 0;
3253 }
3254 else
3255 {
3256 srcTex = texture;
3257 srcTex->AddRef();
3258 }
3259
3260 D3D11_BOX srcBox;
3261 srcBox.left = area.x;
3262 srcBox.right = area.x + area.width;
3263 srcBox.top = area.y;
3264 srcBox.bottom = area.y + area.height;
3265 srcBox.front = 0;
3266 srcBox.back = 1;
3267
3268 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3269
3270 srcTex->Release();
3271 srcTex = NULL;
3272
3273 D3D11_MAPPED_SUBRESOURCE mapping;
3274 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3275
3276 unsigned char *source;
3277 int inputPitch;
3278 if (packReverseRowOrder)
3279 {
3280 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3281 inputPitch = -static_cast<int>(mapping.RowPitch);
3282 }
3283 else
3284 {
3285 source = static_cast<unsigned char*>(mapping.pData);
3286 inputPitch = static_cast<int>(mapping.RowPitch);
3287 }
3288
Geoff Lang697ad3e2013-06-04 10:11:28 -04003289 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003290
Geoff Lang697ad3e2013-06-04 10:11:28 -04003291 GLint sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format);
3292 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3293 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3294
3295 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3296
3297 if (sourceFormat == format && sourceType == type)
3298 {
3299 // Direct copy possible
3300 unsigned char *dest = static_cast<unsigned char*>(pixels);
3301 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003302 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003303 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003304 }
3305 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003306 else
3307 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003308 GLint destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
3309 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3310
3311 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type, getCurrentClientVersion());
3312 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003313 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003314 // Fast copy is possible through some special function
3315 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003316 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003317 for (int x = 0; x < area.width; x++)
3318 {
3319 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3320 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3321
3322 fastCopyFunc(src, dest);
3323 }
3324 }
3325 }
3326 else
3327 {
3328 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format);
3329 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3330
3331 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3332 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3333 sizeof(temp) >= sizeof(gl::ColorUI) &&
3334 sizeof(temp) >= sizeof(gl::ColorI));
3335
3336 for (int y = 0; y < area.height; y++)
3337 {
3338 for (int x = 0; x < area.width; x++)
3339 {
3340 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3341 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3342
3343 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3344 // will not allow the copy otherwise.
3345 readFunc(src, temp);
3346 writeFunc(temp, dest);
3347 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003348 }
3349 }
3350 }
3351
3352 mDeviceContext->Unmap(stagingTex, 0);
3353
3354 stagingTex->Release();
3355 stagingTex = NULL;
3356}
3357
Geoff Lang758d5b22013-06-11 11:42:50 -04003358bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang685806d2013-06-12 11:16:36 -04003359 RenderTarget *drawRenderTarget, GLenum filter, bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003360{
Geoff Lang975af372013-06-12 11:19:22 -04003361 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3362 // it should never be the case that both color and depth/stencil need to be blitted at
3363 // at the same time.
3364 ASSERT(colorBlit != (depthBlit || stencilBlit));
3365
Geoff Langc1f51be2013-06-11 11:49:14 -04003366 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003367
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003368 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3369 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003370 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003371 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003372 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003373 }
3374
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003375 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003376 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003377 unsigned int readSubresource = 0;
3378 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003379 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003380 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3381 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
3382 unresolvedResource->Release();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003383
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003384 if (unresolvedTexture)
3385 {
3386 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3387 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003388
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003389 unresolvedTexture->Release();
Geoff Langc1f51be2013-06-11 11:49:14 -04003390
3391 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3392 if (FAILED(result))
3393 {
3394 readTexture->Release();
3395 return gl::error(GL_OUT_OF_MEMORY, false);
3396 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003397 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003398 }
3399 else
3400 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003401 readTexture = readRenderTarget11->getTexture();
3402 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003403 readSRV = readRenderTarget11->getShaderResourceView();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003404 }
3405
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003406 if (!readTexture)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003407 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003408 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003409 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003410 }
3411
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003412 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003413 if (!drawRenderTarget)
3414 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003415 readTexture->Release();
3416 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003417 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003418 }
3419
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003420 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003421 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003422 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3423 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003424
Geoff Langc1f51be2013-06-11 11:49:14 -04003425 bool wholeBufferCopy = readRect.x == 0 && readRect.width == readRenderTarget11->getWidth() &&
3426 readRect.y == 0 && readRect.height == readRenderTarget11->getHeight() &&
3427 drawRect.x == 0 && drawRect.width == drawRenderTarget->getWidth() &&
3428 drawRect.y == 0 && drawRect.height == drawRenderTarget->getHeight();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003429
Geoff Langc1f51be2013-06-11 11:49:14 -04003430 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003431
Geoff Langc1f51be2013-06-11 11:49:14 -04003432 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang685806d2013-06-12 11:16:36 -04003433 !stretchRequired && (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003434 {
3435 D3D11_BOX readBox;
3436 readBox.left = readRect.x;
3437 readBox.right = readRect.x + readRect.width;
3438 readBox.top = readRect.y;
3439 readBox.bottom = readRect.y + readRect.height;
3440 readBox.front = 0;
3441 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003442
Geoff Langc1f51be2013-06-11 11:49:14 -04003443 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3444 // We also require complete framebuffer copies for depth-stencil blit.
3445 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003446
Geoff Langc1f51be2013-06-11 11:49:14 -04003447 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, drawRect.x, drawRect.y, 0,
3448 readTexture, readSubresource, pSrcBox);
3449 result = true;
3450 }
3451 else
3452 {
3453 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
3454 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3455
3456 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
3457 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3458
Geoff Lang975af372013-06-12 11:19:22 -04003459 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003460 {
Geoff Lang975af372013-06-12 11:19:22 -04003461 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
3462 drawTexture, drawSubresource, drawArea, drawSize);
3463 }
3464 else if (depthBlit)
3465 {
3466 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize);
3467 }
3468 else if (stencilBlit)
3469 {
3470 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
3471 drawTexture, drawSubresource, drawArea, drawSize);
Geoff Langc1f51be2013-06-11 11:49:14 -04003472 }
3473 else
3474 {
Geoff Lang685806d2013-06-12 11:16:36 -04003475 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Langc1f51be2013-06-11 11:49:14 -04003476 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize, format, filter);
3477 }
3478 }
3479
3480 SafeRelease(readTexture);
3481 SafeRelease(readSRV);
3482 SafeRelease(drawTexture);
3483 SafeRelease(drawRTV);
3484 SafeRelease(drawDSV);
3485
3486 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003487}
3488
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003489ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3490{
3491 D3D11_TEXTURE2D_DESC textureDesc;
3492 source->GetDesc(&textureDesc);
3493
3494 if (textureDesc.SampleDesc.Count > 1)
3495 {
3496 D3D11_TEXTURE2D_DESC resolveDesc;
3497 resolveDesc.Width = textureDesc.Width;
3498 resolveDesc.Height = textureDesc.Height;
3499 resolveDesc.MipLevels = 1;
3500 resolveDesc.ArraySize = 1;
3501 resolveDesc.Format = textureDesc.Format;
3502 resolveDesc.SampleDesc.Count = 1;
3503 resolveDesc.SampleDesc.Quality = 0;
3504 resolveDesc.Usage = textureDesc.Usage;
3505 resolveDesc.BindFlags = textureDesc.BindFlags;
3506 resolveDesc.CPUAccessFlags = 0;
3507 resolveDesc.MiscFlags = 0;
3508
3509 ID3D11Texture2D *resolveTexture = NULL;
3510 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3511 if (FAILED(result))
3512 {
3513 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3514 return NULL;
3515 }
3516
3517 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3518 return resolveTexture;
3519 }
3520 else
3521 {
3522 source->AddRef();
3523 return source;
3524 }
3525}
3526
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003527bool Renderer11::getLUID(LUID *adapterLuid) const
3528{
3529 adapterLuid->HighPart = 0;
3530 adapterLuid->LowPart = 0;
3531
3532 if (!mDxgiAdapter)
3533 {
3534 return false;
3535 }
3536
3537 DXGI_ADAPTER_DESC adapterDesc;
3538 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3539 {
3540 return false;
3541 }
3542
3543 *adapterLuid = adapterDesc.AdapterLuid;
3544 return true;
3545}
3546
Geoff Lang61e49a52013-05-29 10:22:58 -04003547Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3548{
3549 MultisampleSupportInfo supportInfo = { 0 };
3550
3551 UINT formatSupport;
3552 HRESULT result;
3553
3554 result = mDevice->CheckFormatSupport(format, &formatSupport);
3555 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3556 {
3557 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3558 {
3559 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3560 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3561 {
3562 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3563 }
3564 else
3565 {
3566 supportInfo.qualityLevels[i - 1] = 0;
3567 }
3568 }
3569 }
3570
3571 return supportInfo;
3572}
3573
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003574}