blob: 75dafad26a94929efe78949b781c33009ddccdda [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
Geoff Langc6354ee2013-07-22 10:40:07 -0400636 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
637 {
638 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
639 1, &constantBuffer);
640 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
641 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000642 }
643 }
644
645 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
646 {
647 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
648 if (uniformBuffer)
649 {
650 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
651 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(GL_UNIFORM_BUFFER);
652
653 if (!constantBuffer)
654 {
655 return false;
656 }
657
Geoff Langc6354ee2013-07-22 10:40:07 -0400658 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
659 {
660 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
661 1, &constantBuffer);
662 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
663 }
664
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000665 pixelConstantBuffers[uniformBufferIndex] = constantBuffer;
666 }
667 }
668
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000669 return true;
670}
671
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000672void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000673{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000674 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000675 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000676 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
677 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000678 if (!dxRasterState)
679 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000680 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000681 "rasterizer state.");
682 }
683
684 mDeviceContext->RSSetState(dxRasterState);
685
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000686 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000687 }
688
689 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000690}
691
Geoff Lang2a64ee42013-05-31 11:22:40 -0400692void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000693 unsigned int sampleMask)
694{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000695 if (mForceSetBlendState ||
696 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400697 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000698 sampleMask != mCurSampleMask)
699 {
700 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
701 if (!dxBlendState)
702 {
703 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
704 "blend state.");
705 }
706
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000707 float blendColors[4] = {0.0f};
708 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
709 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
710 {
711 blendColors[0] = blendColor.red;
712 blendColors[1] = blendColor.green;
713 blendColors[2] = blendColor.blue;
714 blendColors[3] = blendColor.alpha;
715 }
716 else
717 {
718 blendColors[0] = blendColor.alpha;
719 blendColors[1] = blendColor.alpha;
720 blendColors[2] = blendColor.alpha;
721 blendColors[3] = blendColor.alpha;
722 }
723
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000724 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
725
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000726 mCurBlendState = blendState;
727 mCurBlendColor = blendColor;
728 mCurSampleMask = sampleMask;
729 }
730
731 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000732}
733
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000734void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000735 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000736{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000737 if (mForceSetDepthStencilState ||
738 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
739 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
740 {
741 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
742 stencilRef != stencilBackRef ||
743 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
744 {
745 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
746 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000747 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000748 }
749
750 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
751 if (!dxDepthStencilState)
752 {
753 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
754 "setting the default depth stencil state.");
755 }
756
757 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
758
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000759 mCurDepthStencilState = depthStencilState;
760 mCurStencilRef = stencilRef;
761 mCurStencilBackRef = stencilBackRef;
762 }
763
764 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000765}
766
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000767void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000768{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000769 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
770 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000771 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000772 if (enabled)
773 {
774 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000775 rect.left = std::max(0, scissor.x);
776 rect.top = std::max(0, scissor.y);
777 rect.right = scissor.x + std::max(0, scissor.width);
778 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000779
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000780 mDeviceContext->RSSetScissorRects(1, &rect);
781 }
782
783 if (enabled != mScissorEnabled)
784 {
785 mForceSetRasterState = true;
786 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000787
788 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000789 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000790 }
791
792 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000793}
794
daniel@transgaming.com12985182012-12-20 20:56:31 +0000795bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000796 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000797{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000798 gl::Rectangle actualViewport = viewport;
799 float actualZNear = gl::clamp01(zNear);
800 float actualZFar = gl::clamp01(zFar);
801 if (ignoreViewport)
802 {
803 actualViewport.x = 0;
804 actualViewport.y = 0;
805 actualViewport.width = mRenderTargetDesc.width;
806 actualViewport.height = mRenderTargetDesc.height;
807 actualZNear = 0.0f;
808 actualZFar = 1.0f;
809 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000810
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000811 // Get D3D viewport bounds, which depends on the feature level
812 const Range& viewportBounds = getViewportBounds();
813
814 // 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 +0000815 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000816 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
817 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
818 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
819 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
820 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
821 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000822 dxViewport.MinDepth = actualZNear;
823 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000824
825 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
826 {
827 return false; // Nothing to render
828 }
829
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000830 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
831 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000832
daniel@transgaming.com53670042012-11-28 20:55:51 +0000833 if (viewportChanged)
834 {
835 mDeviceContext->RSSetViewports(1, &dxViewport);
836
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000837 mCurViewport = actualViewport;
838 mCurNear = actualZNear;
839 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000840
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000841 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
842 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
843 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
844 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000845
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000846 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
847 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000848
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000849 mVertexConstants.depthRange[0] = actualZNear;
850 mVertexConstants.depthRange[1] = actualZFar;
851 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000852
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000853 mPixelConstants.depthRange[0] = actualZNear;
854 mPixelConstants.depthRange[1] = actualZFar;
855 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000856 }
857
858 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000859 return true;
860}
861
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000862bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
863{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000864 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000865
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000866 switch (mode)
867 {
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000868 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break;
869 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break;
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000870 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000871 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
872 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
873 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000874 // emulate fans via rewriting index buffer
Shannon Woods5ab33c82013-06-26 15:31:09 -0400875 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000876 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000877 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000878 }
879
Geoff Lang4c095862013-07-22 10:43:36 -0400880 if (primitiveTopology != mCurrentPrimitiveTopology)
881 {
882 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
883 mCurrentPrimitiveTopology = primitiveTopology;
884 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000885
886 return count > 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000887}
888
889bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000890{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000891 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000892 // Also extract the render target dimensions and view
893 unsigned int renderTargetWidth = 0;
894 unsigned int renderTargetHeight = 0;
895 GLenum renderTargetFormat = 0;
896 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
897 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
898 bool missingColorRenderTarget = true;
899
900 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000901 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000902 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
903
904 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000905 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000906 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
907 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
908
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000909 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000910
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000911 if (!colorbuffer)
912 {
913 ERR("render target pointer unexpectedly null.");
914 return false;
915 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000916
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000917 // check for zero-sized default framebuffer, which is a special case.
918 // in this case we do not wish to modify any state and just silently return false.
919 // this will not report any gl error but will cause the calling method to return.
920 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
921 {
922 return false;
923 }
924
925 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
926
927 // Extract the render target dimensions and view
928 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
929 if (!renderTarget)
930 {
931 ERR("render target pointer unexpectedly null.");
932 return false;
933 }
934
935 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
936 if (!framebufferRTVs[colorAttachment])
937 {
938 ERR("render target view pointer unexpectedly null.");
939 return false;
940 }
941
942 if (missingColorRenderTarget)
943 {
944 renderTargetWidth = colorbuffer->getWidth();
945 renderTargetHeight = colorbuffer->getHeight();
946 renderTargetFormat = colorbuffer->getActualFormat();
947 missingColorRenderTarget = false;
948 }
949 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000950 }
951
952 // Get the depth stencil render buffer and serials
953 gl::Renderbuffer *depthStencil = NULL;
954 unsigned int depthbufferSerial = 0;
955 unsigned int stencilbufferSerial = 0;
956 if (framebuffer->getDepthbufferType() != GL_NONE)
957 {
958 depthStencil = framebuffer->getDepthbuffer();
959 if (!depthStencil)
960 {
961 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000962 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000963 return false;
964 }
965
966 depthbufferSerial = depthStencil->getSerial();
967 }
968 else if (framebuffer->getStencilbufferType() != GL_NONE)
969 {
970 depthStencil = framebuffer->getStencilbuffer();
971 if (!depthStencil)
972 {
973 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000974 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000975 return false;
976 }
977
978 stencilbufferSerial = depthStencil->getSerial();
979 }
980
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000981 // Extract the depth stencil sizes and view
982 unsigned int depthSize = 0;
983 unsigned int stencilSize = 0;
984 ID3D11DepthStencilView* framebufferDSV = NULL;
985 if (depthStencil)
986 {
987 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
988 if (!depthStencilRenderTarget)
989 {
990 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000991 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000992 return false;
993 }
994
995 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
996 if (!framebufferDSV)
997 {
998 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000999 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001000 return false;
1001 }
1002
1003 // If there is no render buffer, the width, height and format values come from
1004 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001005 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001006 {
1007 renderTargetWidth = depthStencil->getWidth();
1008 renderTargetHeight = depthStencil->getHeight();
1009 renderTargetFormat = depthStencil->getActualFormat();
1010 }
1011
1012 depthSize = depthStencil->getDepthSize();
1013 stencilSize = depthStencil->getStencilSize();
1014 }
1015
1016 // Apply the render target and depth stencil
1017 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001018 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001019 depthbufferSerial != mAppliedDepthbufferSerial ||
1020 stencilbufferSerial != mAppliedStencilbufferSerial)
1021 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001022 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001023
1024 mRenderTargetDesc.width = renderTargetWidth;
1025 mRenderTargetDesc.height = renderTargetHeight;
1026 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001027 mForceSetViewport = true;
1028 mForceSetScissor = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001029
1030 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1031 {
1032 mCurDepthSize = depthSize;
1033 mForceSetRasterState = true;
1034 }
1035
1036 mCurStencilSize = stencilSize;
1037
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001038 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1039 {
1040 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
1041 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001042 mAppliedDepthbufferSerial = depthbufferSerial;
1043 mAppliedStencilbufferSerial = stencilbufferSerial;
1044 mRenderTargetDescInitialized = true;
1045 mDepthStencilInitialized = true;
1046 }
1047
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001048 SafeRelease(framebufferRTVs);
1049 SafeRelease(framebufferDSV);
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001050
1051 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001052}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001053
Jamie Madill57a89722013-07-02 11:57:03 -04001054GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001055 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001056{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001057 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001058 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001059 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001060 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001061 return err;
1062 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001063
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001064 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001065}
1066
daniel@transgaming.com31240482012-11-28 21:06:41 +00001067GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001068{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001069 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001070
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001071 if (err == GL_NO_ERROR)
1072 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001073 if (indexInfo->storage)
1074 {
1075 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1076 {
1077 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1078 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1079
shannonwoods@chromium.org675526e2013-05-30 00:04:49 +00001080 mDeviceContext->IASetIndexBuffer(storage->getBuffer(GL_ELEMENT_ARRAY_BUFFER), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001081
1082 mAppliedIBSerial = 0;
1083 mAppliedStorageIBSerial = storage->getSerial();
1084 mAppliedIBOffset = indexInfo->startOffset;
1085 }
1086 }
1087 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001088 {
1089 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1090
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001091 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001092
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001093 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001094 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001095 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001096 }
1097 }
1098
1099 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001100}
1101
1102void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1103{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001104 if (mode == GL_LINE_LOOP)
1105 {
1106 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1107 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001108 else if (mode == GL_TRIANGLE_FAN)
1109 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001110 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001111 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001112 else if (instances > 0)
1113 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001114 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001115 }
1116 else
1117 {
1118 mDeviceContext->Draw(count, 0);
1119 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001120}
1121
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001122void 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 +00001123{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001124 if (mode == GL_LINE_LOOP)
1125 {
1126 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1127 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001128 else if (mode == GL_TRIANGLE_FAN)
1129 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001130 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1131 }
1132 else if (instances > 0)
1133 {
1134 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001135 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001136 else
1137 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001138 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001139 }
1140}
1141
1142void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1143{
1144 // Get the raw indices for an indexed draw
1145 if (type != GL_NONE && elementArrayBuffer)
1146 {
1147 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001148 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001149 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001150 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001151 }
1152
1153 if (!mLineLoopIB)
1154 {
1155 mLineLoopIB = new StreamingIndexBufferInterface(this);
1156 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1157 {
1158 delete mLineLoopIB;
1159 mLineLoopIB = NULL;
1160
1161 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001162 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001163 }
1164 }
1165
Geoff Langeadfd572013-07-09 15:55:07 -04001166 if (static_cast<unsigned int>(count + 1) > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
1167 {
1168 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1169 return gl::error(GL_OUT_OF_MEMORY);
1170 }
1171
1172 const unsigned int spaceNeeded = (count + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001173 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1174 {
1175 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001176 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001177 }
1178
1179 void* mappedMemory = NULL;
1180 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1181 if (offset == -1 || mappedMemory == NULL)
1182 {
1183 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001184 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001185 }
1186
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001187 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001188 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001189
1190 switch (type)
1191 {
1192 case GL_NONE: // Non-indexed draw
1193 for (int i = 0; i < count; i++)
1194 {
1195 data[i] = i;
1196 }
1197 data[count] = 0;
1198 break;
1199 case GL_UNSIGNED_BYTE:
1200 for (int i = 0; i < count; i++)
1201 {
1202 data[i] = static_cast<const GLubyte*>(indices)[i];
1203 }
1204 data[count] = static_cast<const GLubyte*>(indices)[0];
1205 break;
1206 case GL_UNSIGNED_SHORT:
1207 for (int i = 0; i < count; i++)
1208 {
1209 data[i] = static_cast<const GLushort*>(indices)[i];
1210 }
1211 data[count] = static_cast<const GLushort*>(indices)[0];
1212 break;
1213 case GL_UNSIGNED_INT:
1214 for (int i = 0; i < count; i++)
1215 {
1216 data[i] = static_cast<const GLuint*>(indices)[i];
1217 }
1218 data[count] = static_cast<const GLuint*>(indices)[0];
1219 break;
1220 default: UNREACHABLE();
1221 }
1222
1223 if (!mLineLoopIB->unmapBuffer())
1224 {
1225 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001226 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001227 }
1228
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001229 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001230 {
1231 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1232
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001233 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001234 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001235 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001236 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001237 }
1238
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001239 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001240}
1241
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001242void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001243{
1244 // Get the raw indices for an indexed draw
1245 if (type != GL_NONE && elementArrayBuffer)
1246 {
1247 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001248 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001249 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001250 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001251 }
1252
1253 if (!mTriangleFanIB)
1254 {
1255 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1256 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1257 {
1258 delete mTriangleFanIB;
1259 mTriangleFanIB = NULL;
1260
1261 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001262 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001263 }
1264 }
1265
Geoff Langeadfd572013-07-09 15:55:07 -04001266 const unsigned int numTris = count - 2;
1267
1268 if (numTris * 3 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
1269 {
1270 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1271 return gl::error(GL_OUT_OF_MEMORY);
1272 }
1273
1274 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001275 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1276 {
1277 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001278 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001279 }
1280
1281 void* mappedMemory = NULL;
1282 int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory);
1283 if (offset == -1 || mappedMemory == NULL)
1284 {
1285 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001286 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001287 }
1288
1289 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1290 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
1291
1292 switch (type)
1293 {
1294 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001295 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001296 {
1297 data[i*3 + 0] = 0;
1298 data[i*3 + 1] = i + 1;
1299 data[i*3 + 2] = i + 2;
1300 }
1301 break;
1302 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001303 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001304 {
1305 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1306 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1307 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1308 }
1309 break;
1310 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001311 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001312 {
1313 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1314 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1315 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1316 }
1317 break;
1318 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001319 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001320 {
1321 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1322 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1323 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1324 }
1325 break;
1326 default: UNREACHABLE();
1327 }
1328
1329 if (!mTriangleFanIB->unmapBuffer())
1330 {
1331 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001332 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001333 }
1334
1335 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1336 {
1337 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1338
1339 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1340 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001341 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001342 mAppliedIBOffset = indexBufferOffset;
1343 }
1344
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001345 if (instances > 0)
1346 {
1347 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1348 }
1349 else
1350 {
1351 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1352 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001353}
1354
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001355void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1356{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001357 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001358 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1359
1360 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001361 {
1362 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1363 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001364
daniel@transgaming.come4991412012-12-20 20:55:34 +00001365 ID3D11VertexShader *vertexShader = NULL;
1366 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001367
daniel@transgaming.come4991412012-12-20 20:55:34 +00001368 ID3D11PixelShader *pixelShader = NULL;
1369 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001370
daniel@transgaming.come4991412012-12-20 20:55:34 +00001371 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1372 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001373
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001374 programBinary->dirtyAllUniforms();
1375
1376 mAppliedProgramBinarySerial = programBinarySerial;
1377 }
1378
1379 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001380 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001381
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001382 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001383 {
1384 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001385 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001386 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1387 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001388 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1389 }
1390 else
1391 {
1392 mDeviceContext->GSSetShader(NULL, NULL, 0);
1393 }
1394
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001395 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001396 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001397}
1398
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001399void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001400{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001401 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1402 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001403
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001404 unsigned int totalRegisterCountVS = 0;
1405 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001406
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001407 bool vertexUniformsDirty = false;
1408 bool pixelUniformsDirty = false;
1409
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001410 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1411 {
1412 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001413
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001414 if (uniform->isReferencedByVertexShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001415 {
1416 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001417 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001418 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001419
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001420 if (uniform->isReferencedByFragmentShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001421 {
1422 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001423 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001424 }
1425 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001426
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001427 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1428 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1429
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001430 float (*mapVS)[4] = NULL;
1431 float (*mapPS)[4] = NULL;
1432
Shannon Woods5ab33c82013-06-26 15:31:09 -04001433 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1434 {
1435 D3D11_MAPPED_SUBRESOURCE map = {0};
1436 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1437 ASSERT(SUCCEEDED(result));
1438 mapVS = (float(*)[4])map.pData;
1439 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001440
Shannon Woods5ab33c82013-06-26 15:31:09 -04001441 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1442 {
1443 D3D11_MAPPED_SUBRESOURCE map = {0};
1444 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1445 ASSERT(SUCCEEDED(result));
1446 mapPS = (float(*)[4])map.pData;
1447 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001448
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001449 for (gl::UniformArray::iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001450 {
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001451 gl::Uniform *uniform = *uniform_iterator;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001452
Nicolas Capense6050882013-07-08 10:43:10 -04001453 if (!gl::IsSampler(uniform->type))
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001454 {
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001455 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001456 {
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001457 memcpy(mapVS + uniform->vsRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4]));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001458 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001459
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001460 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001461 {
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001462 memcpy(mapPS + uniform->psRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4]));
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001463 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001464 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001465
1466 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001467 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001468
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001469 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001470 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001471 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001472 }
1473
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001474 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001475 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001476 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001477 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001478
1479 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1480 {
1481 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1482 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1483 }
1484
1485 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1486 {
1487 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1488 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1489 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001490
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001491 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001492 if (!mDriverConstantBufferVS)
1493 {
1494 D3D11_BUFFER_DESC constantBufferDescription = {0};
1495 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1496 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1497 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1498 constantBufferDescription.CPUAccessFlags = 0;
1499 constantBufferDescription.MiscFlags = 0;
1500 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001501
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001502 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001503 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001504
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001505 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1506 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001507
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001508 if (!mDriverConstantBufferPS)
1509 {
1510 D3D11_BUFFER_DESC constantBufferDescription = {0};
1511 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1512 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1513 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1514 constantBufferDescription.CPUAccessFlags = 0;
1515 constantBufferDescription.MiscFlags = 0;
1516 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001517
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001518 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001519 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001520
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001521 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1522 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001523
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001524 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1525 {
1526 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1527 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1528 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001529
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001530 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1531 {
1532 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1533 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1534 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001535
1536 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001537 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1538 {
1539 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1540 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1541 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001542}
1543
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001544void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001545{
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001546 bool alphaUnmasked = gl::GetAlphaBits(mRenderTargetDesc.format, getCurrentClientVersion()) == 0 ||
1547 clearParams.colorMaskAlpha;
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001548 bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1549 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1550 clearParams.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001551
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001552 unsigned int stencilUnmasked = 0x0;
1553 if (frameBuffer->hasStencil())
1554 {
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001555 unsigned int stencilSize = gl::GetStencilBits(frameBuffer->getStencilbuffer()->getActualFormat(),
1556 getCurrentClientVersion());
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001557 stencilUnmasked = (0x1 << stencilSize) - 1;
1558 }
1559 bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1560 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001561
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001562 bool needScissoredClear = mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1563 mCurScissor.x + mCurScissor.width < mRenderTargetDesc.width ||
1564 mCurScissor.y + mCurScissor.height < mRenderTargetDesc.height);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001565
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001566 if (needMaskedColorClear || needMaskedStencilClear || needScissoredClear)
1567 {
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001568 maskedClear(clearParams, frameBuffer->usingExtendedDrawBuffers());
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001569 }
1570 else
1571 {
1572 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1573 {
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001574 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001575 {
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001576 if (frameBuffer->isEnabledColorAttachment(colorAttachment))
1577 {
1578 gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer(colorAttachment);
1579 if (renderbufferObject)
1580 {
1581 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
1582 if (!renderTarget)
1583 {
1584 ERR("render target pointer unexpectedly null.");
1585 return;
1586 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001587
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001588 ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView();
1589 if (!framebufferRTV)
1590 {
1591 ERR("render target view pointer unexpectedly null.");
1592 return;
1593 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001594
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001595 const float clearValues[4] = { clearParams.colorClearValue.red,
1596 clearParams.colorClearValue.green,
1597 clearParams.colorClearValue.blue,
1598 clearParams.colorClearValue.alpha };
1599 mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001600
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001601 framebufferRTV->Release();
1602 }
1603 }
1604 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001605 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001606 if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001607 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001608 gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer();
1609 if (renderbufferObject)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001610 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001611 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil());
1612 if (!renderTarget)
1613 {
1614 ERR("render target pointer unexpectedly null.");
1615 return;
1616 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001617
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001618 ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView();
1619 if (!framebufferDSV)
1620 {
1621 ERR("depth stencil view pointer unexpectedly null.");
1622 return;
1623 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001624
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001625 UINT clearFlags = 0;
1626 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1627 {
1628 clearFlags |= D3D11_CLEAR_DEPTH;
1629 }
1630 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1631 {
1632 clearFlags |= D3D11_CLEAR_STENCIL;
1633 }
1634
shannon.woods@transgaming.combe211b32013-02-28 23:17:16 +00001635 float depthClear = gl::clamp01(clearParams.depthClearValue);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001636 UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF;
1637
1638 mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001639
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001640 framebufferDSV->Release();
1641 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001642 }
1643 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001644}
1645
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001646void Renderer11::maskedClear(const gl::ClearParameters &clearParams, bool usingExtendedDrawBuffers)
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001647{
1648 HRESULT result;
1649
1650 if (!mClearResourcesInitialized)
1651 {
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001652 ASSERT(!mClearVB && !mClearVS && !mClearSinglePS && !mClearMultiplePS && !mClearScissorRS && !mClearNoScissorRS);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001653
1654 D3D11_BUFFER_DESC vbDesc;
1655 vbDesc.ByteWidth = sizeof(d3d11::PositionDepthColorVertex) * 4;
1656 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
1657 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
1658 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1659 vbDesc.MiscFlags = 0;
1660 vbDesc.StructureByteStride = 0;
1661
1662 result = mDevice->CreateBuffer(&vbDesc, NULL, &mClearVB);
1663 ASSERT(SUCCEEDED(result));
1664 d3d11::SetDebugName(mClearVB, "Renderer11 masked clear vertex buffer");
1665
1666 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
1667 {
1668 { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1669 { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1670 };
1671
1672 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Clear, sizeof(g_VS_Clear), &mClearIL);
1673 ASSERT(SUCCEEDED(result));
1674 d3d11::SetDebugName(mClearIL, "Renderer11 masked clear input layout");
1675
1676 result = mDevice->CreateVertexShader(g_VS_Clear, sizeof(g_VS_Clear), NULL, &mClearVS);
1677 ASSERT(SUCCEEDED(result));
1678 d3d11::SetDebugName(mClearVS, "Renderer11 masked clear vertex shader");
1679
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001680 result = mDevice->CreatePixelShader(g_PS_ClearSingle, sizeof(g_PS_ClearSingle), NULL, &mClearSinglePS);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001681 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001682 d3d11::SetDebugName(mClearSinglePS, "Renderer11 masked clear pixel shader (1 RT)");
1683
1684 result = mDevice->CreatePixelShader(g_PS_ClearMultiple, sizeof(g_PS_ClearMultiple), NULL, &mClearMultiplePS);
1685 ASSERT(SUCCEEDED(result));
1686 d3d11::SetDebugName(mClearMultiplePS, "Renderer11 masked clear pixel shader (MRT)");
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001687
1688 D3D11_RASTERIZER_DESC rsScissorDesc;
1689 rsScissorDesc.FillMode = D3D11_FILL_SOLID;
1690 rsScissorDesc.CullMode = D3D11_CULL_NONE;
1691 rsScissorDesc.FrontCounterClockwise = FALSE;
1692 rsScissorDesc.DepthBias = 0;
1693 rsScissorDesc.DepthBiasClamp = 0.0f;
1694 rsScissorDesc.SlopeScaledDepthBias = 0.0f;
1695 rsScissorDesc.DepthClipEnable = FALSE;
1696 rsScissorDesc.ScissorEnable = TRUE;
1697 rsScissorDesc.MultisampleEnable = FALSE;
1698 rsScissorDesc.AntialiasedLineEnable = FALSE;
1699
1700 result = mDevice->CreateRasterizerState(&rsScissorDesc, &mClearScissorRS);
1701 ASSERT(SUCCEEDED(result));
1702 d3d11::SetDebugName(mClearScissorRS, "Renderer11 masked clear scissor rasterizer state");
1703
1704 D3D11_RASTERIZER_DESC rsNoScissorDesc;
1705 rsNoScissorDesc.FillMode = D3D11_FILL_SOLID;
1706 rsNoScissorDesc.CullMode = D3D11_CULL_NONE;
1707 rsNoScissorDesc.FrontCounterClockwise = FALSE;
1708 rsNoScissorDesc.DepthBias = 0;
1709 rsNoScissorDesc.DepthBiasClamp = 0.0f;
1710 rsNoScissorDesc.SlopeScaledDepthBias = 0.0f;
1711 rsNoScissorDesc.DepthClipEnable = FALSE;
1712 rsNoScissorDesc.ScissorEnable = FALSE;
1713 rsNoScissorDesc.MultisampleEnable = FALSE;
1714 rsNoScissorDesc.AntialiasedLineEnable = FALSE;
1715
1716 result = mDevice->CreateRasterizerState(&rsNoScissorDesc, &mClearNoScissorRS);
1717 ASSERT(SUCCEEDED(result));
1718 d3d11::SetDebugName(mClearNoScissorRS, "Renderer11 masked clear no scissor rasterizer state");
1719
1720 mClearResourcesInitialized = true;
1721 }
1722
1723 // Prepare the depth stencil state to write depth values if the depth should be cleared
1724 // and stencil values if the stencil should be cleared
1725 gl::DepthStencilState glDSState;
1726 glDSState.depthTest = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1727 glDSState.depthFunc = GL_ALWAYS;
1728 glDSState.depthMask = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1729 glDSState.stencilTest = (clearParams.mask & GL_STENCIL_BUFFER_BIT) != 0;
1730 glDSState.stencilFunc = GL_ALWAYS;
1731 glDSState.stencilMask = 0;
1732 glDSState.stencilFail = GL_REPLACE;
1733 glDSState.stencilPassDepthFail = GL_REPLACE;
1734 glDSState.stencilPassDepthPass = GL_REPLACE;
1735 glDSState.stencilWritemask = clearParams.stencilWriteMask;
1736 glDSState.stencilBackFunc = GL_ALWAYS;
1737 glDSState.stencilBackMask = 0;
1738 glDSState.stencilBackFail = GL_REPLACE;
1739 glDSState.stencilBackPassDepthFail = GL_REPLACE;
1740 glDSState.stencilBackPassDepthPass = GL_REPLACE;
1741 glDSState.stencilBackWritemask = clearParams.stencilWriteMask;
1742
1743 int stencilClear = clearParams.stencilClearValue & 0x000000FF;
1744
1745 ID3D11DepthStencilState *dsState = mStateCache.getDepthStencilState(glDSState);
1746
1747 // Prepare the blend state to use a write mask if the color buffer should be cleared
1748 gl::BlendState glBlendState;
1749 glBlendState.blend = false;
1750 glBlendState.sourceBlendRGB = GL_ONE;
1751 glBlendState.destBlendRGB = GL_ZERO;
1752 glBlendState.sourceBlendAlpha = GL_ONE;
1753 glBlendState.destBlendAlpha = GL_ZERO;
1754 glBlendState.blendEquationRGB = GL_FUNC_ADD;
1755 glBlendState.blendEquationAlpha = GL_FUNC_ADD;
1756 glBlendState.colorMaskRed = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskRed : false;
1757 glBlendState.colorMaskGreen = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskGreen : false;
1758 glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false;
shannon.woods@transgaming.com50ea9932013-02-28 23:13:40 +00001759 glBlendState.colorMaskAlpha = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskAlpha : false;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001760 glBlendState.sampleAlphaToCoverage = false;
1761 glBlendState.dither = false;
1762
1763 static const float blendFactors[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
1764 static const UINT sampleMask = 0xFFFFFFFF;
1765
1766 ID3D11BlendState *blendState = mStateCache.getBlendState(glBlendState);
1767
1768 // Set the vertices
1769 D3D11_MAPPED_SUBRESOURCE mappedResource;
1770 result = mDeviceContext->Map(mClearVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
1771 if (FAILED(result))
1772 {
1773 ERR("Failed to map masked clear vertex buffer, HRESULT: 0x%X.", result);
1774 return;
1775 }
1776
1777 d3d11::PositionDepthColorVertex *vertices = reinterpret_cast<d3d11::PositionDepthColorVertex*>(mappedResource.pData);
1778
1779 float depthClear = gl::clamp01(clearParams.depthClearValue);
1780 d3d11::SetPositionDepthColorVertex(&vertices[0], -1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1781 d3d11::SetPositionDepthColorVertex(&vertices[1], -1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1782 d3d11::SetPositionDepthColorVertex(&vertices[2], 1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1783 d3d11::SetPositionDepthColorVertex(&vertices[3], 1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1784
1785 mDeviceContext->Unmap(mClearVB, 0);
1786
1787 // Apply state
1788 mDeviceContext->OMSetBlendState(blendState, blendFactors, sampleMask);
1789 mDeviceContext->OMSetDepthStencilState(dsState, stencilClear);
1790 mDeviceContext->RSSetState(mScissorEnabled ? mClearScissorRS : mClearNoScissorRS);
1791
1792 // Apply shaders
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001793 ID3D11PixelShader *pixelShader = usingExtendedDrawBuffers ? mClearMultiplePS : mClearSinglePS;
1794
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001795 mDeviceContext->IASetInputLayout(mClearIL);
1796 mDeviceContext->VSSetShader(mClearVS, NULL, 0);
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001797 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
shannon.woods@transgaming.com2c53e472013-02-28 23:13:56 +00001798 mDeviceContext->GSSetShader(NULL, NULL, 0);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001799
1800 // Apply vertex buffer
1801 static UINT stride = sizeof(d3d11::PositionDepthColorVertex);
1802 static UINT startIdx = 0;
1803 mDeviceContext->IASetVertexBuffers(0, 1, &mClearVB, &stride, &startIdx);
1804 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
1805
1806 // Draw the clear quad
1807 mDeviceContext->Draw(4, 0);
1808
1809 // Clean up
1810 markAllStateDirty();
1811}
1812
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001813void Renderer11::markAllStateDirty()
1814{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001815 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1816 {
1817 mAppliedRenderTargetSerials[rtIndex] = 0;
1818 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001819 mAppliedDepthbufferSerial = 0;
1820 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001821 mDepthStencilInitialized = false;
1822 mRenderTargetDescInitialized = false;
1823
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001824 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001825 {
1826 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001827 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001828 }
1829 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1830 {
1831 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001832 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001833 }
1834
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001835 mForceSetBlendState = true;
1836 mForceSetRasterState = true;
1837 mForceSetDepthStencilState = true;
1838 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001839 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001840
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001841 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001842 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001843 mAppliedIBOffset = 0;
1844
daniel@transgaming.come4991412012-12-20 20:55:34 +00001845 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001846 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1847 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001848
1849 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001850
1851 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1852 {
1853 mCurrentConstantBufferVS[i] = -1;
1854 mCurrentConstantBufferPS[i] = -1;
1855 }
1856
1857 mCurrentVertexConstantBuffer = NULL;
1858 mCurrentPixelConstantBuffer = NULL;
1859 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001860
1861 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001862}
1863
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001864void Renderer11::releaseDeviceResources()
1865{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001866 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001867 mInputLayoutCache.clear();
1868
1869 delete mVertexDataManager;
1870 mVertexDataManager = NULL;
1871
1872 delete mIndexDataManager;
1873 mIndexDataManager = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001874
1875 delete mLineLoopIB;
1876 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001877
1878 delete mTriangleFanIB;
1879 mTriangleFanIB = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001880
Geoff Langb86b9792013-06-04 16:32:05 -04001881 delete mBlit;
1882 mBlit = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001883
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001884 SafeRelease(mClearVB);
1885 SafeRelease(mClearIL);
1886 SafeRelease(mClearVS);
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001887 SafeRelease(mClearSinglePS);
1888 SafeRelease(mClearMultiplePS);
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001889 SafeRelease(mClearScissorRS);
1890 SafeRelease(mClearNoScissorRS);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001891
1892 mClearResourcesInitialized = false;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001893
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001894 SafeRelease(mDriverConstantBufferVS);
1895 SafeRelease(mDriverConstantBufferPS);
1896 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001897}
1898
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001899void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001900{
1901 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001902 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001903}
1904
1905bool Renderer11::isDeviceLost()
1906{
1907 return mDeviceLost;
1908}
1909
1910// set notify to true to broadcast a message to all contexts of the device loss
1911bool Renderer11::testDeviceLost(bool notify)
1912{
1913 bool isLost = false;
1914
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001915 // GetRemovedReason is used to test if the device is removed
1916 HRESULT result = mDevice->GetDeviceRemovedReason();
1917 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001918
1919 if (isLost)
1920 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001921 // Log error if this is a new device lost event
1922 if (mDeviceLost == false)
1923 {
1924 ERR("The D3D11 device was removed: 0x%08X", result);
1925 }
1926
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001927 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001928 // we'll probably get this done again by notifyDeviceLost
1929 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001930 // Note that we don't want to clear the device loss status here
1931 // -- this needs to be done by resetDevice
1932 mDeviceLost = true;
1933 if (notify)
1934 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001935 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001936 }
1937 }
1938
1939 return isLost;
1940}
1941
1942bool Renderer11::testDeviceResettable()
1943{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001944 // determine if the device is resettable by creating a dummy device
1945 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001946
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001947 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001948 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001949 return false;
1950 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001951
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001952 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001953 {
1954 D3D_FEATURE_LEVEL_11_0,
1955 D3D_FEATURE_LEVEL_10_1,
1956 D3D_FEATURE_LEVEL_10_0,
1957 };
1958
1959 ID3D11Device* dummyDevice;
1960 D3D_FEATURE_LEVEL dummyFeatureLevel;
1961 ID3D11DeviceContext* dummyContext;
1962
1963 HRESULT result = D3D11CreateDevice(NULL,
1964 D3D_DRIVER_TYPE_HARDWARE,
1965 NULL,
1966 #if defined(_DEBUG)
1967 D3D11_CREATE_DEVICE_DEBUG,
1968 #else
1969 0,
1970 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001971 featureLevels,
1972 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001973 D3D11_SDK_VERSION,
1974 &dummyDevice,
1975 &dummyFeatureLevel,
1976 &dummyContext);
1977
1978 if (!mDevice || FAILED(result))
1979 {
1980 return false;
1981 }
1982
1983 dummyContext->Release();
1984 dummyDevice->Release();
1985
1986 return true;
1987}
1988
1989void Renderer11::release()
1990{
1991 releaseDeviceResources();
1992
1993 if (mDxgiFactory)
1994 {
1995 mDxgiFactory->Release();
1996 mDxgiFactory = NULL;
1997 }
1998
1999 if (mDxgiAdapter)
2000 {
2001 mDxgiAdapter->Release();
2002 mDxgiAdapter = NULL;
2003 }
2004
2005 if (mDeviceContext)
2006 {
2007 mDeviceContext->ClearState();
2008 mDeviceContext->Flush();
2009 mDeviceContext->Release();
2010 mDeviceContext = NULL;
2011 }
2012
2013 if (mDevice)
2014 {
2015 mDevice->Release();
2016 mDevice = NULL;
2017 }
2018
2019 if (mD3d11Module)
2020 {
2021 FreeLibrary(mD3d11Module);
2022 mD3d11Module = NULL;
2023 }
2024
2025 if (mDxgiModule)
2026 {
2027 FreeLibrary(mDxgiModule);
2028 mDxgiModule = NULL;
2029 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002030}
2031
2032bool Renderer11::resetDevice()
2033{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00002034 // recreate everything
2035 release();
2036 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002037
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00002038 if (result != EGL_SUCCESS)
2039 {
2040 ERR("Could not reinitialize D3D11 device: %08X", result);
2041 return false;
2042 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002043
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002044 mDeviceLost = false;
2045
2046 return true;
2047}
2048
2049DWORD Renderer11::getAdapterVendor() const
2050{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00002051 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002052}
2053
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002054std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002055{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002056 std::ostringstream rendererString;
2057
2058 rendererString << mDescription;
2059 rendererString << " Direct3D11";
2060
2061 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
2062 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
2063
2064 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002065}
2066
2067GUID Renderer11::getAdapterIdentifier() const
2068{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00002069 // Use the adapter LUID as our adapter ID
2070 // This number is local to a machine is only guaranteed to be unique between restarts
2071 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
2072 GUID adapterId = {0};
2073 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
2074 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002075}
2076
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002077bool Renderer11::getBGRATextureSupport() const
2078{
2079 return mBGRATextureSupport;
2080}
2081
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002082bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002083{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002084 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002085}
2086
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002087bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002088{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002089 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002090}
2091
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002092bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002093{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002094 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002095}
2096
2097bool Renderer11::getDepthTextureSupport() const
2098{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00002099 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002100}
2101
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002102bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002103{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00002104 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002105}
2106
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002107bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002108{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002109 return mFloat32FilterSupport;
2110}
2111
2112bool Renderer11::getFloat32TextureRenderingSupport() const
2113{
2114 return mFloat32RenderSupport;
2115}
2116
2117bool Renderer11::getFloat16TextureSupport() const
2118{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00002119 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002120}
2121
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002122bool Renderer11::getFloat16TextureFilteringSupport() const
2123{
2124 return mFloat16FilterSupport;
2125}
2126
2127bool Renderer11::getFloat16TextureRenderingSupport() const
2128{
2129 return mFloat16RenderSupport;
2130}
2131
Geoff Langd42cf4e2013-06-05 16:09:17 -04002132bool Renderer11::getRGB565TextureSupport() const
2133{
2134 return false;
2135}
2136
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002137bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002138{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002139 return false;
2140}
2141
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002142bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002143{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002144 return false;
2145}
2146
2147bool Renderer11::getTextureFilterAnisotropySupport() const
2148{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002149 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002150}
2151
2152float Renderer11::getTextureMaxAnisotropy() const
2153{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002154 switch (mFeatureLevel)
2155 {
2156 case D3D_FEATURE_LEVEL_11_0:
2157 return D3D11_MAX_MAXANISOTROPY;
2158 case D3D_FEATURE_LEVEL_10_1:
2159 case D3D_FEATURE_LEVEL_10_0:
2160 return D3D10_MAX_MAXANISOTROPY;
2161 default: UNREACHABLE();
2162 return 0;
2163 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002164}
2165
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002166bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002167{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002168 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002169}
2170
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002171Range Renderer11::getViewportBounds() const
2172{
2173 switch (mFeatureLevel)
2174 {
2175 case D3D_FEATURE_LEVEL_11_0:
2176 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
2177 case D3D_FEATURE_LEVEL_10_1:
2178 case D3D_FEATURE_LEVEL_10_0:
2179 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
2180 default: UNREACHABLE();
2181 return Range(0, 0);
2182 }
2183}
2184
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002185unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002186{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002187 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2188 switch (mFeatureLevel)
2189 {
2190 case D3D_FEATURE_LEVEL_11_0:
2191 case D3D_FEATURE_LEVEL_10_1:
2192 case D3D_FEATURE_LEVEL_10_0:
2193 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
2194 default: UNREACHABLE();
2195 return 0;
2196 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002197}
2198
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002199unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
2200{
2201 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2202}
2203
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002204unsigned int Renderer11::getReservedVertexUniformVectors() const
2205{
Shannon Woods5ab33c82013-06-26 15:31:09 -04002206 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002207}
2208
2209unsigned int Renderer11::getReservedFragmentUniformVectors() const
2210{
Shannon Woods5ab33c82013-06-26 15:31:09 -04002211 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002212}
2213
2214unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002215{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002216 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2217 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2218 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002219}
2220
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002221unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002222{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002223 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2224 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2225 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002226}
2227
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002228unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002229{
2230 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00002231 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
2232 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002233 switch (mFeatureLevel)
2234 {
2235 case D3D_FEATURE_LEVEL_11_0:
2236 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2237 case D3D_FEATURE_LEVEL_10_1:
2238 case D3D_FEATURE_LEVEL_10_0:
2239 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2240 default: UNREACHABLE();
2241 return 0;
2242 }
2243}
2244
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002245unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2246{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002247 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2248 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2249
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002250 switch (mFeatureLevel)
2251 {
2252 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002253 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002254 case D3D_FEATURE_LEVEL_10_1:
2255 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002256 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002257 default: UNREACHABLE();
2258 return 0;
2259 }
2260}
2261
2262unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2263{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002264 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2265 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2266
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002267 switch (mFeatureLevel)
2268 {
2269 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002270 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002271 case D3D_FEATURE_LEVEL_10_1:
2272 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002273 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002274 default: UNREACHABLE();
2275 return 0;
2276 }
2277}
2278
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002279unsigned int Renderer11::getReservedVertexUniformBuffers() const
2280{
2281 // we reserve one buffer for the application uniforms, and one for driver uniforms
2282 return 2;
2283}
2284
2285unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2286{
2287 // we reserve one buffer for the application uniforms, and one for driver uniforms
2288 return 2;
2289}
2290
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002291unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2292{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002293 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2294 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2295
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002296 switch (mFeatureLevel)
2297 {
2298 case D3D_FEATURE_LEVEL_11_0:
2299 return D3D11_SO_BUFFER_SLOT_COUNT;
2300 case D3D_FEATURE_LEVEL_10_1:
2301 case D3D_FEATURE_LEVEL_10_0:
2302 return D3D10_SO_BUFFER_SLOT_COUNT;
2303 default: UNREACHABLE();
2304 return 0;
2305 }
2306}
2307
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002308unsigned int Renderer11::getMaxUniformBufferSize() const
2309{
2310 // Each component is a 4-element vector of 4-byte units (floats)
2311 const unsigned int bytesPerComponent = 4 * sizeof(float);
2312
2313 switch (mFeatureLevel)
2314 {
2315 case D3D_FEATURE_LEVEL_11_0:
2316 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2317 case D3D_FEATURE_LEVEL_10_1:
2318 case D3D_FEATURE_LEVEL_10_0:
2319 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2320 default: UNREACHABLE();
2321 return 0;
2322 }
2323}
2324
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002325bool Renderer11::getNonPower2TextureSupport() const
2326{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002327 switch (mFeatureLevel)
2328 {
2329 case D3D_FEATURE_LEVEL_11_0:
2330 case D3D_FEATURE_LEVEL_10_1:
2331 case D3D_FEATURE_LEVEL_10_0:
2332 return true;
2333 default: UNREACHABLE();
2334 return false;
2335 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002336}
2337
2338bool Renderer11::getOcclusionQuerySupport() const
2339{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002340 switch (mFeatureLevel)
2341 {
2342 case D3D_FEATURE_LEVEL_11_0:
2343 case D3D_FEATURE_LEVEL_10_1:
2344 case D3D_FEATURE_LEVEL_10_0:
2345 return true;
2346 default: UNREACHABLE();
2347 return false;
2348 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002349}
2350
2351bool Renderer11::getInstancingSupport() const
2352{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002353 switch (mFeatureLevel)
2354 {
2355 case D3D_FEATURE_LEVEL_11_0:
2356 case D3D_FEATURE_LEVEL_10_1:
2357 case D3D_FEATURE_LEVEL_10_0:
2358 return true;
2359 default: UNREACHABLE();
2360 return false;
2361 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002362}
2363
2364bool Renderer11::getShareHandleSupport() const
2365{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002366 // We only currently support share handles with BGRA surfaces, because
2367 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002368 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002369 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002370}
2371
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002372bool Renderer11::getDerivativeInstructionSupport() const
2373{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002374 switch (mFeatureLevel)
2375 {
2376 case D3D_FEATURE_LEVEL_11_0:
2377 case D3D_FEATURE_LEVEL_10_1:
2378 case D3D_FEATURE_LEVEL_10_0:
2379 return true;
2380 default: UNREACHABLE();
2381 return false;
2382 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002383}
2384
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002385bool Renderer11::getPostSubBufferSupport() const
2386{
2387 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2388 return false;
2389}
2390
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002391int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002392{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002393 switch (mFeatureLevel)
2394 {
2395 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002396 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002397 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2398 default: UNREACHABLE(); return 0;
2399 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002400}
2401
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002402int Renderer11::getMinorShaderModel() const
2403{
2404 switch (mFeatureLevel)
2405 {
2406 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2407 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2408 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2409 default: UNREACHABLE(); return 0;
2410 }
2411}
2412
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002413float Renderer11::getMaxPointSize() const
2414{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002415 // choose a reasonable maximum. we enforce this in the shader.
2416 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2417 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002418}
2419
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002420int Renderer11::getMaxViewportDimension() const
2421{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002422 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2423 // In our case return the maximum texture size, which is the maximum render buffer size.
2424 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2425 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2426
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002427 switch (mFeatureLevel)
2428 {
2429 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002430 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002431 case D3D_FEATURE_LEVEL_10_1:
2432 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002433 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002434 default: UNREACHABLE();
2435 return 0;
2436 }
2437}
2438
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002439int Renderer11::getMaxTextureWidth() const
2440{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002441 switch (mFeatureLevel)
2442 {
2443 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2444 case D3D_FEATURE_LEVEL_10_1:
2445 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2446 default: UNREACHABLE(); return 0;
2447 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002448}
2449
2450int Renderer11::getMaxTextureHeight() const
2451{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002452 switch (mFeatureLevel)
2453 {
2454 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2455 case D3D_FEATURE_LEVEL_10_1:
2456 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2457 default: UNREACHABLE(); return 0;
2458 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002459}
2460
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002461int Renderer11::getMaxTextureDepth() const
2462{
2463 switch (mFeatureLevel)
2464 {
2465 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2466 case D3D_FEATURE_LEVEL_10_1:
2467 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2468 default: UNREACHABLE(); return 0;
2469 }
2470}
2471
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002472int Renderer11::getMaxTextureArrayLayers() const
2473{
2474 switch (mFeatureLevel)
2475 {
2476 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2477 case D3D_FEATURE_LEVEL_10_1:
2478 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2479 default: UNREACHABLE(); return 0;
2480 }
2481}
2482
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002483bool Renderer11::get32BitIndexSupport() const
2484{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002485 switch (mFeatureLevel)
2486 {
2487 case D3D_FEATURE_LEVEL_11_0:
2488 case D3D_FEATURE_LEVEL_10_1:
2489 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2490 default: UNREACHABLE(); return false;
2491 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002492}
2493
2494int Renderer11::getMinSwapInterval() const
2495{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002496 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002497}
2498
2499int Renderer11::getMaxSwapInterval() const
2500{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002501 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002502}
2503
2504int Renderer11::getMaxSupportedSamples() const
2505{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002506 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002507}
2508
Geoff Lang0e120e32013-05-29 10:23:55 -04002509GLsizei Renderer11::getMaxSupportedFormatSamples(GLint internalFormat) const
2510{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002511 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002512 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2513 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2514}
2515
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002516GLsizei Renderer11::getNumSampleCounts(GLint internalFormat) const
2517{
2518 unsigned int numCounts = 0;
2519
2520 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
2521 if (!gl::IsIntegerFormat(internalFormat, getCurrentClientVersion()))
2522 {
2523 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2524 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2525
2526 if (iter != mMultisampleSupportMap.end())
2527 {
2528 const MultisampleSupportInfo& info = iter->second;
2529 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2530 {
2531 if (info.qualityLevels[i] > 0)
2532 {
2533 numCounts++;
2534 }
2535 }
2536 }
2537 }
2538
2539 return numCounts;
2540}
2541
2542void Renderer11::getSampleCounts(GLint internalFormat, GLsizei bufSize, GLint *params) const
2543{
2544 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
2545 if (gl::IsIntegerFormat(internalFormat, getCurrentClientVersion()))
2546 return;
2547
2548 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2549 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2550
2551 if (iter != mMultisampleSupportMap.end())
2552 {
2553 const MultisampleSupportInfo& info = iter->second;
2554 int bufPos = 0;
2555 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2556 {
2557 if (info.qualityLevels[i] > 0)
2558 {
2559 params[bufPos++] = i + 1;
2560 }
2561 }
2562 }
2563}
2564
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002565int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2566{
2567 if (requested == 0)
2568 {
2569 return 0;
2570 }
2571
2572 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2573 if (iter != mMultisampleSupportMap.end())
2574 {
2575 const MultisampleSupportInfo& info = iter->second;
2576 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2577 {
2578 if (info.qualityLevels[i] > 0)
2579 {
2580 return i + 1;
2581 }
2582 }
2583 }
2584
2585 return -1;
2586}
2587
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002588unsigned int Renderer11::getMaxRenderTargets() const
2589{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002590 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2591 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2592
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002593 switch (mFeatureLevel)
2594 {
2595 case D3D_FEATURE_LEVEL_11_0:
2596 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2597 case D3D_FEATURE_LEVEL_10_1:
2598 case D3D_FEATURE_LEVEL_10_0:
2599 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2600 default:
2601 UNREACHABLE();
2602 return 1;
2603 }
2604}
2605
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002606bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002607{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002608 if (source && dest)
2609 {
2610 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2611 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2612
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002613 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002614 return true;
2615 }
2616
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002617 return false;
2618}
2619
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002620bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002621{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002622 if (source && dest)
2623 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002624 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2625 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002626
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002627 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002628 return true;
2629 }
2630
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002631 return false;
2632}
2633
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002634bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2635{
2636 if (source && dest)
2637 {
2638 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2639 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2640
2641 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2642 return true;
2643 }
2644
2645 return false;
2646}
2647
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002648bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2649{
2650 if (source && dest)
2651 {
2652 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2653 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2654
2655 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2656 return true;
2657 }
2658
2659 return false;
2660}
2661
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002662bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002663 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002664{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002665 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002666 if (!colorbuffer)
2667 {
2668 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002669 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002670 }
2671
2672 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2673 if (!sourceRenderTarget)
2674 {
2675 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002676 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002677 }
2678
2679 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2680 if (!source)
2681 {
2682 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002683 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002684 }
2685
2686 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2687 if (!storage11)
2688 {
2689 source->Release();
2690 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002691 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002692 }
2693
2694 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2695 if (!destRenderTarget)
2696 {
2697 source->Release();
2698 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002699 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002700 }
2701
2702 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2703 if (!dest)
2704 {
2705 source->Release();
2706 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002707 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002708 }
2709
Geoff Langb86b9792013-06-04 16:32:05 -04002710 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2711 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002712
Geoff Langb86b9792013-06-04 16:32:05 -04002713 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2714 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002715
Geoff Langb86b9792013-06-04 16:32:05 -04002716 // Use nearest filtering because source and destination are the same size for the direct
2717 // copy
2718 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize,
2719 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002720
2721 source->Release();
2722 dest->Release();
2723
2724 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002725}
2726
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002727bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002728 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002729{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002730 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002731 if (!colorbuffer)
2732 {
2733 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002734 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002735 }
2736
2737 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2738 if (!sourceRenderTarget)
2739 {
2740 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002741 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002742 }
2743
2744 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2745 if (!source)
2746 {
2747 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002748 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002749 }
2750
2751 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2752 if (!storage11)
2753 {
2754 source->Release();
2755 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002756 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002757 }
2758
Nicolas Capensb13f8662013-06-04 13:30:19 -04002759 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002760 if (!destRenderTarget)
2761 {
2762 source->Release();
2763 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002764 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002765 }
2766
2767 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2768 if (!dest)
2769 {
2770 source->Release();
2771 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002772 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002773 }
2774
Geoff Langb86b9792013-06-04 16:32:05 -04002775 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2776 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002777
Geoff Langb86b9792013-06-04 16:32:05 -04002778 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2779 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002780
Geoff Langb86b9792013-06-04 16:32:05 -04002781 // Use nearest filtering because source and destination are the same size for the direct
2782 // copy
2783 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize,
2784 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002785
2786 source->Release();
2787 dest->Release();
2788
2789 return ret;
2790}
2791
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002792bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2793 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2794{
2795 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2796 if (!colorbuffer)
2797 {
2798 ERR("Failed to retrieve the color buffer from the frame buffer.");
2799 return gl::error(GL_OUT_OF_MEMORY, false);
2800 }
2801
2802 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2803 if (!sourceRenderTarget)
2804 {
2805 ERR("Failed to retrieve the render target from the frame buffer.");
2806 return gl::error(GL_OUT_OF_MEMORY, false);
2807 }
2808
2809 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2810 if (!source)
2811 {
2812 ERR("Failed to retrieve the render target view from the render target.");
2813 return gl::error(GL_OUT_OF_MEMORY, false);
2814 }
2815
2816 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2817 if (!storage11)
2818 {
2819 source->Release();
2820 ERR("Failed to retrieve the texture storage from the destination.");
2821 return gl::error(GL_OUT_OF_MEMORY, false);
2822 }
2823
2824 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2825 if (!destRenderTarget)
2826 {
2827 source->Release();
2828 ERR("Failed to retrieve the render target from the destination storage.");
2829 return gl::error(GL_OUT_OF_MEMORY, false);
2830 }
2831
2832 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2833 if (!dest)
2834 {
2835 source->Release();
2836 ERR("Failed to retrieve the render target view from the destination render target.");
2837 return gl::error(GL_OUT_OF_MEMORY, false);
2838 }
2839
Geoff Langb86b9792013-06-04 16:32:05 -04002840 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2841 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002842
Geoff Langb86b9792013-06-04 16:32:05 -04002843 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2844 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002845
Geoff Langb86b9792013-06-04 16:32:05 -04002846 // Use nearest filtering because source and destination are the same size for the direct
2847 // copy
2848 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize,
2849 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002850
2851 source->Release();
2852 dest->Release();
2853
2854 return ret;
2855}
2856
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002857bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2858 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2859{
2860 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2861 if (!colorbuffer)
2862 {
2863 ERR("Failed to retrieve the color buffer from the frame buffer.");
2864 return gl::error(GL_OUT_OF_MEMORY, false);
2865 }
2866
2867 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2868 if (!sourceRenderTarget)
2869 {
2870 ERR("Failed to retrieve the render target from the frame buffer.");
2871 return gl::error(GL_OUT_OF_MEMORY, false);
2872 }
2873
2874 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2875 if (!source)
2876 {
2877 ERR("Failed to retrieve the render target view from the render target.");
2878 return gl::error(GL_OUT_OF_MEMORY, false);
2879 }
2880
2881 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2882 if (!storage11)
2883 {
2884 source->Release();
2885 ERR("Failed to retrieve the texture storage from the destination.");
2886 return gl::error(GL_OUT_OF_MEMORY, false);
2887 }
2888
2889 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2890 if (!destRenderTarget)
2891 {
2892 source->Release();
2893 ERR("Failed to retrieve the render target from the destination storage.");
2894 return gl::error(GL_OUT_OF_MEMORY, false);
2895 }
2896
2897 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2898 if (!dest)
2899 {
2900 source->Release();
2901 ERR("Failed to retrieve the render target view from the destination render target.");
2902 return gl::error(GL_OUT_OF_MEMORY, false);
2903 }
2904
Geoff Langb86b9792013-06-04 16:32:05 -04002905 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2906 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002907
Geoff Langb86b9792013-06-04 16:32:05 -04002908 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2909 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002910
Geoff Langb86b9792013-06-04 16:32:05 -04002911 // Use nearest filtering because source and destination are the same size for the direct
2912 // copy
2913 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize,
2914 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002915
2916 source->Release();
2917 dest->Release();
2918
2919 return ret;
2920}
2921
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002922void Renderer11::unapplyRenderTargets()
2923{
2924 setOneTimeRenderTarget(NULL);
2925}
2926
2927void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2928{
2929 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2930
2931 rtvArray[0] = renderTargetView;
2932
2933 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2934
2935 // Do not preserve the serial for this one-time-use render target
2936 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2937 {
2938 mAppliedRenderTargetSerials[rtIndex] = 0;
2939 }
2940}
2941
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002942RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2943{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002944 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002945 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002946
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002947 if (depth)
2948 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002949 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002950 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
2951 swapChain11->getDepthStencilTexture(), NULL,
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002952 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002953 }
2954 else
2955 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002956 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002957 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002958 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002959 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002960 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002961 }
2962 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002963}
2964
Geoff Langa2d97f12013-06-11 11:44:02 -04002965RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002966{
Geoff Langa2d97f12013-06-11 11:44:02 -04002967 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002968 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002969}
2970
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002971ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002972{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002973 ShaderExecutable11 *executable = NULL;
2974
2975 switch (type)
2976 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002977 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002978 {
2979 ID3D11VertexShader *vshader = NULL;
2980 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2981 ASSERT(SUCCEEDED(result));
2982
2983 if (vshader)
2984 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002985 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002986 }
2987 }
2988 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002989 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002990 {
2991 ID3D11PixelShader *pshader = NULL;
2992 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2993 ASSERT(SUCCEEDED(result));
2994
2995 if (pshader)
2996 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002997 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002998 }
2999 }
3000 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00003001 case rx::SHADER_GEOMETRY:
3002 {
3003 ID3D11GeometryShader *gshader = NULL;
3004 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
3005 ASSERT(SUCCEEDED(result));
3006
3007 if (gshader)
3008 {
3009 executable = new ShaderExecutable11(function, length, gshader);
3010 }
3011 }
3012 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003013 default:
3014 UNREACHABLE();
3015 break;
3016 }
3017
3018 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00003019}
3020
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003021ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003022{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003023 const char *profile = NULL;
3024
3025 switch (type)
3026 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003027 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003028 profile = "vs_4_0";
3029 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003030 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003031 profile = "ps_4_0";
3032 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00003033 case rx::SHADER_GEOMETRY:
3034 profile = "gs_4_0";
3035 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003036 default:
3037 UNREACHABLE();
3038 return NULL;
3039 }
3040
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00003041 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003042 if (!binary)
3043 return NULL;
3044
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003045 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003046 binary->Release();
3047
3048 return executable;
3049}
3050
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00003051VertexBuffer *Renderer11::createVertexBuffer()
3052{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00003053 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00003054}
3055
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00003056IndexBuffer *Renderer11::createIndexBuffer()
3057{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00003058 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003059}
3060
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00003061BufferStorage *Renderer11::createBufferStorage()
3062{
3063 return new BufferStorage11(this);
3064}
3065
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003066QueryImpl *Renderer11::createQuery(GLenum type)
3067{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00003068 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003069}
3070
3071FenceImpl *Renderer11::createFence()
3072{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00003073 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00003074}
3075
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003076bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003077{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003078 ASSERT(colorbuffer != NULL);
3079
3080 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
3081 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003082 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003083 *subresourceIndex = renderTarget->getSubresourceIndex();
3084
3085 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
3086 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003087 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003088 ID3D11Resource *textureResource = NULL;
3089 colorBufferRTV->GetResource(&textureResource);
3090 colorBufferRTV->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003091
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003092 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003093 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003094 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
3095 textureResource->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003096
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003097 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003098 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003099 return true;
3100 }
3101 else
3102 {
3103 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
3104 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003105 }
3106 }
3107 }
3108 }
3109
3110 return false;
3111}
3112
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003113bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang685806d2013-06-12 11:16:36 -04003114 bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003115{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003116 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003117 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003118 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003119
3120 if (!readBuffer)
3121 {
3122 ERR("Failed to retrieve the read buffer from the read framebuffer.");
3123 return gl::error(GL_OUT_OF_MEMORY, false);
3124 }
3125
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003126 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003127
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003128 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003129 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003130 if (drawTarget->isEnabledColorAttachment(colorAttachment))
3131 {
3132 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
3133
3134 if (!drawBuffer)
3135 {
3136 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
3137 return gl::error(GL_OUT_OF_MEMORY, false);
3138 }
3139
3140 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
3141
Geoff Lang685806d2013-06-12 11:16:36 -04003142 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter,
3143 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003144 {
3145 return false;
3146 }
3147 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003148 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003149 }
3150
Geoff Lang685806d2013-06-12 11:16:36 -04003151 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003152 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003153 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
3154 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
3155
3156 if (!readBuffer)
3157 {
3158 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
3159 return gl::error(GL_OUT_OF_MEMORY, false);
3160 }
3161
3162 if (!drawBuffer)
3163 {
3164 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
3165 return gl::error(GL_OUT_OF_MEMORY, false);
3166 }
3167
3168 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3169 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3170
Geoff Lang685806d2013-06-12 11:16:36 -04003171 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter,
3172 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003173 {
3174 return false;
3175 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003176 }
3177
3178 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003179}
3180
3181void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3182 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3183{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003184 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003185 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003186
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003187 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3188
3189 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003190 {
3191 gl::Rectangle area;
3192 area.x = x;
3193 area.y = y;
3194 area.width = width;
3195 area.height = height;
3196
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003197 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3198 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003199
3200 colorBufferTexture->Release();
3201 colorBufferTexture = NULL;
3202 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003203}
3204
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003205Image *Renderer11::createImage()
3206{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003207 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003208}
3209
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003210void Renderer11::generateMipmap(Image *dest, Image *src)
3211{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003212 Image11 *dest11 = Image11::makeImage11(dest);
3213 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04003214 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003215}
3216
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003217TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3218{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003219 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3220 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003221}
3222
3223TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3224{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003225 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003226}
3227
3228TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3229{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003230 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003231}
3232
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003233TextureStorage *Renderer11::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3234{
3235 return new TextureStorage11_3D(this, levels, internalformat, usage, width, height, depth);
3236}
3237
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003238TextureStorage *Renderer11::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3239{
3240 return new TextureStorage11_2DArray(this, levels, internalformat, usage, width, height, depth);
3241}
3242
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003243void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3244 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3245 GLint packAlignment, void *pixels)
3246{
3247 D3D11_TEXTURE2D_DESC textureDesc;
3248 texture->GetDesc(&textureDesc);
3249
3250 D3D11_TEXTURE2D_DESC stagingDesc;
3251 stagingDesc.Width = area.width;
3252 stagingDesc.Height = area.height;
3253 stagingDesc.MipLevels = 1;
3254 stagingDesc.ArraySize = 1;
3255 stagingDesc.Format = textureDesc.Format;
3256 stagingDesc.SampleDesc.Count = 1;
3257 stagingDesc.SampleDesc.Quality = 0;
3258 stagingDesc.Usage = D3D11_USAGE_STAGING;
3259 stagingDesc.BindFlags = 0;
3260 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3261 stagingDesc.MiscFlags = 0;
3262
3263 ID3D11Texture2D* stagingTex = NULL;
3264 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3265 if (FAILED(result))
3266 {
3267 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3268 return;
3269 }
3270
3271 ID3D11Texture2D* srcTex = NULL;
3272 if (textureDesc.SampleDesc.Count > 1)
3273 {
3274 D3D11_TEXTURE2D_DESC resolveDesc;
3275 resolveDesc.Width = textureDesc.Width;
3276 resolveDesc.Height = textureDesc.Height;
3277 resolveDesc.MipLevels = 1;
3278 resolveDesc.ArraySize = 1;
3279 resolveDesc.Format = textureDesc.Format;
3280 resolveDesc.SampleDesc.Count = 1;
3281 resolveDesc.SampleDesc.Quality = 0;
3282 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3283 resolveDesc.BindFlags = 0;
3284 resolveDesc.CPUAccessFlags = 0;
3285 resolveDesc.MiscFlags = 0;
3286
3287 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3288 if (FAILED(result))
3289 {
3290 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
3291 stagingTex->Release();
3292 return;
3293 }
3294
3295 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3296 subResource = 0;
3297 }
3298 else
3299 {
3300 srcTex = texture;
3301 srcTex->AddRef();
3302 }
3303
3304 D3D11_BOX srcBox;
3305 srcBox.left = area.x;
3306 srcBox.right = area.x + area.width;
3307 srcBox.top = area.y;
3308 srcBox.bottom = area.y + area.height;
3309 srcBox.front = 0;
3310 srcBox.back = 1;
3311
3312 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3313
3314 srcTex->Release();
3315 srcTex = NULL;
3316
3317 D3D11_MAPPED_SUBRESOURCE mapping;
3318 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3319
3320 unsigned char *source;
3321 int inputPitch;
3322 if (packReverseRowOrder)
3323 {
3324 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3325 inputPitch = -static_cast<int>(mapping.RowPitch);
3326 }
3327 else
3328 {
3329 source = static_cast<unsigned char*>(mapping.pData);
3330 inputPitch = static_cast<int>(mapping.RowPitch);
3331 }
3332
Geoff Lang697ad3e2013-06-04 10:11:28 -04003333 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003334
Jamie Madilld6cb2442013-07-10 15:13:38 -04003335 GLint sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003336 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3337 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3338
3339 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3340
3341 if (sourceFormat == format && sourceType == type)
3342 {
3343 // Direct copy possible
3344 unsigned char *dest = static_cast<unsigned char*>(pixels);
3345 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003346 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003347 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003348 }
3349 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003350 else
3351 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003352 GLint destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
3353 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3354
3355 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type, getCurrentClientVersion());
3356 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003357 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003358 // Fast copy is possible through some special function
3359 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003360 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003361 for (int x = 0; x < area.width; x++)
3362 {
3363 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3364 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3365
3366 fastCopyFunc(src, dest);
3367 }
3368 }
3369 }
3370 else
3371 {
Jamie Madilld6cb2442013-07-10 15:13:38 -04003372 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003373 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3374
3375 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3376 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3377 sizeof(temp) >= sizeof(gl::ColorUI) &&
3378 sizeof(temp) >= sizeof(gl::ColorI));
3379
3380 for (int y = 0; y < area.height; y++)
3381 {
3382 for (int x = 0; x < area.width; x++)
3383 {
3384 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3385 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3386
3387 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3388 // will not allow the copy otherwise.
3389 readFunc(src, temp);
3390 writeFunc(temp, dest);
3391 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003392 }
3393 }
3394 }
3395
3396 mDeviceContext->Unmap(stagingTex, 0);
3397
3398 stagingTex->Release();
3399 stagingTex = NULL;
3400}
3401
Geoff Lang758d5b22013-06-11 11:42:50 -04003402bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang685806d2013-06-12 11:16:36 -04003403 RenderTarget *drawRenderTarget, GLenum filter, bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003404{
Geoff Lang975af372013-06-12 11:19:22 -04003405 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3406 // it should never be the case that both color and depth/stencil need to be blitted at
3407 // at the same time.
3408 ASSERT(colorBlit != (depthBlit || stencilBlit));
3409
Geoff Langc1f51be2013-06-11 11:49:14 -04003410 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003411
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003412 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3413 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003414 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003415 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003416 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003417 }
3418
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003419 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003420 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003421 unsigned int readSubresource = 0;
3422 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003423 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003424 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3425 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
3426 unresolvedResource->Release();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003427
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003428 if (unresolvedTexture)
3429 {
3430 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3431 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003432
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003433 unresolvedTexture->Release();
Geoff Langc1f51be2013-06-11 11:49:14 -04003434
3435 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3436 if (FAILED(result))
3437 {
3438 readTexture->Release();
3439 return gl::error(GL_OUT_OF_MEMORY, false);
3440 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003441 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003442 }
3443 else
3444 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003445 readTexture = readRenderTarget11->getTexture();
3446 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003447 readSRV = readRenderTarget11->getShaderResourceView();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003448 }
3449
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003450 if (!readTexture)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003451 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003452 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003453 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003454 }
3455
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003456 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003457 if (!drawRenderTarget)
3458 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003459 readTexture->Release();
3460 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003461 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003462 }
3463
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003464 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003465 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003466 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3467 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003468
Geoff Langc1f51be2013-06-11 11:49:14 -04003469 bool wholeBufferCopy = readRect.x == 0 && readRect.width == readRenderTarget11->getWidth() &&
3470 readRect.y == 0 && readRect.height == readRenderTarget11->getHeight() &&
3471 drawRect.x == 0 && drawRect.width == drawRenderTarget->getWidth() &&
3472 drawRect.y == 0 && drawRect.height == drawRenderTarget->getHeight();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003473
Geoff Langc1f51be2013-06-11 11:49:14 -04003474 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003475
Geoff Langc1f51be2013-06-11 11:49:14 -04003476 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang685806d2013-06-12 11:16:36 -04003477 !stretchRequired && (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003478 {
3479 D3D11_BOX readBox;
3480 readBox.left = readRect.x;
3481 readBox.right = readRect.x + readRect.width;
3482 readBox.top = readRect.y;
3483 readBox.bottom = readRect.y + readRect.height;
3484 readBox.front = 0;
3485 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003486
Geoff Langc1f51be2013-06-11 11:49:14 -04003487 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3488 // We also require complete framebuffer copies for depth-stencil blit.
3489 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003490
Geoff Langc1f51be2013-06-11 11:49:14 -04003491 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, drawRect.x, drawRect.y, 0,
3492 readTexture, readSubresource, pSrcBox);
3493 result = true;
3494 }
3495 else
3496 {
3497 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
3498 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3499
3500 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
3501 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3502
Geoff Lang975af372013-06-12 11:19:22 -04003503 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003504 {
Geoff Lang975af372013-06-12 11:19:22 -04003505 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
3506 drawTexture, drawSubresource, drawArea, drawSize);
3507 }
3508 else if (depthBlit)
3509 {
3510 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize);
3511 }
3512 else if (stencilBlit)
3513 {
3514 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
3515 drawTexture, drawSubresource, drawArea, drawSize);
Geoff Langc1f51be2013-06-11 11:49:14 -04003516 }
3517 else
3518 {
Geoff Lang685806d2013-06-12 11:16:36 -04003519 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Langc1f51be2013-06-11 11:49:14 -04003520 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize, format, filter);
3521 }
3522 }
3523
3524 SafeRelease(readTexture);
3525 SafeRelease(readSRV);
3526 SafeRelease(drawTexture);
3527 SafeRelease(drawRTV);
3528 SafeRelease(drawDSV);
3529
3530 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003531}
3532
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003533ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3534{
3535 D3D11_TEXTURE2D_DESC textureDesc;
3536 source->GetDesc(&textureDesc);
3537
3538 if (textureDesc.SampleDesc.Count > 1)
3539 {
3540 D3D11_TEXTURE2D_DESC resolveDesc;
3541 resolveDesc.Width = textureDesc.Width;
3542 resolveDesc.Height = textureDesc.Height;
3543 resolveDesc.MipLevels = 1;
3544 resolveDesc.ArraySize = 1;
3545 resolveDesc.Format = textureDesc.Format;
3546 resolveDesc.SampleDesc.Count = 1;
3547 resolveDesc.SampleDesc.Quality = 0;
3548 resolveDesc.Usage = textureDesc.Usage;
3549 resolveDesc.BindFlags = textureDesc.BindFlags;
3550 resolveDesc.CPUAccessFlags = 0;
3551 resolveDesc.MiscFlags = 0;
3552
3553 ID3D11Texture2D *resolveTexture = NULL;
3554 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3555 if (FAILED(result))
3556 {
3557 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3558 return NULL;
3559 }
3560
3561 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3562 return resolveTexture;
3563 }
3564 else
3565 {
3566 source->AddRef();
3567 return source;
3568 }
3569}
3570
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003571bool Renderer11::getLUID(LUID *adapterLuid) const
3572{
3573 adapterLuid->HighPart = 0;
3574 adapterLuid->LowPart = 0;
3575
3576 if (!mDxgiAdapter)
3577 {
3578 return false;
3579 }
3580
3581 DXGI_ADAPTER_DESC adapterDesc;
3582 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3583 {
3584 return false;
3585 }
3586
3587 *adapterLuid = adapterDesc.AdapterLuid;
3588 return true;
3589}
3590
Geoff Lang61e49a52013-05-29 10:22:58 -04003591Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3592{
3593 MultisampleSupportInfo supportInfo = { 0 };
3594
3595 UINT formatSupport;
3596 HRESULT result;
3597
3598 result = mDevice->CheckFormatSupport(format, &formatSupport);
3599 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3600 {
3601 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3602 {
3603 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3604 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3605 {
3606 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3607 }
3608 else
3609 {
3610 supportInfo.qualityLevels[i - 1] = 0;
3611 }
3612 }
3613 }
3614
3615 return supportInfo;
3616}
3617
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003618}