blob: 57a84f60fb4681fff7ebd5a7bfb23ec3fe89d4e1 [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 {
Jamie Madilld6cb2442013-07-10 15:13:38 -0400437 // FIXME: parse types from context version
438 ASSERT(d3d11_gl::GetInternalFormat(renderTargetFormat, 2) == d3d11_gl::GetInternalFormat(renderTargetFormat, 3));
439 ASSERT(d3d11_gl::GetInternalFormat(depthStencilFormat, 2) == d3d11_gl::GetInternalFormat(depthStencilFormat, 3));
440
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000441 ConfigDesc newConfig;
Jamie Madilld6cb2442013-07-10 15:13:38 -0400442 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat, getCurrentClientVersion());
443 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat, getCurrentClientVersion());
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000444 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
445 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000446 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000447
448 (*configDescList)[numConfigs++] = newConfig;
449 }
450 }
451 }
452 }
453
454 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000455}
456
457void Renderer11::deleteConfigs(ConfigDesc *configDescList)
458{
459 delete [] (configDescList);
460}
461
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000462void Renderer11::sync(bool block)
463{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000464 if (block)
465 {
466 HRESULT result;
467
468 if (!mSyncQuery)
469 {
470 D3D11_QUERY_DESC queryDesc;
471 queryDesc.Query = D3D11_QUERY_EVENT;
472 queryDesc.MiscFlags = 0;
473
474 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
475 ASSERT(SUCCEEDED(result));
476 }
477
478 mDeviceContext->End(mSyncQuery);
479 mDeviceContext->Flush();
480
481 do
482 {
483 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
484
485 // Keep polling, but allow other threads to do something useful first
486 Sleep(0);
487
488 if (testDeviceLost(true))
489 {
490 return;
491 }
492 }
493 while (result == S_FALSE);
494 }
495 else
496 {
497 mDeviceContext->Flush();
498 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000499}
500
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000501SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
502{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000503 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000504}
505
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000506void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
507{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000508 if (type == gl::SAMPLER_PIXEL)
509 {
510 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
511 {
512 ERR("Pixel shader sampler index %i is not valid.", index);
513 return;
514 }
515
516 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
517 {
518 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
519
520 if (!dxSamplerState)
521 {
522 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
523 "sampler state for pixel shaders at slot %i.", index);
524 }
525
526 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
527
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000528 mCurPixelSamplerStates[index] = samplerState;
529 }
530
531 mForceSetPixelSamplerStates[index] = false;
532 }
533 else if (type == gl::SAMPLER_VERTEX)
534 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000535 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000536 {
537 ERR("Vertex shader sampler index %i is not valid.", index);
538 return;
539 }
540
541 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
542 {
543 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
544
545 if (!dxSamplerState)
546 {
547 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
548 "sampler state for vertex shaders at slot %i.", index);
549 }
550
551 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
552
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000553 mCurVertexSamplerStates[index] = samplerState;
554 }
555
556 mForceSetVertexSamplerStates[index] = false;
557 }
558 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000559}
560
561void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
562{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000563 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000564 unsigned int serial = 0;
565 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000566
567 if (texture)
568 {
569 TextureStorageInterface *texStorage = texture->getNativeTexture();
570 if (texStorage)
571 {
572 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
573 textureSRV = storage11->getSRV();
574 }
575
576 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
577 // missing the shader resource view
578 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000579
580 serial = texture->getTextureSerial();
581 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000582 }
583
584 if (type == gl::SAMPLER_PIXEL)
585 {
586 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
587 {
588 ERR("Pixel shader sampler index %i is not valid.", index);
589 return;
590 }
591
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000592 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
593 {
594 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
595 }
596
597 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000598 }
599 else if (type == gl::SAMPLER_VERTEX)
600 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000601 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000602 {
603 ERR("Vertex shader sampler index %i is not valid.", index);
604 return;
605 }
606
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000607 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
608 {
609 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
610 }
611
612 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000613 }
614 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000615}
616
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000617bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
618{
619 // convert buffers to ID3D11Buffer*
620 ID3D11Buffer *vertexConstantBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = { NULL };
621 ID3D11Buffer *pixelConstantBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = { NULL };
622
623 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
624 {
625 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
626 if (uniformBuffer)
627 {
628 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
629 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(GL_UNIFORM_BUFFER);
630
631 if (!constantBuffer)
632 {
633 return false;
634 }
635
636 vertexConstantBuffers[uniformBufferIndex] = constantBuffer;
637 }
638 }
639
640 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
641 {
642 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
643 if (uniformBuffer)
644 {
645 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
646 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(GL_UNIFORM_BUFFER);
647
648 if (!constantBuffer)
649 {
650 return false;
651 }
652
653 pixelConstantBuffers[uniformBufferIndex] = constantBuffer;
654 }
655 }
656
657 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers(), getMaxVertexShaderUniformBuffers(), vertexConstantBuffers);
658 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers(), getMaxFragmentShaderUniformBuffers(), pixelConstantBuffers);
659
660 return true;
661}
662
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000663void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000664{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000665 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000666 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000667 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
668 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000669 if (!dxRasterState)
670 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000671 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000672 "rasterizer state.");
673 }
674
675 mDeviceContext->RSSetState(dxRasterState);
676
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000677 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000678 }
679
680 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000681}
682
Geoff Lang2a64ee42013-05-31 11:22:40 -0400683void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000684 unsigned int sampleMask)
685{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000686 if (mForceSetBlendState ||
687 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400688 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000689 sampleMask != mCurSampleMask)
690 {
691 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
692 if (!dxBlendState)
693 {
694 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
695 "blend state.");
696 }
697
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000698 float blendColors[4] = {0.0f};
699 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
700 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
701 {
702 blendColors[0] = blendColor.red;
703 blendColors[1] = blendColor.green;
704 blendColors[2] = blendColor.blue;
705 blendColors[3] = blendColor.alpha;
706 }
707 else
708 {
709 blendColors[0] = blendColor.alpha;
710 blendColors[1] = blendColor.alpha;
711 blendColors[2] = blendColor.alpha;
712 blendColors[3] = blendColor.alpha;
713 }
714
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000715 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
716
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000717 mCurBlendState = blendState;
718 mCurBlendColor = blendColor;
719 mCurSampleMask = sampleMask;
720 }
721
722 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000723}
724
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000725void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000726 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000727{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000728 if (mForceSetDepthStencilState ||
729 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
730 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
731 {
732 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
733 stencilRef != stencilBackRef ||
734 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
735 {
736 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
737 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000738 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000739 }
740
741 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
742 if (!dxDepthStencilState)
743 {
744 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
745 "setting the default depth stencil state.");
746 }
747
748 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
749
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000750 mCurDepthStencilState = depthStencilState;
751 mCurStencilRef = stencilRef;
752 mCurStencilBackRef = stencilBackRef;
753 }
754
755 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000756}
757
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000758void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000759{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000760 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
761 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000762 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000763 if (enabled)
764 {
765 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000766 rect.left = std::max(0, scissor.x);
767 rect.top = std::max(0, scissor.y);
768 rect.right = scissor.x + std::max(0, scissor.width);
769 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000770
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000771 mDeviceContext->RSSetScissorRects(1, &rect);
772 }
773
774 if (enabled != mScissorEnabled)
775 {
776 mForceSetRasterState = true;
777 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000778
779 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000780 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000781 }
782
783 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000784}
785
daniel@transgaming.com12985182012-12-20 20:56:31 +0000786bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000787 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000788{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000789 gl::Rectangle actualViewport = viewport;
790 float actualZNear = gl::clamp01(zNear);
791 float actualZFar = gl::clamp01(zFar);
792 if (ignoreViewport)
793 {
794 actualViewport.x = 0;
795 actualViewport.y = 0;
796 actualViewport.width = mRenderTargetDesc.width;
797 actualViewport.height = mRenderTargetDesc.height;
798 actualZNear = 0.0f;
799 actualZFar = 1.0f;
800 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000801
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000802 // Get D3D viewport bounds, which depends on the feature level
803 const Range& viewportBounds = getViewportBounds();
804
805 // 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 +0000806 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000807 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
808 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
809 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
810 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
811 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
812 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000813 dxViewport.MinDepth = actualZNear;
814 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000815
816 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
817 {
818 return false; // Nothing to render
819 }
820
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000821 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
822 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000823
daniel@transgaming.com53670042012-11-28 20:55:51 +0000824 if (viewportChanged)
825 {
826 mDeviceContext->RSSetViewports(1, &dxViewport);
827
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000828 mCurViewport = actualViewport;
829 mCurNear = actualZNear;
830 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000831
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000832 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
833 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
834 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
835 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000836
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000837 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
838 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000839
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000840 mVertexConstants.depthRange[0] = actualZNear;
841 mVertexConstants.depthRange[1] = actualZFar;
842 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000843
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000844 mPixelConstants.depthRange[0] = actualZNear;
845 mPixelConstants.depthRange[1] = actualZFar;
846 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000847 }
848
849 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000850 return true;
851}
852
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000853bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
854{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000855 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000856
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000857 switch (mode)
858 {
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000859 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break;
860 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break;
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000861 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000862 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
863 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
864 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000865 // emulate fans via rewriting index buffer
Shannon Woods5ab33c82013-06-26 15:31:09 -0400866 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000867 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000868 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000869 }
870
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000871 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000872
873 return count > 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000874}
875
876bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000877{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000878 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000879 // Also extract the render target dimensions and view
880 unsigned int renderTargetWidth = 0;
881 unsigned int renderTargetHeight = 0;
882 GLenum renderTargetFormat = 0;
883 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
884 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
885 bool missingColorRenderTarget = true;
886
887 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000888 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000889 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
890
891 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000892 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000893 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
894 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
895
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000896 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000897
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000898 if (!colorbuffer)
899 {
900 ERR("render target pointer unexpectedly null.");
901 return false;
902 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000903
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000904 // check for zero-sized default framebuffer, which is a special case.
905 // in this case we do not wish to modify any state and just silently return false.
906 // this will not report any gl error but will cause the calling method to return.
907 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
908 {
909 return false;
910 }
911
912 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
913
914 // Extract the render target dimensions and view
915 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
916 if (!renderTarget)
917 {
918 ERR("render target pointer unexpectedly null.");
919 return false;
920 }
921
922 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
923 if (!framebufferRTVs[colorAttachment])
924 {
925 ERR("render target view pointer unexpectedly null.");
926 return false;
927 }
928
929 if (missingColorRenderTarget)
930 {
931 renderTargetWidth = colorbuffer->getWidth();
932 renderTargetHeight = colorbuffer->getHeight();
933 renderTargetFormat = colorbuffer->getActualFormat();
934 missingColorRenderTarget = false;
935 }
936 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000937 }
938
939 // Get the depth stencil render buffer and serials
940 gl::Renderbuffer *depthStencil = NULL;
941 unsigned int depthbufferSerial = 0;
942 unsigned int stencilbufferSerial = 0;
943 if (framebuffer->getDepthbufferType() != GL_NONE)
944 {
945 depthStencil = framebuffer->getDepthbuffer();
946 if (!depthStencil)
947 {
948 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000949 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000950 return false;
951 }
952
953 depthbufferSerial = depthStencil->getSerial();
954 }
955 else if (framebuffer->getStencilbufferType() != GL_NONE)
956 {
957 depthStencil = framebuffer->getStencilbuffer();
958 if (!depthStencil)
959 {
960 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000961 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000962 return false;
963 }
964
965 stencilbufferSerial = depthStencil->getSerial();
966 }
967
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000968 // Extract the depth stencil sizes and view
969 unsigned int depthSize = 0;
970 unsigned int stencilSize = 0;
971 ID3D11DepthStencilView* framebufferDSV = NULL;
972 if (depthStencil)
973 {
974 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
975 if (!depthStencilRenderTarget)
976 {
977 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000978 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000979 return false;
980 }
981
982 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
983 if (!framebufferDSV)
984 {
985 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000986 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000987 return false;
988 }
989
990 // If there is no render buffer, the width, height and format values come from
991 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000992 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000993 {
994 renderTargetWidth = depthStencil->getWidth();
995 renderTargetHeight = depthStencil->getHeight();
996 renderTargetFormat = depthStencil->getActualFormat();
997 }
998
999 depthSize = depthStencil->getDepthSize();
1000 stencilSize = depthStencil->getStencilSize();
1001 }
1002
1003 // Apply the render target and depth stencil
1004 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001005 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001006 depthbufferSerial != mAppliedDepthbufferSerial ||
1007 stencilbufferSerial != mAppliedStencilbufferSerial)
1008 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001009 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001010
1011 mRenderTargetDesc.width = renderTargetWidth;
1012 mRenderTargetDesc.height = renderTargetHeight;
1013 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001014 mForceSetViewport = true;
1015 mForceSetScissor = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001016
1017 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1018 {
1019 mCurDepthSize = depthSize;
1020 mForceSetRasterState = true;
1021 }
1022
1023 mCurStencilSize = stencilSize;
1024
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001025 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1026 {
1027 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
1028 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001029 mAppliedDepthbufferSerial = depthbufferSerial;
1030 mAppliedStencilbufferSerial = stencilbufferSerial;
1031 mRenderTargetDescInitialized = true;
1032 mDepthStencilInitialized = true;
1033 }
1034
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001035 SafeRelease(framebufferRTVs);
1036 SafeRelease(framebufferDSV);
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001037
1038 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001039}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001040
Jamie Madill57a89722013-07-02 11:57:03 -04001041GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001042 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001043{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001044 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001045 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001046 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001047 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001048 return err;
1049 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001050
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001051 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001052}
1053
daniel@transgaming.com31240482012-11-28 21:06:41 +00001054GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001055{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001056 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001057
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001058 if (err == GL_NO_ERROR)
1059 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001060 if (indexInfo->storage)
1061 {
1062 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1063 {
1064 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1065 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1066
shannonwoods@chromium.org675526e2013-05-30 00:04:49 +00001067 mDeviceContext->IASetIndexBuffer(storage->getBuffer(GL_ELEMENT_ARRAY_BUFFER), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001068
1069 mAppliedIBSerial = 0;
1070 mAppliedStorageIBSerial = storage->getSerial();
1071 mAppliedIBOffset = indexInfo->startOffset;
1072 }
1073 }
1074 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001075 {
1076 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1077
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001078 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001079
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001080 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001081 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001082 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001083 }
1084 }
1085
1086 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001087}
1088
1089void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1090{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001091 if (mode == GL_LINE_LOOP)
1092 {
1093 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1094 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001095 else if (mode == GL_TRIANGLE_FAN)
1096 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001097 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001098 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001099 else if (instances > 0)
1100 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001101 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001102 }
1103 else
1104 {
1105 mDeviceContext->Draw(count, 0);
1106 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001107}
1108
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001109void 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 +00001110{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001111 if (mode == GL_LINE_LOOP)
1112 {
1113 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1114 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001115 else if (mode == GL_TRIANGLE_FAN)
1116 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001117 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1118 }
1119 else if (instances > 0)
1120 {
1121 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001122 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001123 else
1124 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001125 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001126 }
1127}
1128
1129void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1130{
1131 // Get the raw indices for an indexed draw
1132 if (type != GL_NONE && elementArrayBuffer)
1133 {
1134 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001135 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001136 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001137 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001138 }
1139
1140 if (!mLineLoopIB)
1141 {
1142 mLineLoopIB = new StreamingIndexBufferInterface(this);
1143 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1144 {
1145 delete mLineLoopIB;
1146 mLineLoopIB = NULL;
1147
1148 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001149 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001150 }
1151 }
1152
Geoff Langeadfd572013-07-09 15:55:07 -04001153 if (static_cast<unsigned int>(count + 1) > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
1154 {
1155 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1156 return gl::error(GL_OUT_OF_MEMORY);
1157 }
1158
1159 const unsigned int spaceNeeded = (count + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001160 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1161 {
1162 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001163 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001164 }
1165
1166 void* mappedMemory = NULL;
1167 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1168 if (offset == -1 || mappedMemory == NULL)
1169 {
1170 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001171 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001172 }
1173
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001174 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001175 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001176
1177 switch (type)
1178 {
1179 case GL_NONE: // Non-indexed draw
1180 for (int i = 0; i < count; i++)
1181 {
1182 data[i] = i;
1183 }
1184 data[count] = 0;
1185 break;
1186 case GL_UNSIGNED_BYTE:
1187 for (int i = 0; i < count; i++)
1188 {
1189 data[i] = static_cast<const GLubyte*>(indices)[i];
1190 }
1191 data[count] = static_cast<const GLubyte*>(indices)[0];
1192 break;
1193 case GL_UNSIGNED_SHORT:
1194 for (int i = 0; i < count; i++)
1195 {
1196 data[i] = static_cast<const GLushort*>(indices)[i];
1197 }
1198 data[count] = static_cast<const GLushort*>(indices)[0];
1199 break;
1200 case GL_UNSIGNED_INT:
1201 for (int i = 0; i < count; i++)
1202 {
1203 data[i] = static_cast<const GLuint*>(indices)[i];
1204 }
1205 data[count] = static_cast<const GLuint*>(indices)[0];
1206 break;
1207 default: UNREACHABLE();
1208 }
1209
1210 if (!mLineLoopIB->unmapBuffer())
1211 {
1212 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001213 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001214 }
1215
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001216 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001217 {
1218 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1219
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001220 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001221 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001222 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001223 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001224 }
1225
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001226 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001227}
1228
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001229void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001230{
1231 // Get the raw indices for an indexed draw
1232 if (type != GL_NONE && elementArrayBuffer)
1233 {
1234 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001235 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001236 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001237 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001238 }
1239
1240 if (!mTriangleFanIB)
1241 {
1242 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1243 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1244 {
1245 delete mTriangleFanIB;
1246 mTriangleFanIB = NULL;
1247
1248 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001249 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001250 }
1251 }
1252
Geoff Langeadfd572013-07-09 15:55:07 -04001253 const unsigned int numTris = count - 2;
1254
1255 if (numTris * 3 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
1256 {
1257 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1258 return gl::error(GL_OUT_OF_MEMORY);
1259 }
1260
1261 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001262 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1263 {
1264 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001265 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001266 }
1267
1268 void* mappedMemory = NULL;
1269 int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory);
1270 if (offset == -1 || mappedMemory == NULL)
1271 {
1272 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001273 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001274 }
1275
1276 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1277 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
1278
1279 switch (type)
1280 {
1281 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001282 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001283 {
1284 data[i*3 + 0] = 0;
1285 data[i*3 + 1] = i + 1;
1286 data[i*3 + 2] = i + 2;
1287 }
1288 break;
1289 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001290 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001291 {
1292 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1293 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1294 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1295 }
1296 break;
1297 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001298 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001299 {
1300 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1301 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1302 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1303 }
1304 break;
1305 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001306 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001307 {
1308 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1309 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1310 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1311 }
1312 break;
1313 default: UNREACHABLE();
1314 }
1315
1316 if (!mTriangleFanIB->unmapBuffer())
1317 {
1318 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001319 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001320 }
1321
1322 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1323 {
1324 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1325
1326 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1327 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001328 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001329 mAppliedIBOffset = indexBufferOffset;
1330 }
1331
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001332 if (instances > 0)
1333 {
1334 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1335 }
1336 else
1337 {
1338 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1339 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001340}
1341
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001342void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1343{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001344 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001345 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1346
1347 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001348 {
1349 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1350 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001351
daniel@transgaming.come4991412012-12-20 20:55:34 +00001352 ID3D11VertexShader *vertexShader = NULL;
1353 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001354
daniel@transgaming.come4991412012-12-20 20:55:34 +00001355 ID3D11PixelShader *pixelShader = NULL;
1356 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001357
daniel@transgaming.come4991412012-12-20 20:55:34 +00001358 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1359 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001360
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001361 programBinary->dirtyAllUniforms();
1362
1363 mAppliedProgramBinarySerial = programBinarySerial;
1364 }
1365
1366 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001367 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001368
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001369 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001370 {
1371 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001372 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001373 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1374 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001375 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1376 }
1377 else
1378 {
1379 mDeviceContext->GSSetShader(NULL, NULL, 0);
1380 }
1381
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001382 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001383 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001384}
1385
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001386void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001387{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001388 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1389 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001390
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001391 unsigned int totalRegisterCountVS = 0;
1392 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001393
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001394 bool vertexUniformsDirty = false;
1395 bool pixelUniformsDirty = false;
1396
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001397 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1398 {
1399 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001400
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001401 if (uniform->isReferencedByVertexShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001402 {
1403 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001404 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001405 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001406
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001407 if (uniform->isReferencedByFragmentShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001408 {
1409 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001410 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001411 }
1412 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001413
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001414 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1415 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1416
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001417 float (*mapVS)[4] = NULL;
1418 float (*mapPS)[4] = NULL;
1419
Shannon Woods5ab33c82013-06-26 15:31:09 -04001420 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1421 {
1422 D3D11_MAPPED_SUBRESOURCE map = {0};
1423 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1424 ASSERT(SUCCEEDED(result));
1425 mapVS = (float(*)[4])map.pData;
1426 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001427
Shannon Woods5ab33c82013-06-26 15:31:09 -04001428 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1429 {
1430 D3D11_MAPPED_SUBRESOURCE map = {0};
1431 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1432 ASSERT(SUCCEEDED(result));
1433 mapPS = (float(*)[4])map.pData;
1434 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001435
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001436 for (gl::UniformArray::iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001437 {
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001438 gl::Uniform *uniform = *uniform_iterator;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001439
Nicolas Capense6050882013-07-08 10:43:10 -04001440 if (!gl::IsSampler(uniform->type))
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001441 {
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001442 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001443 {
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001444 memcpy(mapVS + uniform->vsRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4]));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001445 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001446
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001447 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001448 {
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001449 memcpy(mapPS + uniform->psRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4]));
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001450 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001451 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001452
1453 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001454 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001455
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001456 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001457 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001458 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001459 }
1460
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001461 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001462 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001463 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001464 }
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001465
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001466 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1467 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001468
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001469 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001470 if (!mDriverConstantBufferVS)
1471 {
1472 D3D11_BUFFER_DESC constantBufferDescription = {0};
1473 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1474 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1475 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1476 constantBufferDescription.CPUAccessFlags = 0;
1477 constantBufferDescription.MiscFlags = 0;
1478 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001479
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001480 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001481 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001482
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001483 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1484 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001485
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001486 if (!mDriverConstantBufferPS)
1487 {
1488 D3D11_BUFFER_DESC constantBufferDescription = {0};
1489 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1490 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1491 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1492 constantBufferDescription.CPUAccessFlags = 0;
1493 constantBufferDescription.MiscFlags = 0;
1494 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001495
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001496 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001497 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001498
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001499 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1500 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001501
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001502 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1503 {
1504 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1505 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1506 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001507
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001508 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1509 {
1510 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1511 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1512 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001513
1514 // needed for the point sprite geometry shader
1515 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001516}
1517
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001518void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001519{
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001520 bool alphaUnmasked = gl::GetAlphaBits(mRenderTargetDesc.format, getCurrentClientVersion()) == 0 ||
1521 clearParams.colorMaskAlpha;
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001522 bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1523 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1524 clearParams.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001525
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001526 unsigned int stencilUnmasked = 0x0;
1527 if (frameBuffer->hasStencil())
1528 {
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001529 unsigned int stencilSize = gl::GetStencilBits(frameBuffer->getStencilbuffer()->getActualFormat(),
1530 getCurrentClientVersion());
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001531 stencilUnmasked = (0x1 << stencilSize) - 1;
1532 }
1533 bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1534 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001535
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001536 bool needScissoredClear = mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1537 mCurScissor.x + mCurScissor.width < mRenderTargetDesc.width ||
1538 mCurScissor.y + mCurScissor.height < mRenderTargetDesc.height);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001539
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001540 if (needMaskedColorClear || needMaskedStencilClear || needScissoredClear)
1541 {
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001542 maskedClear(clearParams, frameBuffer->usingExtendedDrawBuffers());
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001543 }
1544 else
1545 {
1546 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1547 {
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001548 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001549 {
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001550 if (frameBuffer->isEnabledColorAttachment(colorAttachment))
1551 {
1552 gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer(colorAttachment);
1553 if (renderbufferObject)
1554 {
1555 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
1556 if (!renderTarget)
1557 {
1558 ERR("render target pointer unexpectedly null.");
1559 return;
1560 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001561
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001562 ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView();
1563 if (!framebufferRTV)
1564 {
1565 ERR("render target view pointer unexpectedly null.");
1566 return;
1567 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001568
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001569 const float clearValues[4] = { clearParams.colorClearValue.red,
1570 clearParams.colorClearValue.green,
1571 clearParams.colorClearValue.blue,
1572 clearParams.colorClearValue.alpha };
1573 mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001574
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001575 framebufferRTV->Release();
1576 }
1577 }
1578 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001579 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001580 if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001581 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001582 gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer();
1583 if (renderbufferObject)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001584 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001585 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil());
1586 if (!renderTarget)
1587 {
1588 ERR("render target pointer unexpectedly null.");
1589 return;
1590 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001591
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001592 ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView();
1593 if (!framebufferDSV)
1594 {
1595 ERR("depth stencil view pointer unexpectedly null.");
1596 return;
1597 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001598
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001599 UINT clearFlags = 0;
1600 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1601 {
1602 clearFlags |= D3D11_CLEAR_DEPTH;
1603 }
1604 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1605 {
1606 clearFlags |= D3D11_CLEAR_STENCIL;
1607 }
1608
shannon.woods@transgaming.combe211b32013-02-28 23:17:16 +00001609 float depthClear = gl::clamp01(clearParams.depthClearValue);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001610 UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF;
1611
1612 mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001613
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001614 framebufferDSV->Release();
1615 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001616 }
1617 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001618}
1619
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001620void Renderer11::maskedClear(const gl::ClearParameters &clearParams, bool usingExtendedDrawBuffers)
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001621{
1622 HRESULT result;
1623
1624 if (!mClearResourcesInitialized)
1625 {
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001626 ASSERT(!mClearVB && !mClearVS && !mClearSinglePS && !mClearMultiplePS && !mClearScissorRS && !mClearNoScissorRS);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001627
1628 D3D11_BUFFER_DESC vbDesc;
1629 vbDesc.ByteWidth = sizeof(d3d11::PositionDepthColorVertex) * 4;
1630 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
1631 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
1632 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1633 vbDesc.MiscFlags = 0;
1634 vbDesc.StructureByteStride = 0;
1635
1636 result = mDevice->CreateBuffer(&vbDesc, NULL, &mClearVB);
1637 ASSERT(SUCCEEDED(result));
1638 d3d11::SetDebugName(mClearVB, "Renderer11 masked clear vertex buffer");
1639
1640 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
1641 {
1642 { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1643 { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1644 };
1645
1646 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Clear, sizeof(g_VS_Clear), &mClearIL);
1647 ASSERT(SUCCEEDED(result));
1648 d3d11::SetDebugName(mClearIL, "Renderer11 masked clear input layout");
1649
1650 result = mDevice->CreateVertexShader(g_VS_Clear, sizeof(g_VS_Clear), NULL, &mClearVS);
1651 ASSERT(SUCCEEDED(result));
1652 d3d11::SetDebugName(mClearVS, "Renderer11 masked clear vertex shader");
1653
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001654 result = mDevice->CreatePixelShader(g_PS_ClearSingle, sizeof(g_PS_ClearSingle), NULL, &mClearSinglePS);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001655 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001656 d3d11::SetDebugName(mClearSinglePS, "Renderer11 masked clear pixel shader (1 RT)");
1657
1658 result = mDevice->CreatePixelShader(g_PS_ClearMultiple, sizeof(g_PS_ClearMultiple), NULL, &mClearMultiplePS);
1659 ASSERT(SUCCEEDED(result));
1660 d3d11::SetDebugName(mClearMultiplePS, "Renderer11 masked clear pixel shader (MRT)");
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001661
1662 D3D11_RASTERIZER_DESC rsScissorDesc;
1663 rsScissorDesc.FillMode = D3D11_FILL_SOLID;
1664 rsScissorDesc.CullMode = D3D11_CULL_NONE;
1665 rsScissorDesc.FrontCounterClockwise = FALSE;
1666 rsScissorDesc.DepthBias = 0;
1667 rsScissorDesc.DepthBiasClamp = 0.0f;
1668 rsScissorDesc.SlopeScaledDepthBias = 0.0f;
1669 rsScissorDesc.DepthClipEnable = FALSE;
1670 rsScissorDesc.ScissorEnable = TRUE;
1671 rsScissorDesc.MultisampleEnable = FALSE;
1672 rsScissorDesc.AntialiasedLineEnable = FALSE;
1673
1674 result = mDevice->CreateRasterizerState(&rsScissorDesc, &mClearScissorRS);
1675 ASSERT(SUCCEEDED(result));
1676 d3d11::SetDebugName(mClearScissorRS, "Renderer11 masked clear scissor rasterizer state");
1677
1678 D3D11_RASTERIZER_DESC rsNoScissorDesc;
1679 rsNoScissorDesc.FillMode = D3D11_FILL_SOLID;
1680 rsNoScissorDesc.CullMode = D3D11_CULL_NONE;
1681 rsNoScissorDesc.FrontCounterClockwise = FALSE;
1682 rsNoScissorDesc.DepthBias = 0;
1683 rsNoScissorDesc.DepthBiasClamp = 0.0f;
1684 rsNoScissorDesc.SlopeScaledDepthBias = 0.0f;
1685 rsNoScissorDesc.DepthClipEnable = FALSE;
1686 rsNoScissorDesc.ScissorEnable = FALSE;
1687 rsNoScissorDesc.MultisampleEnable = FALSE;
1688 rsNoScissorDesc.AntialiasedLineEnable = FALSE;
1689
1690 result = mDevice->CreateRasterizerState(&rsNoScissorDesc, &mClearNoScissorRS);
1691 ASSERT(SUCCEEDED(result));
1692 d3d11::SetDebugName(mClearNoScissorRS, "Renderer11 masked clear no scissor rasterizer state");
1693
1694 mClearResourcesInitialized = true;
1695 }
1696
1697 // Prepare the depth stencil state to write depth values if the depth should be cleared
1698 // and stencil values if the stencil should be cleared
1699 gl::DepthStencilState glDSState;
1700 glDSState.depthTest = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1701 glDSState.depthFunc = GL_ALWAYS;
1702 glDSState.depthMask = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1703 glDSState.stencilTest = (clearParams.mask & GL_STENCIL_BUFFER_BIT) != 0;
1704 glDSState.stencilFunc = GL_ALWAYS;
1705 glDSState.stencilMask = 0;
1706 glDSState.stencilFail = GL_REPLACE;
1707 glDSState.stencilPassDepthFail = GL_REPLACE;
1708 glDSState.stencilPassDepthPass = GL_REPLACE;
1709 glDSState.stencilWritemask = clearParams.stencilWriteMask;
1710 glDSState.stencilBackFunc = GL_ALWAYS;
1711 glDSState.stencilBackMask = 0;
1712 glDSState.stencilBackFail = GL_REPLACE;
1713 glDSState.stencilBackPassDepthFail = GL_REPLACE;
1714 glDSState.stencilBackPassDepthPass = GL_REPLACE;
1715 glDSState.stencilBackWritemask = clearParams.stencilWriteMask;
1716
1717 int stencilClear = clearParams.stencilClearValue & 0x000000FF;
1718
1719 ID3D11DepthStencilState *dsState = mStateCache.getDepthStencilState(glDSState);
1720
1721 // Prepare the blend state to use a write mask if the color buffer should be cleared
1722 gl::BlendState glBlendState;
1723 glBlendState.blend = false;
1724 glBlendState.sourceBlendRGB = GL_ONE;
1725 glBlendState.destBlendRGB = GL_ZERO;
1726 glBlendState.sourceBlendAlpha = GL_ONE;
1727 glBlendState.destBlendAlpha = GL_ZERO;
1728 glBlendState.blendEquationRGB = GL_FUNC_ADD;
1729 glBlendState.blendEquationAlpha = GL_FUNC_ADD;
1730 glBlendState.colorMaskRed = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskRed : false;
1731 glBlendState.colorMaskGreen = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskGreen : false;
1732 glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false;
shannon.woods@transgaming.com50ea9932013-02-28 23:13:40 +00001733 glBlendState.colorMaskAlpha = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskAlpha : false;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001734 glBlendState.sampleAlphaToCoverage = false;
1735 glBlendState.dither = false;
1736
1737 static const float blendFactors[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
1738 static const UINT sampleMask = 0xFFFFFFFF;
1739
1740 ID3D11BlendState *blendState = mStateCache.getBlendState(glBlendState);
1741
1742 // Set the vertices
1743 D3D11_MAPPED_SUBRESOURCE mappedResource;
1744 result = mDeviceContext->Map(mClearVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
1745 if (FAILED(result))
1746 {
1747 ERR("Failed to map masked clear vertex buffer, HRESULT: 0x%X.", result);
1748 return;
1749 }
1750
1751 d3d11::PositionDepthColorVertex *vertices = reinterpret_cast<d3d11::PositionDepthColorVertex*>(mappedResource.pData);
1752
1753 float depthClear = gl::clamp01(clearParams.depthClearValue);
1754 d3d11::SetPositionDepthColorVertex(&vertices[0], -1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1755 d3d11::SetPositionDepthColorVertex(&vertices[1], -1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1756 d3d11::SetPositionDepthColorVertex(&vertices[2], 1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1757 d3d11::SetPositionDepthColorVertex(&vertices[3], 1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1758
1759 mDeviceContext->Unmap(mClearVB, 0);
1760
1761 // Apply state
1762 mDeviceContext->OMSetBlendState(blendState, blendFactors, sampleMask);
1763 mDeviceContext->OMSetDepthStencilState(dsState, stencilClear);
1764 mDeviceContext->RSSetState(mScissorEnabled ? mClearScissorRS : mClearNoScissorRS);
1765
1766 // Apply shaders
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001767 ID3D11PixelShader *pixelShader = usingExtendedDrawBuffers ? mClearMultiplePS : mClearSinglePS;
1768
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001769 mDeviceContext->IASetInputLayout(mClearIL);
1770 mDeviceContext->VSSetShader(mClearVS, NULL, 0);
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001771 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
shannon.woods@transgaming.com2c53e472013-02-28 23:13:56 +00001772 mDeviceContext->GSSetShader(NULL, NULL, 0);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001773
1774 // Apply vertex buffer
1775 static UINT stride = sizeof(d3d11::PositionDepthColorVertex);
1776 static UINT startIdx = 0;
1777 mDeviceContext->IASetVertexBuffers(0, 1, &mClearVB, &stride, &startIdx);
1778 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
1779
1780 // Draw the clear quad
1781 mDeviceContext->Draw(4, 0);
1782
1783 // Clean up
1784 markAllStateDirty();
1785}
1786
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001787void Renderer11::markAllStateDirty()
1788{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001789 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1790 {
1791 mAppliedRenderTargetSerials[rtIndex] = 0;
1792 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001793 mAppliedDepthbufferSerial = 0;
1794 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001795 mDepthStencilInitialized = false;
1796 mRenderTargetDescInitialized = false;
1797
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001798 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001799 {
1800 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001801 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001802 }
1803 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1804 {
1805 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001806 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001807 }
1808
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001809 mForceSetBlendState = true;
1810 mForceSetRasterState = true;
1811 mForceSetDepthStencilState = true;
1812 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001813 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001814
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001815 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001816 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001817 mAppliedIBOffset = 0;
1818
daniel@transgaming.come4991412012-12-20 20:55:34 +00001819 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001820 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1821 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001822}
1823
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001824void Renderer11::releaseDeviceResources()
1825{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001826 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001827 mInputLayoutCache.clear();
1828
1829 delete mVertexDataManager;
1830 mVertexDataManager = NULL;
1831
1832 delete mIndexDataManager;
1833 mIndexDataManager = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001834
1835 delete mLineLoopIB;
1836 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001837
1838 delete mTriangleFanIB;
1839 mTriangleFanIB = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001840
Geoff Langb86b9792013-06-04 16:32:05 -04001841 delete mBlit;
1842 mBlit = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001843
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001844 SafeRelease(mClearVB);
1845 SafeRelease(mClearIL);
1846 SafeRelease(mClearVS);
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001847 SafeRelease(mClearSinglePS);
1848 SafeRelease(mClearMultiplePS);
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001849 SafeRelease(mClearScissorRS);
1850 SafeRelease(mClearNoScissorRS);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001851
1852 mClearResourcesInitialized = false;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001853
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001854 SafeRelease(mDriverConstantBufferVS);
1855 SafeRelease(mDriverConstantBufferPS);
1856 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001857}
1858
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001859void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001860{
1861 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001862 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001863}
1864
1865bool Renderer11::isDeviceLost()
1866{
1867 return mDeviceLost;
1868}
1869
1870// set notify to true to broadcast a message to all contexts of the device loss
1871bool Renderer11::testDeviceLost(bool notify)
1872{
1873 bool isLost = false;
1874
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001875 // GetRemovedReason is used to test if the device is removed
1876 HRESULT result = mDevice->GetDeviceRemovedReason();
1877 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001878
1879 if (isLost)
1880 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001881 // Log error if this is a new device lost event
1882 if (mDeviceLost == false)
1883 {
1884 ERR("The D3D11 device was removed: 0x%08X", result);
1885 }
1886
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001887 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001888 // we'll probably get this done again by notifyDeviceLost
1889 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001890 // Note that we don't want to clear the device loss status here
1891 // -- this needs to be done by resetDevice
1892 mDeviceLost = true;
1893 if (notify)
1894 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001895 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001896 }
1897 }
1898
1899 return isLost;
1900}
1901
1902bool Renderer11::testDeviceResettable()
1903{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001904 // determine if the device is resettable by creating a dummy device
1905 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001906
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001907 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001908 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001909 return false;
1910 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001911
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001912 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001913 {
1914 D3D_FEATURE_LEVEL_11_0,
1915 D3D_FEATURE_LEVEL_10_1,
1916 D3D_FEATURE_LEVEL_10_0,
1917 };
1918
1919 ID3D11Device* dummyDevice;
1920 D3D_FEATURE_LEVEL dummyFeatureLevel;
1921 ID3D11DeviceContext* dummyContext;
1922
1923 HRESULT result = D3D11CreateDevice(NULL,
1924 D3D_DRIVER_TYPE_HARDWARE,
1925 NULL,
1926 #if defined(_DEBUG)
1927 D3D11_CREATE_DEVICE_DEBUG,
1928 #else
1929 0,
1930 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001931 featureLevels,
1932 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001933 D3D11_SDK_VERSION,
1934 &dummyDevice,
1935 &dummyFeatureLevel,
1936 &dummyContext);
1937
1938 if (!mDevice || FAILED(result))
1939 {
1940 return false;
1941 }
1942
1943 dummyContext->Release();
1944 dummyDevice->Release();
1945
1946 return true;
1947}
1948
1949void Renderer11::release()
1950{
1951 releaseDeviceResources();
1952
1953 if (mDxgiFactory)
1954 {
1955 mDxgiFactory->Release();
1956 mDxgiFactory = NULL;
1957 }
1958
1959 if (mDxgiAdapter)
1960 {
1961 mDxgiAdapter->Release();
1962 mDxgiAdapter = NULL;
1963 }
1964
1965 if (mDeviceContext)
1966 {
1967 mDeviceContext->ClearState();
1968 mDeviceContext->Flush();
1969 mDeviceContext->Release();
1970 mDeviceContext = NULL;
1971 }
1972
1973 if (mDevice)
1974 {
1975 mDevice->Release();
1976 mDevice = NULL;
1977 }
1978
1979 if (mD3d11Module)
1980 {
1981 FreeLibrary(mD3d11Module);
1982 mD3d11Module = NULL;
1983 }
1984
1985 if (mDxgiModule)
1986 {
1987 FreeLibrary(mDxgiModule);
1988 mDxgiModule = NULL;
1989 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001990}
1991
1992bool Renderer11::resetDevice()
1993{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001994 // recreate everything
1995 release();
1996 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001997
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001998 if (result != EGL_SUCCESS)
1999 {
2000 ERR("Could not reinitialize D3D11 device: %08X", result);
2001 return false;
2002 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002003
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002004 mDeviceLost = false;
2005
2006 return true;
2007}
2008
2009DWORD Renderer11::getAdapterVendor() const
2010{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00002011 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002012}
2013
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002014std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002015{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002016 std::ostringstream rendererString;
2017
2018 rendererString << mDescription;
2019 rendererString << " Direct3D11";
2020
2021 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
2022 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
2023
2024 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002025}
2026
2027GUID Renderer11::getAdapterIdentifier() const
2028{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00002029 // Use the adapter LUID as our adapter ID
2030 // This number is local to a machine is only guaranteed to be unique between restarts
2031 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
2032 GUID adapterId = {0};
2033 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
2034 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002035}
2036
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002037bool Renderer11::getBGRATextureSupport() const
2038{
2039 return mBGRATextureSupport;
2040}
2041
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002042bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002043{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002044 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002045}
2046
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002047bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002048{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002049 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002050}
2051
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002052bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002053{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002054 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002055}
2056
2057bool Renderer11::getDepthTextureSupport() const
2058{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00002059 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002060}
2061
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002062bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002063{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00002064 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002065}
2066
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002067bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002068{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002069 return mFloat32FilterSupport;
2070}
2071
2072bool Renderer11::getFloat32TextureRenderingSupport() const
2073{
2074 return mFloat32RenderSupport;
2075}
2076
2077bool Renderer11::getFloat16TextureSupport() const
2078{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00002079 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002080}
2081
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002082bool Renderer11::getFloat16TextureFilteringSupport() const
2083{
2084 return mFloat16FilterSupport;
2085}
2086
2087bool Renderer11::getFloat16TextureRenderingSupport() const
2088{
2089 return mFloat16RenderSupport;
2090}
2091
Geoff Langd42cf4e2013-06-05 16:09:17 -04002092bool Renderer11::getRGB565TextureSupport() const
2093{
2094 return false;
2095}
2096
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002097bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002098{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002099 return false;
2100}
2101
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002102bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002103{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002104 return false;
2105}
2106
2107bool Renderer11::getTextureFilterAnisotropySupport() const
2108{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002109 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002110}
2111
2112float Renderer11::getTextureMaxAnisotropy() const
2113{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002114 switch (mFeatureLevel)
2115 {
2116 case D3D_FEATURE_LEVEL_11_0:
2117 return D3D11_MAX_MAXANISOTROPY;
2118 case D3D_FEATURE_LEVEL_10_1:
2119 case D3D_FEATURE_LEVEL_10_0:
2120 return D3D10_MAX_MAXANISOTROPY;
2121 default: UNREACHABLE();
2122 return 0;
2123 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002124}
2125
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002126bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002127{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002128 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002129}
2130
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002131Range Renderer11::getViewportBounds() const
2132{
2133 switch (mFeatureLevel)
2134 {
2135 case D3D_FEATURE_LEVEL_11_0:
2136 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
2137 case D3D_FEATURE_LEVEL_10_1:
2138 case D3D_FEATURE_LEVEL_10_0:
2139 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
2140 default: UNREACHABLE();
2141 return Range(0, 0);
2142 }
2143}
2144
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002145unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002146{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002147 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2148 switch (mFeatureLevel)
2149 {
2150 case D3D_FEATURE_LEVEL_11_0:
2151 case D3D_FEATURE_LEVEL_10_1:
2152 case D3D_FEATURE_LEVEL_10_0:
2153 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
2154 default: UNREACHABLE();
2155 return 0;
2156 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002157}
2158
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002159unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
2160{
2161 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2162}
2163
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002164unsigned int Renderer11::getReservedVertexUniformVectors() const
2165{
Shannon Woods5ab33c82013-06-26 15:31:09 -04002166 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002167}
2168
2169unsigned int Renderer11::getReservedFragmentUniformVectors() const
2170{
Shannon Woods5ab33c82013-06-26 15:31:09 -04002171 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002172}
2173
2174unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002175{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002176 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2177 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2178 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002179}
2180
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002181unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002182{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002183 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2184 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2185 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002186}
2187
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002188unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002189{
2190 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00002191 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
2192 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002193 switch (mFeatureLevel)
2194 {
2195 case D3D_FEATURE_LEVEL_11_0:
2196 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2197 case D3D_FEATURE_LEVEL_10_1:
2198 case D3D_FEATURE_LEVEL_10_0:
2199 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2200 default: UNREACHABLE();
2201 return 0;
2202 }
2203}
2204
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002205unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2206{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002207 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2208 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2209
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002210 switch (mFeatureLevel)
2211 {
2212 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002213 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002214 case D3D_FEATURE_LEVEL_10_1:
2215 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002216 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002217 default: UNREACHABLE();
2218 return 0;
2219 }
2220}
2221
2222unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2223{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002224 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2225 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2226
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002227 switch (mFeatureLevel)
2228 {
2229 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002230 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002231 case D3D_FEATURE_LEVEL_10_1:
2232 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002233 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002234 default: UNREACHABLE();
2235 return 0;
2236 }
2237}
2238
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002239unsigned int Renderer11::getReservedVertexUniformBuffers() const
2240{
2241 // we reserve one buffer for the application uniforms, and one for driver uniforms
2242 return 2;
2243}
2244
2245unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2246{
2247 // we reserve one buffer for the application uniforms, and one for driver uniforms
2248 return 2;
2249}
2250
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002251unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2252{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002253 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2254 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2255
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002256 switch (mFeatureLevel)
2257 {
2258 case D3D_FEATURE_LEVEL_11_0:
2259 return D3D11_SO_BUFFER_SLOT_COUNT;
2260 case D3D_FEATURE_LEVEL_10_1:
2261 case D3D_FEATURE_LEVEL_10_0:
2262 return D3D10_SO_BUFFER_SLOT_COUNT;
2263 default: UNREACHABLE();
2264 return 0;
2265 }
2266}
2267
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002268unsigned int Renderer11::getMaxUniformBufferSize() const
2269{
2270 // Each component is a 4-element vector of 4-byte units (floats)
2271 const unsigned int bytesPerComponent = 4 * sizeof(float);
2272
2273 switch (mFeatureLevel)
2274 {
2275 case D3D_FEATURE_LEVEL_11_0:
2276 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2277 case D3D_FEATURE_LEVEL_10_1:
2278 case D3D_FEATURE_LEVEL_10_0:
2279 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2280 default: UNREACHABLE();
2281 return 0;
2282 }
2283}
2284
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002285bool Renderer11::getNonPower2TextureSupport() const
2286{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002287 switch (mFeatureLevel)
2288 {
2289 case D3D_FEATURE_LEVEL_11_0:
2290 case D3D_FEATURE_LEVEL_10_1:
2291 case D3D_FEATURE_LEVEL_10_0:
2292 return true;
2293 default: UNREACHABLE();
2294 return false;
2295 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002296}
2297
2298bool Renderer11::getOcclusionQuerySupport() const
2299{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002300 switch (mFeatureLevel)
2301 {
2302 case D3D_FEATURE_LEVEL_11_0:
2303 case D3D_FEATURE_LEVEL_10_1:
2304 case D3D_FEATURE_LEVEL_10_0:
2305 return true;
2306 default: UNREACHABLE();
2307 return false;
2308 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002309}
2310
2311bool Renderer11::getInstancingSupport() const
2312{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002313 switch (mFeatureLevel)
2314 {
2315 case D3D_FEATURE_LEVEL_11_0:
2316 case D3D_FEATURE_LEVEL_10_1:
2317 case D3D_FEATURE_LEVEL_10_0:
2318 return true;
2319 default: UNREACHABLE();
2320 return false;
2321 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002322}
2323
2324bool Renderer11::getShareHandleSupport() const
2325{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002326 // We only currently support share handles with BGRA surfaces, because
2327 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002328 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002329 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002330}
2331
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002332bool Renderer11::getDerivativeInstructionSupport() const
2333{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002334 switch (mFeatureLevel)
2335 {
2336 case D3D_FEATURE_LEVEL_11_0:
2337 case D3D_FEATURE_LEVEL_10_1:
2338 case D3D_FEATURE_LEVEL_10_0:
2339 return true;
2340 default: UNREACHABLE();
2341 return false;
2342 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002343}
2344
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002345bool Renderer11::getPostSubBufferSupport() const
2346{
2347 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2348 return false;
2349}
2350
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002351int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002352{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002353 switch (mFeatureLevel)
2354 {
2355 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002356 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002357 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2358 default: UNREACHABLE(); return 0;
2359 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002360}
2361
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002362int Renderer11::getMinorShaderModel() const
2363{
2364 switch (mFeatureLevel)
2365 {
2366 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2367 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2368 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2369 default: UNREACHABLE(); return 0;
2370 }
2371}
2372
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002373float Renderer11::getMaxPointSize() const
2374{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002375 // choose a reasonable maximum. we enforce this in the shader.
2376 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2377 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002378}
2379
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002380int Renderer11::getMaxViewportDimension() const
2381{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002382 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2383 // In our case return the maximum texture size, which is the maximum render buffer size.
2384 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2385 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2386
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002387 switch (mFeatureLevel)
2388 {
2389 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002390 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002391 case D3D_FEATURE_LEVEL_10_1:
2392 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002393 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002394 default: UNREACHABLE();
2395 return 0;
2396 }
2397}
2398
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002399int Renderer11::getMaxTextureWidth() const
2400{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002401 switch (mFeatureLevel)
2402 {
2403 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2404 case D3D_FEATURE_LEVEL_10_1:
2405 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2406 default: UNREACHABLE(); return 0;
2407 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002408}
2409
2410int Renderer11::getMaxTextureHeight() const
2411{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002412 switch (mFeatureLevel)
2413 {
2414 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2415 case D3D_FEATURE_LEVEL_10_1:
2416 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2417 default: UNREACHABLE(); return 0;
2418 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002419}
2420
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002421int Renderer11::getMaxTextureDepth() const
2422{
2423 switch (mFeatureLevel)
2424 {
2425 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2426 case D3D_FEATURE_LEVEL_10_1:
2427 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2428 default: UNREACHABLE(); return 0;
2429 }
2430}
2431
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002432int Renderer11::getMaxTextureArrayLayers() const
2433{
2434 switch (mFeatureLevel)
2435 {
2436 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2437 case D3D_FEATURE_LEVEL_10_1:
2438 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2439 default: UNREACHABLE(); return 0;
2440 }
2441}
2442
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002443bool Renderer11::get32BitIndexSupport() const
2444{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002445 switch (mFeatureLevel)
2446 {
2447 case D3D_FEATURE_LEVEL_11_0:
2448 case D3D_FEATURE_LEVEL_10_1:
2449 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2450 default: UNREACHABLE(); return false;
2451 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002452}
2453
2454int Renderer11::getMinSwapInterval() const
2455{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002456 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002457}
2458
2459int Renderer11::getMaxSwapInterval() const
2460{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002461 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002462}
2463
2464int Renderer11::getMaxSupportedSamples() const
2465{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002466 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002467}
2468
Geoff Lang0e120e32013-05-29 10:23:55 -04002469GLsizei Renderer11::getMaxSupportedFormatSamples(GLint internalFormat) const
2470{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002471 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002472 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2473 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2474}
2475
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002476GLsizei Renderer11::getNumSampleCounts(GLint internalFormat) const
2477{
2478 unsigned int numCounts = 0;
2479
2480 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
2481 if (!gl::IsIntegerFormat(internalFormat, getCurrentClientVersion()))
2482 {
2483 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2484 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2485
2486 if (iter != mMultisampleSupportMap.end())
2487 {
2488 const MultisampleSupportInfo& info = iter->second;
2489 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2490 {
2491 if (info.qualityLevels[i] > 0)
2492 {
2493 numCounts++;
2494 }
2495 }
2496 }
2497 }
2498
2499 return numCounts;
2500}
2501
2502void Renderer11::getSampleCounts(GLint internalFormat, GLsizei bufSize, GLint *params) const
2503{
2504 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
2505 if (gl::IsIntegerFormat(internalFormat, getCurrentClientVersion()))
2506 return;
2507
2508 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2509 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2510
2511 if (iter != mMultisampleSupportMap.end())
2512 {
2513 const MultisampleSupportInfo& info = iter->second;
2514 int bufPos = 0;
2515 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2516 {
2517 if (info.qualityLevels[i] > 0)
2518 {
2519 params[bufPos++] = i + 1;
2520 }
2521 }
2522 }
2523}
2524
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002525int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2526{
2527 if (requested == 0)
2528 {
2529 return 0;
2530 }
2531
2532 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2533 if (iter != mMultisampleSupportMap.end())
2534 {
2535 const MultisampleSupportInfo& info = iter->second;
2536 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2537 {
2538 if (info.qualityLevels[i] > 0)
2539 {
2540 return i + 1;
2541 }
2542 }
2543 }
2544
2545 return -1;
2546}
2547
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002548unsigned int Renderer11::getMaxRenderTargets() const
2549{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002550 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2551 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2552
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002553 switch (mFeatureLevel)
2554 {
2555 case D3D_FEATURE_LEVEL_11_0:
2556 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2557 case D3D_FEATURE_LEVEL_10_1:
2558 case D3D_FEATURE_LEVEL_10_0:
2559 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2560 default:
2561 UNREACHABLE();
2562 return 1;
2563 }
2564}
2565
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002566bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002567{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002568 if (source && dest)
2569 {
2570 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2571 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2572
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002573 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002574 return true;
2575 }
2576
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002577 return false;
2578}
2579
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002580bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002581{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002582 if (source && dest)
2583 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002584 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2585 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002586
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002587 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002588 return true;
2589 }
2590
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002591 return false;
2592}
2593
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002594bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2595{
2596 if (source && dest)
2597 {
2598 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2599 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2600
2601 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2602 return true;
2603 }
2604
2605 return false;
2606}
2607
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002608bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2609{
2610 if (source && dest)
2611 {
2612 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2613 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2614
2615 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2616 return true;
2617 }
2618
2619 return false;
2620}
2621
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002622bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002623 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002624{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002625 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002626 if (!colorbuffer)
2627 {
2628 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002629 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002630 }
2631
2632 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2633 if (!sourceRenderTarget)
2634 {
2635 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002636 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002637 }
2638
2639 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2640 if (!source)
2641 {
2642 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002643 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002644 }
2645
2646 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2647 if (!storage11)
2648 {
2649 source->Release();
2650 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002651 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002652 }
2653
2654 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2655 if (!destRenderTarget)
2656 {
2657 source->Release();
2658 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002659 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002660 }
2661
2662 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2663 if (!dest)
2664 {
2665 source->Release();
2666 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002667 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002668 }
2669
Geoff Langb86b9792013-06-04 16:32:05 -04002670 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2671 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002672
Geoff Langb86b9792013-06-04 16:32:05 -04002673 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2674 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002675
Geoff Langb86b9792013-06-04 16:32:05 -04002676 // Use nearest filtering because source and destination are the same size for the direct
2677 // copy
2678 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize,
2679 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002680
2681 source->Release();
2682 dest->Release();
2683
2684 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002685}
2686
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002687bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002688 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002689{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002690 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002691 if (!colorbuffer)
2692 {
2693 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002694 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002695 }
2696
2697 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2698 if (!sourceRenderTarget)
2699 {
2700 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002701 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002702 }
2703
2704 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2705 if (!source)
2706 {
2707 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002708 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002709 }
2710
2711 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2712 if (!storage11)
2713 {
2714 source->Release();
2715 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002716 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002717 }
2718
Nicolas Capensb13f8662013-06-04 13:30:19 -04002719 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002720 if (!destRenderTarget)
2721 {
2722 source->Release();
2723 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002724 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002725 }
2726
2727 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2728 if (!dest)
2729 {
2730 source->Release();
2731 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002732 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002733 }
2734
Geoff Langb86b9792013-06-04 16:32:05 -04002735 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2736 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002737
Geoff Langb86b9792013-06-04 16:32:05 -04002738 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2739 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002740
Geoff Langb86b9792013-06-04 16:32:05 -04002741 // Use nearest filtering because source and destination are the same size for the direct
2742 // copy
2743 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize,
2744 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002745
2746 source->Release();
2747 dest->Release();
2748
2749 return ret;
2750}
2751
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002752bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2753 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2754{
2755 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2756 if (!colorbuffer)
2757 {
2758 ERR("Failed to retrieve the color buffer from the frame buffer.");
2759 return gl::error(GL_OUT_OF_MEMORY, false);
2760 }
2761
2762 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2763 if (!sourceRenderTarget)
2764 {
2765 ERR("Failed to retrieve the render target from the frame buffer.");
2766 return gl::error(GL_OUT_OF_MEMORY, false);
2767 }
2768
2769 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2770 if (!source)
2771 {
2772 ERR("Failed to retrieve the render target view from the render target.");
2773 return gl::error(GL_OUT_OF_MEMORY, false);
2774 }
2775
2776 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2777 if (!storage11)
2778 {
2779 source->Release();
2780 ERR("Failed to retrieve the texture storage from the destination.");
2781 return gl::error(GL_OUT_OF_MEMORY, false);
2782 }
2783
2784 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2785 if (!destRenderTarget)
2786 {
2787 source->Release();
2788 ERR("Failed to retrieve the render target from the destination storage.");
2789 return gl::error(GL_OUT_OF_MEMORY, false);
2790 }
2791
2792 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2793 if (!dest)
2794 {
2795 source->Release();
2796 ERR("Failed to retrieve the render target view from the destination render target.");
2797 return gl::error(GL_OUT_OF_MEMORY, false);
2798 }
2799
Geoff Langb86b9792013-06-04 16:32:05 -04002800 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2801 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002802
Geoff Langb86b9792013-06-04 16:32:05 -04002803 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2804 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002805
Geoff Langb86b9792013-06-04 16:32:05 -04002806 // Use nearest filtering because source and destination are the same size for the direct
2807 // copy
2808 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize,
2809 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002810
2811 source->Release();
2812 dest->Release();
2813
2814 return ret;
2815}
2816
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002817bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2818 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2819{
2820 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2821 if (!colorbuffer)
2822 {
2823 ERR("Failed to retrieve the color buffer from the frame buffer.");
2824 return gl::error(GL_OUT_OF_MEMORY, false);
2825 }
2826
2827 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2828 if (!sourceRenderTarget)
2829 {
2830 ERR("Failed to retrieve the render target from the frame buffer.");
2831 return gl::error(GL_OUT_OF_MEMORY, false);
2832 }
2833
2834 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2835 if (!source)
2836 {
2837 ERR("Failed to retrieve the render target view from the render target.");
2838 return gl::error(GL_OUT_OF_MEMORY, false);
2839 }
2840
2841 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2842 if (!storage11)
2843 {
2844 source->Release();
2845 ERR("Failed to retrieve the texture storage from the destination.");
2846 return gl::error(GL_OUT_OF_MEMORY, false);
2847 }
2848
2849 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2850 if (!destRenderTarget)
2851 {
2852 source->Release();
2853 ERR("Failed to retrieve the render target from the destination storage.");
2854 return gl::error(GL_OUT_OF_MEMORY, false);
2855 }
2856
2857 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2858 if (!dest)
2859 {
2860 source->Release();
2861 ERR("Failed to retrieve the render target view from the destination render target.");
2862 return gl::error(GL_OUT_OF_MEMORY, false);
2863 }
2864
Geoff Langb86b9792013-06-04 16:32:05 -04002865 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2866 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002867
Geoff Langb86b9792013-06-04 16:32:05 -04002868 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2869 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002870
Geoff Langb86b9792013-06-04 16:32:05 -04002871 // Use nearest filtering because source and destination are the same size for the direct
2872 // copy
2873 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize,
2874 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002875
2876 source->Release();
2877 dest->Release();
2878
2879 return ret;
2880}
2881
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002882void Renderer11::unapplyRenderTargets()
2883{
2884 setOneTimeRenderTarget(NULL);
2885}
2886
2887void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2888{
2889 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2890
2891 rtvArray[0] = renderTargetView;
2892
2893 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2894
2895 // Do not preserve the serial for this one-time-use render target
2896 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2897 {
2898 mAppliedRenderTargetSerials[rtIndex] = 0;
2899 }
2900}
2901
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002902RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2903{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002904 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002905 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002906
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002907 if (depth)
2908 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002909 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002910 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
2911 swapChain11->getDepthStencilTexture(), NULL,
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002912 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002913 }
2914 else
2915 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002916 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002917 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002918 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002919 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002920 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002921 }
2922 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002923}
2924
Geoff Langa2d97f12013-06-11 11:44:02 -04002925RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002926{
Geoff Langa2d97f12013-06-11 11:44:02 -04002927 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002928 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002929}
2930
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002931ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002932{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002933 ShaderExecutable11 *executable = NULL;
2934
2935 switch (type)
2936 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002937 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002938 {
2939 ID3D11VertexShader *vshader = NULL;
2940 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2941 ASSERT(SUCCEEDED(result));
2942
2943 if (vshader)
2944 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002945 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002946 }
2947 }
2948 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002949 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002950 {
2951 ID3D11PixelShader *pshader = NULL;
2952 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2953 ASSERT(SUCCEEDED(result));
2954
2955 if (pshader)
2956 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002957 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002958 }
2959 }
2960 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002961 case rx::SHADER_GEOMETRY:
2962 {
2963 ID3D11GeometryShader *gshader = NULL;
2964 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2965 ASSERT(SUCCEEDED(result));
2966
2967 if (gshader)
2968 {
2969 executable = new ShaderExecutable11(function, length, gshader);
2970 }
2971 }
2972 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002973 default:
2974 UNREACHABLE();
2975 break;
2976 }
2977
2978 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002979}
2980
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002981ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002982{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002983 const char *profile = NULL;
2984
2985 switch (type)
2986 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002987 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002988 profile = "vs_4_0";
2989 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002990 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002991 profile = "ps_4_0";
2992 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002993 case rx::SHADER_GEOMETRY:
2994 profile = "gs_4_0";
2995 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002996 default:
2997 UNREACHABLE();
2998 return NULL;
2999 }
3000
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00003001 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003002 if (!binary)
3003 return NULL;
3004
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003005 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003006 binary->Release();
3007
3008 return executable;
3009}
3010
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00003011VertexBuffer *Renderer11::createVertexBuffer()
3012{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00003013 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00003014}
3015
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00003016IndexBuffer *Renderer11::createIndexBuffer()
3017{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00003018 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003019}
3020
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00003021BufferStorage *Renderer11::createBufferStorage()
3022{
3023 return new BufferStorage11(this);
3024}
3025
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003026QueryImpl *Renderer11::createQuery(GLenum type)
3027{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00003028 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003029}
3030
3031FenceImpl *Renderer11::createFence()
3032{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00003033 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00003034}
3035
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003036bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003037{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003038 ASSERT(colorbuffer != NULL);
3039
3040 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
3041 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003042 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003043 *subresourceIndex = renderTarget->getSubresourceIndex();
3044
3045 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
3046 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003047 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003048 ID3D11Resource *textureResource = NULL;
3049 colorBufferRTV->GetResource(&textureResource);
3050 colorBufferRTV->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003051
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003052 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003053 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003054 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
3055 textureResource->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003056
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003057 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003058 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003059 return true;
3060 }
3061 else
3062 {
3063 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
3064 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003065 }
3066 }
3067 }
3068 }
3069
3070 return false;
3071}
3072
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003073bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang685806d2013-06-12 11:16:36 -04003074 bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003075{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003076 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003077 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003078 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003079
3080 if (!readBuffer)
3081 {
3082 ERR("Failed to retrieve the read buffer from the read framebuffer.");
3083 return gl::error(GL_OUT_OF_MEMORY, false);
3084 }
3085
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003086 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003087
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003088 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003089 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003090 if (drawTarget->isEnabledColorAttachment(colorAttachment))
3091 {
3092 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
3093
3094 if (!drawBuffer)
3095 {
3096 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
3097 return gl::error(GL_OUT_OF_MEMORY, false);
3098 }
3099
3100 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
3101
Geoff Lang685806d2013-06-12 11:16:36 -04003102 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter,
3103 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003104 {
3105 return false;
3106 }
3107 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003108 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003109 }
3110
Geoff Lang685806d2013-06-12 11:16:36 -04003111 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003112 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003113 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
3114 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
3115
3116 if (!readBuffer)
3117 {
3118 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
3119 return gl::error(GL_OUT_OF_MEMORY, false);
3120 }
3121
3122 if (!drawBuffer)
3123 {
3124 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
3125 return gl::error(GL_OUT_OF_MEMORY, false);
3126 }
3127
3128 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3129 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3130
Geoff Lang685806d2013-06-12 11:16:36 -04003131 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter,
3132 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003133 {
3134 return false;
3135 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003136 }
3137
3138 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003139}
3140
3141void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3142 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3143{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003144 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003145 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003146
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003147 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3148
3149 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003150 {
3151 gl::Rectangle area;
3152 area.x = x;
3153 area.y = y;
3154 area.width = width;
3155 area.height = height;
3156
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003157 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3158 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003159
3160 colorBufferTexture->Release();
3161 colorBufferTexture = NULL;
3162 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003163}
3164
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003165Image *Renderer11::createImage()
3166{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003167 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003168}
3169
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003170void Renderer11::generateMipmap(Image *dest, Image *src)
3171{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003172 Image11 *dest11 = Image11::makeImage11(dest);
3173 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003174 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003175}
3176
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003177TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3178{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003179 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3180 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003181}
3182
3183TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3184{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003185 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003186}
3187
3188TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3189{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003190 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003191}
3192
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003193TextureStorage *Renderer11::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3194{
3195 return new TextureStorage11_3D(this, levels, internalformat, usage, width, height, depth);
3196}
3197
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003198TextureStorage *Renderer11::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3199{
3200 return new TextureStorage11_2DArray(this, levels, internalformat, usage, width, height, depth);
3201}
3202
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003203void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3204 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3205 GLint packAlignment, void *pixels)
3206{
3207 D3D11_TEXTURE2D_DESC textureDesc;
3208 texture->GetDesc(&textureDesc);
3209
3210 D3D11_TEXTURE2D_DESC stagingDesc;
3211 stagingDesc.Width = area.width;
3212 stagingDesc.Height = area.height;
3213 stagingDesc.MipLevels = 1;
3214 stagingDesc.ArraySize = 1;
3215 stagingDesc.Format = textureDesc.Format;
3216 stagingDesc.SampleDesc.Count = 1;
3217 stagingDesc.SampleDesc.Quality = 0;
3218 stagingDesc.Usage = D3D11_USAGE_STAGING;
3219 stagingDesc.BindFlags = 0;
3220 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3221 stagingDesc.MiscFlags = 0;
3222
3223 ID3D11Texture2D* stagingTex = NULL;
3224 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3225 if (FAILED(result))
3226 {
3227 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3228 return;
3229 }
3230
3231 ID3D11Texture2D* srcTex = NULL;
3232 if (textureDesc.SampleDesc.Count > 1)
3233 {
3234 D3D11_TEXTURE2D_DESC resolveDesc;
3235 resolveDesc.Width = textureDesc.Width;
3236 resolveDesc.Height = textureDesc.Height;
3237 resolveDesc.MipLevels = 1;
3238 resolveDesc.ArraySize = 1;
3239 resolveDesc.Format = textureDesc.Format;
3240 resolveDesc.SampleDesc.Count = 1;
3241 resolveDesc.SampleDesc.Quality = 0;
3242 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3243 resolveDesc.BindFlags = 0;
3244 resolveDesc.CPUAccessFlags = 0;
3245 resolveDesc.MiscFlags = 0;
3246
3247 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3248 if (FAILED(result))
3249 {
3250 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
3251 stagingTex->Release();
3252 return;
3253 }
3254
3255 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3256 subResource = 0;
3257 }
3258 else
3259 {
3260 srcTex = texture;
3261 srcTex->AddRef();
3262 }
3263
3264 D3D11_BOX srcBox;
3265 srcBox.left = area.x;
3266 srcBox.right = area.x + area.width;
3267 srcBox.top = area.y;
3268 srcBox.bottom = area.y + area.height;
3269 srcBox.front = 0;
3270 srcBox.back = 1;
3271
3272 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3273
3274 srcTex->Release();
3275 srcTex = NULL;
3276
3277 D3D11_MAPPED_SUBRESOURCE mapping;
3278 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3279
3280 unsigned char *source;
3281 int inputPitch;
3282 if (packReverseRowOrder)
3283 {
3284 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3285 inputPitch = -static_cast<int>(mapping.RowPitch);
3286 }
3287 else
3288 {
3289 source = static_cast<unsigned char*>(mapping.pData);
3290 inputPitch = static_cast<int>(mapping.RowPitch);
3291 }
3292
Geoff Lang697ad3e2013-06-04 10:11:28 -04003293 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003294
Jamie Madilld6cb2442013-07-10 15:13:38 -04003295 GLint sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003296 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3297 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3298
3299 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3300
3301 if (sourceFormat == format && sourceType == type)
3302 {
3303 // Direct copy possible
3304 unsigned char *dest = static_cast<unsigned char*>(pixels);
3305 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003306 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003307 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003308 }
3309 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003310 else
3311 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003312 GLint destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
3313 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3314
3315 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type, getCurrentClientVersion());
3316 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003317 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003318 // Fast copy is possible through some special function
3319 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003320 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003321 for (int x = 0; x < area.width; x++)
3322 {
3323 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3324 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3325
3326 fastCopyFunc(src, dest);
3327 }
3328 }
3329 }
3330 else
3331 {
Jamie Madilld6cb2442013-07-10 15:13:38 -04003332 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003333 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3334
3335 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3336 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3337 sizeof(temp) >= sizeof(gl::ColorUI) &&
3338 sizeof(temp) >= sizeof(gl::ColorI));
3339
3340 for (int y = 0; y < area.height; y++)
3341 {
3342 for (int x = 0; x < area.width; x++)
3343 {
3344 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3345 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3346
3347 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3348 // will not allow the copy otherwise.
3349 readFunc(src, temp);
3350 writeFunc(temp, dest);
3351 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003352 }
3353 }
3354 }
3355
3356 mDeviceContext->Unmap(stagingTex, 0);
3357
3358 stagingTex->Release();
3359 stagingTex = NULL;
3360}
3361
Geoff Lang758d5b22013-06-11 11:42:50 -04003362bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang685806d2013-06-12 11:16:36 -04003363 RenderTarget *drawRenderTarget, GLenum filter, bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003364{
Geoff Lang975af372013-06-12 11:19:22 -04003365 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3366 // it should never be the case that both color and depth/stencil need to be blitted at
3367 // at the same time.
3368 ASSERT(colorBlit != (depthBlit || stencilBlit));
3369
Geoff Langc1f51be2013-06-11 11:49:14 -04003370 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003371
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003372 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3373 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003374 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003375 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003376 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003377 }
3378
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003379 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003380 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003381 unsigned int readSubresource = 0;
3382 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003383 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003384 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3385 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
3386 unresolvedResource->Release();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003387
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003388 if (unresolvedTexture)
3389 {
3390 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3391 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003392
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003393 unresolvedTexture->Release();
Geoff Langc1f51be2013-06-11 11:49:14 -04003394
3395 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3396 if (FAILED(result))
3397 {
3398 readTexture->Release();
3399 return gl::error(GL_OUT_OF_MEMORY, false);
3400 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003401 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003402 }
3403 else
3404 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003405 readTexture = readRenderTarget11->getTexture();
3406 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003407 readSRV = readRenderTarget11->getShaderResourceView();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003408 }
3409
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003410 if (!readTexture)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003411 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003412 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003413 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003414 }
3415
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003416 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003417 if (!drawRenderTarget)
3418 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003419 readTexture->Release();
3420 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003421 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003422 }
3423
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003424 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003425 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003426 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3427 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003428
Geoff Langc1f51be2013-06-11 11:49:14 -04003429 bool wholeBufferCopy = readRect.x == 0 && readRect.width == readRenderTarget11->getWidth() &&
3430 readRect.y == 0 && readRect.height == readRenderTarget11->getHeight() &&
3431 drawRect.x == 0 && drawRect.width == drawRenderTarget->getWidth() &&
3432 drawRect.y == 0 && drawRect.height == drawRenderTarget->getHeight();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003433
Geoff Langc1f51be2013-06-11 11:49:14 -04003434 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003435
Geoff Langc1f51be2013-06-11 11:49:14 -04003436 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang685806d2013-06-12 11:16:36 -04003437 !stretchRequired && (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003438 {
3439 D3D11_BOX readBox;
3440 readBox.left = readRect.x;
3441 readBox.right = readRect.x + readRect.width;
3442 readBox.top = readRect.y;
3443 readBox.bottom = readRect.y + readRect.height;
3444 readBox.front = 0;
3445 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003446
Geoff Langc1f51be2013-06-11 11:49:14 -04003447 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3448 // We also require complete framebuffer copies for depth-stencil blit.
3449 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003450
Geoff Langc1f51be2013-06-11 11:49:14 -04003451 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, drawRect.x, drawRect.y, 0,
3452 readTexture, readSubresource, pSrcBox);
3453 result = true;
3454 }
3455 else
3456 {
3457 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
3458 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3459
3460 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
3461 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3462
Geoff Lang975af372013-06-12 11:19:22 -04003463 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003464 {
Geoff Lang975af372013-06-12 11:19:22 -04003465 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
3466 drawTexture, drawSubresource, drawArea, drawSize);
3467 }
3468 else if (depthBlit)
3469 {
3470 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize);
3471 }
3472 else if (stencilBlit)
3473 {
3474 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
3475 drawTexture, drawSubresource, drawArea, drawSize);
Geoff Langc1f51be2013-06-11 11:49:14 -04003476 }
3477 else
3478 {
Geoff Lang685806d2013-06-12 11:16:36 -04003479 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Langc1f51be2013-06-11 11:49:14 -04003480 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize, format, filter);
3481 }
3482 }
3483
3484 SafeRelease(readTexture);
3485 SafeRelease(readSRV);
3486 SafeRelease(drawTexture);
3487 SafeRelease(drawRTV);
3488 SafeRelease(drawDSV);
3489
3490 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003491}
3492
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003493ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3494{
3495 D3D11_TEXTURE2D_DESC textureDesc;
3496 source->GetDesc(&textureDesc);
3497
3498 if (textureDesc.SampleDesc.Count > 1)
3499 {
3500 D3D11_TEXTURE2D_DESC resolveDesc;
3501 resolveDesc.Width = textureDesc.Width;
3502 resolveDesc.Height = textureDesc.Height;
3503 resolveDesc.MipLevels = 1;
3504 resolveDesc.ArraySize = 1;
3505 resolveDesc.Format = textureDesc.Format;
3506 resolveDesc.SampleDesc.Count = 1;
3507 resolveDesc.SampleDesc.Quality = 0;
3508 resolveDesc.Usage = textureDesc.Usage;
3509 resolveDesc.BindFlags = textureDesc.BindFlags;
3510 resolveDesc.CPUAccessFlags = 0;
3511 resolveDesc.MiscFlags = 0;
3512
3513 ID3D11Texture2D *resolveTexture = NULL;
3514 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3515 if (FAILED(result))
3516 {
3517 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3518 return NULL;
3519 }
3520
3521 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3522 return resolveTexture;
3523 }
3524 else
3525 {
3526 source->AddRef();
3527 return source;
3528 }
3529}
3530
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003531bool Renderer11::getLUID(LUID *adapterLuid) const
3532{
3533 adapterLuid->HighPart = 0;
3534 adapterLuid->LowPart = 0;
3535
3536 if (!mDxgiAdapter)
3537 {
3538 return false;
3539 }
3540
3541 DXGI_ADAPTER_DESC adapterDesc;
3542 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3543 {
3544 return false;
3545 }
3546
3547 *adapterLuid = adapterDesc.AdapterLuid;
3548 return true;
3549}
3550
Geoff Lang61e49a52013-05-29 10:22:58 -04003551Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3552{
3553 MultisampleSupportInfo supportInfo = { 0 };
3554
3555 UINT formatSupport;
3556 HRESULT result;
3557
3558 result = mDevice->CheckFormatSupport(format, &formatSupport);
3559 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3560 {
3561 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3562 {
3563 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3564 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3565 {
3566 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3567 }
3568 else
3569 {
3570 supportInfo.qualityLevels[i - 1] = 0;
3571 }
3572 }
3573 }
3574
3575 return supportInfo;
3576}
3577
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003578}