blob: 7aee526aab0b39186084ae193c3d4352bdcc9572 [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"
Geoff Langda507fe2013-08-20 12:01:42 -040032#include "libGLESv2/renderer/Clear11.h"
Jamie Madilla21eea32013-09-18 14:36:25 -040033#include "libGLESv2/renderer/PixelTransfer11.h"
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000034
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000035#include "libEGL/Display.h"
36
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000037#ifdef _DEBUG
38// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
39// and conformance tests. to enable all warnings, remove this define.
40#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
41#endif
42
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000043namespace rx
44{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000045static const DXGI_FORMAT RenderTargetFormats[] =
46 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000047 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000048 DXGI_FORMAT_R8G8B8A8_UNORM
49 };
50
51static const DXGI_FORMAT DepthStencilFormats[] =
52 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000053 DXGI_FORMAT_UNKNOWN,
54 DXGI_FORMAT_D24_UNORM_S8_UINT,
55 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000056 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000057
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000058enum
59{
60 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
61};
62
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000063Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
64{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000065 mVertexDataManager = NULL;
66 mIndexDataManager = NULL;
67
daniel@transgaming.comc5114302012-12-20 21:11:36 +000068 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000069 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000070
Geoff Langb86b9792013-06-04 16:32:05 -040071 mBlit = NULL;
Jamie Madilla21eea32013-09-18 14:36:25 -040072 mPixelTransfer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000073
Geoff Langda507fe2013-08-20 12:01:42 -040074 mClear = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000075
daniel@transgaming.combdf787f2013-02-01 03:20:36 +000076 mSyncQuery = NULL;
77
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000078 mD3d11Module = NULL;
79 mDxgiModule = NULL;
80
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000081 mDeviceLost = false;
82
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +000083 mMaxSupportedSamples = 0;
84
daniel@transgaming.com25072f62012-11-28 19:31:32 +000085 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000086 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000087 mDxgiAdapter = NULL;
88 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +000089
90 mDriverConstantBufferVS = NULL;
91 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +000092
93 mBGRATextureSupport = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +000094
95 mIsGeometryShaderActive = false;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000096}
97
98Renderer11::~Renderer11()
99{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000100 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000101}
102
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000103Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
104{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000105 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000106 return static_cast<rx::Renderer11*>(renderer);
107}
108
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000109#ifndef __d3d11_1_h__
110#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
111#endif
112
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000113EGLint Renderer11::initialize()
114{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000115 if (!initializeCompiler())
116 {
117 return EGL_NOT_INITIALIZED;
118 }
119
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000120 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
121 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000122
123 if (mD3d11Module == NULL || mDxgiModule == NULL)
124 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000125 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000126 return EGL_NOT_INITIALIZED;
127 }
128
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000129 // create the D3D11 device
130 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000131 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000132
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000133 if (D3D11CreateDevice == NULL)
134 {
135 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
136 return EGL_NOT_INITIALIZED;
137 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000138
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000139 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000140 {
141 D3D_FEATURE_LEVEL_11_0,
142 D3D_FEATURE_LEVEL_10_1,
143 D3D_FEATURE_LEVEL_10_0,
144 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000145
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000146 HRESULT result = S_OK;
147
148#ifdef _DEBUG
149 result = D3D11CreateDevice(NULL,
150 D3D_DRIVER_TYPE_HARDWARE,
151 NULL,
152 D3D11_CREATE_DEVICE_DEBUG,
153 featureLevels,
154 ArraySize(featureLevels),
155 D3D11_SDK_VERSION,
156 &mDevice,
157 &mFeatureLevel,
158 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000159
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000160 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000161 {
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000162 ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n");
163 }
164
165 if (!mDevice || FAILED(result))
166#endif
167 {
168 result = D3D11CreateDevice(NULL,
169 D3D_DRIVER_TYPE_HARDWARE,
170 NULL,
171 0,
172 featureLevels,
173 ArraySize(featureLevels),
174 D3D11_SDK_VERSION,
175 &mDevice,
176 &mFeatureLevel,
177 &mDeviceContext);
178
179 if (!mDevice || FAILED(result))
180 {
181 ERR("Could not create D3D11 device - aborting!\n");
182 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
183 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000184 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000185
186 IDXGIDevice *dxgiDevice = NULL;
187 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
188
189 if (FAILED(result))
190 {
191 ERR("Could not query DXGI device - aborting!\n");
192 return EGL_NOT_INITIALIZED;
193 }
194
195 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
196
197 if (FAILED(result))
198 {
199 ERR("Could not retrieve DXGI adapter - aborting!\n");
200 return EGL_NOT_INITIALIZED;
201 }
202
Geoff Langea228632013-07-30 15:17:12 -0400203 SafeRelease(dxgiDevice);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000204
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000205 mDxgiAdapter->GetDesc(&mAdapterDescription);
206 memset(mDescription, 0, sizeof(mDescription));
207 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
208
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000209 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
210
211 if (!mDxgiFactory || FAILED(result))
212 {
213 ERR("Could not create DXGI factory - aborting!\n");
214 return EGL_NOT_INITIALIZED;
215 }
216
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000217 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
218#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
219 ID3D11InfoQueue *infoQueue;
220 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
221
222 if (SUCCEEDED(result))
223 {
224 D3D11_MESSAGE_ID hideMessages[] =
225 {
226 D3D11_MESSAGE_ID_DEVICE_OMSETRENDERTARGETS_HAZARD,
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000227 D3D11_MESSAGE_ID_DEVICE_PSSETSHADERRESOURCES_HAZARD,
228 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000229 };
230
231 D3D11_INFO_QUEUE_FILTER filter = {0};
232 filter.DenyList.NumIDs = ArraySize(hideMessages);
233 filter.DenyList.pIDList = hideMessages;
234
235 infoQueue->AddStorageFilterEntries(&filter);
Geoff Langea228632013-07-30 15:17:12 -0400236 SafeRelease(infoQueue);
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000237 }
238#endif
239
Geoff Lang61e49a52013-05-29 10:22:58 -0400240 mMaxSupportedSamples = 0;
241
242 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
243 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000244 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400245 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
246 mMultisampleSupportMap.insert(std::make_pair(*i, support));
247 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000248 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000249
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000250 initializeDevice();
251
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000252 // BGRA texture support is optional in feature levels 10 and 10_1
253 UINT formatSupport;
254 result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport);
255 if (FAILED(result))
256 {
257 ERR("Error checking BGRA format support: 0x%08X", result);
258 }
259 else
260 {
261 const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET);
262 mBGRATextureSupport = (formatSupport & flags) == flags;
263 }
264
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000265 // Check floating point texture support
266 static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE;
267 static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET;
268 static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE;
269
270 DXGI_FORMAT float16Formats[] =
271 {
272 DXGI_FORMAT_R16_FLOAT,
273 DXGI_FORMAT_R16G16_FLOAT,
274 DXGI_FORMAT_R16G16B16A16_FLOAT,
275 };
276
277 DXGI_FORMAT float32Formats[] =
278 {
279 DXGI_FORMAT_R32_FLOAT,
280 DXGI_FORMAT_R32G32_FLOAT,
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000281 DXGI_FORMAT_R32G32B32A32_FLOAT,
282 };
283
284 mFloat16TextureSupport = true;
285 mFloat16FilterSupport = true;
286 mFloat16RenderSupport = true;
287 for (unsigned int i = 0; i < ArraySize(float16Formats); i++)
288 {
289 if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport)))
290 {
291 mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
292 mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
293 mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
294 }
295 else
296 {
297 mFloat16TextureSupport = false;
298 mFloat16RenderSupport = false;
299 mFloat16FilterSupport = false;
300 }
301 }
302
303 mFloat32TextureSupport = true;
304 mFloat32FilterSupport = true;
305 mFloat32RenderSupport = true;
306 for (unsigned int i = 0; i < ArraySize(float32Formats); i++)
307 {
308 if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport)))
309 {
310 mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
311 mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
312 mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
313 }
314 else
315 {
316 mFloat32TextureSupport = false;
317 mFloat32FilterSupport = false;
318 mFloat32RenderSupport = false;
319 }
320 }
321
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +0000322 // Check compressed texture support
323 const unsigned int requiredCompressedTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D;
324
325 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &formatSupport)))
326 {
327 mDXT1TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
328 }
329 else
330 {
331 mDXT1TextureSupport = false;
332 }
333
334 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport)))
335 {
336 mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
337 }
338 else
339 {
340 mDXT3TextureSupport = false;
341 }
342
343 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC5_UNORM, &formatSupport)))
344 {
345 mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
346 }
347 else
348 {
349 mDXT5TextureSupport = false;
350 }
351
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +0000352 // Check depth texture support
353 DXGI_FORMAT depthTextureFormats[] =
354 {
355 DXGI_FORMAT_D16_UNORM,
356 DXGI_FORMAT_D24_UNORM_S8_UINT,
357 };
358
359 static const unsigned int requiredDepthTextureFlags = D3D11_FORMAT_SUPPORT_DEPTH_STENCIL |
360 D3D11_FORMAT_SUPPORT_TEXTURE2D;
361
362 mDepthTextureSupport = true;
363 for (unsigned int i = 0; i < ArraySize(depthTextureFormats); i++)
364 {
365 if (SUCCEEDED(mDevice->CheckFormatSupport(depthTextureFormats[i], &formatSupport)))
366 {
367 mDepthTextureSupport = mDepthTextureSupport && ((formatSupport & requiredDepthTextureFlags) == requiredDepthTextureFlags);
368 }
369 else
370 {
371 mDepthTextureSupport = false;
372 }
373 }
374
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000375 return EGL_SUCCESS;
376}
377
378// do any one-time device initialization
379// NOTE: this is also needed after a device lost/reset
380// to reset the scene status and ensure the default states are reset.
381void Renderer11::initializeDevice()
382{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000383 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000384 mInputLayoutCache.initialize(mDevice, mDeviceContext);
385
386 ASSERT(!mVertexDataManager && !mIndexDataManager);
387 mVertexDataManager = new VertexDataManager(this);
388 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000389
Geoff Langb86b9792013-06-04 16:32:05 -0400390 ASSERT(!mBlit);
391 mBlit = new Blit11(this);
392
Geoff Langda507fe2013-08-20 12:01:42 -0400393 ASSERT(!mClear);
394 mClear = new Clear11(this);
395
Jamie Madilla21eea32013-09-18 14:36:25 -0400396 ASSERT(!mPixelTransfer);
397 mPixelTransfer = new PixelTransfer11(this);
398
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000399 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000400}
401
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000402int Renderer11::generateConfigs(ConfigDesc **configDescList)
403{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000404 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
405 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000406 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
407 int numConfigs = 0;
408
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000409 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000410 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000411 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000412 {
413 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
414
415 UINT formatSupport = 0;
416 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000417
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000418 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
419 {
420 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
421
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000422 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000423
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000424 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
425 {
426 UINT formatSupport = 0;
427 result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
428 depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
429 }
430
431 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000432 {
Jamie Madilld6cb2442013-07-10 15:13:38 -0400433 // FIXME: parse types from context version
434 ASSERT(d3d11_gl::GetInternalFormat(renderTargetFormat, 2) == d3d11_gl::GetInternalFormat(renderTargetFormat, 3));
435 ASSERT(d3d11_gl::GetInternalFormat(depthStencilFormat, 2) == d3d11_gl::GetInternalFormat(depthStencilFormat, 3));
436
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000437 ConfigDesc newConfig;
Jamie Madilld6cb2442013-07-10 15:13:38 -0400438 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat, getCurrentClientVersion());
439 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat, getCurrentClientVersion());
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000440 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
441 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000442 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000443
444 (*configDescList)[numConfigs++] = newConfig;
445 }
446 }
447 }
448 }
449
450 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000451}
452
453void Renderer11::deleteConfigs(ConfigDesc *configDescList)
454{
455 delete [] (configDescList);
456}
457
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000458void Renderer11::sync(bool block)
459{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000460 if (block)
461 {
462 HRESULT result;
463
464 if (!mSyncQuery)
465 {
466 D3D11_QUERY_DESC queryDesc;
467 queryDesc.Query = D3D11_QUERY_EVENT;
468 queryDesc.MiscFlags = 0;
469
470 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
471 ASSERT(SUCCEEDED(result));
472 }
473
474 mDeviceContext->End(mSyncQuery);
475 mDeviceContext->Flush();
476
477 do
478 {
479 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
480
481 // Keep polling, but allow other threads to do something useful first
482 Sleep(0);
483
484 if (testDeviceLost(true))
485 {
486 return;
487 }
488 }
489 while (result == S_FALSE);
490 }
491 else
492 {
493 mDeviceContext->Flush();
494 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000495}
496
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000497SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
498{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000499 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000500}
501
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000502void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
503{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000504 if (type == gl::SAMPLER_PIXEL)
505 {
506 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
507 {
508 ERR("Pixel shader sampler index %i is not valid.", index);
509 return;
510 }
511
512 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
513 {
514 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
515
516 if (!dxSamplerState)
517 {
518 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
519 "sampler state for pixel shaders at slot %i.", index);
520 }
521
522 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
523
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000524 mCurPixelSamplerStates[index] = samplerState;
525 }
526
527 mForceSetPixelSamplerStates[index] = false;
528 }
529 else if (type == gl::SAMPLER_VERTEX)
530 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000531 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000532 {
533 ERR("Vertex shader sampler index %i is not valid.", index);
534 return;
535 }
536
537 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
538 {
539 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
540
541 if (!dxSamplerState)
542 {
543 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
544 "sampler state for vertex shaders at slot %i.", index);
545 }
546
547 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
548
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000549 mCurVertexSamplerStates[index] = samplerState;
550 }
551
552 mForceSetVertexSamplerStates[index] = false;
553 }
554 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000555}
556
557void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
558{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000559 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000560 unsigned int serial = 0;
561 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000562
563 if (texture)
564 {
565 TextureStorageInterface *texStorage = texture->getNativeTexture();
566 if (texStorage)
567 {
568 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
569 textureSRV = storage11->getSRV();
570 }
571
572 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
573 // missing the shader resource view
574 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000575
576 serial = texture->getTextureSerial();
577 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000578 }
579
580 if (type == gl::SAMPLER_PIXEL)
581 {
582 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
583 {
584 ERR("Pixel shader sampler index %i is not valid.", index);
585 return;
586 }
587
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000588 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
589 {
590 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
591 }
592
593 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000594 }
595 else if (type == gl::SAMPLER_VERTEX)
596 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000597 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000598 {
599 ERR("Vertex shader sampler index %i is not valid.", index);
600 return;
601 }
602
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000603 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
604 {
605 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
606 }
607
608 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000609 }
610 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000611}
612
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000613bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
614{
615 // convert buffers to ID3D11Buffer*
616 ID3D11Buffer *vertexConstantBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = { NULL };
617 ID3D11Buffer *pixelConstantBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = { NULL };
618
619 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
620 {
621 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
622 if (uniformBuffer)
623 {
624 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
625 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(GL_UNIFORM_BUFFER);
626
627 if (!constantBuffer)
628 {
629 return false;
630 }
631
Geoff Langc6354ee2013-07-22 10:40:07 -0400632 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
633 {
634 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
635 1, &constantBuffer);
636 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
637 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000638 }
639 }
640
641 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
642 {
643 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
644 if (uniformBuffer)
645 {
646 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
647 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(GL_UNIFORM_BUFFER);
648
649 if (!constantBuffer)
650 {
651 return false;
652 }
653
Geoff Langc6354ee2013-07-22 10:40:07 -0400654 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
655 {
656 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
657 1, &constantBuffer);
658 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
659 }
660
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000661 pixelConstantBuffers[uniformBufferIndex] = constantBuffer;
662 }
663 }
664
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000665 return true;
666}
667
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000668void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000669{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000670 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000671 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000672 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
673 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000674 if (!dxRasterState)
675 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000676 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000677 "rasterizer state.");
678 }
679
680 mDeviceContext->RSSetState(dxRasterState);
681
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000682 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000683 }
684
685 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000686}
687
Geoff Lang2a64ee42013-05-31 11:22:40 -0400688void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000689 unsigned int sampleMask)
690{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000691 if (mForceSetBlendState ||
692 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400693 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000694 sampleMask != mCurSampleMask)
695 {
696 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
697 if (!dxBlendState)
698 {
699 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
700 "blend state.");
701 }
702
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000703 float blendColors[4] = {0.0f};
704 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
705 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
706 {
707 blendColors[0] = blendColor.red;
708 blendColors[1] = blendColor.green;
709 blendColors[2] = blendColor.blue;
710 blendColors[3] = blendColor.alpha;
711 }
712 else
713 {
714 blendColors[0] = blendColor.alpha;
715 blendColors[1] = blendColor.alpha;
716 blendColors[2] = blendColor.alpha;
717 blendColors[3] = blendColor.alpha;
718 }
719
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000720 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
721
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000722 mCurBlendState = blendState;
723 mCurBlendColor = blendColor;
724 mCurSampleMask = sampleMask;
725 }
726
727 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000728}
729
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000730void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000731 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000732{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000733 if (mForceSetDepthStencilState ||
734 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
735 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
736 {
737 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
738 stencilRef != stencilBackRef ||
739 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
740 {
741 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
742 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000743 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000744 }
745
746 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
747 if (!dxDepthStencilState)
748 {
749 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
750 "setting the default depth stencil state.");
751 }
752
753 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
754
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000755 mCurDepthStencilState = depthStencilState;
756 mCurStencilRef = stencilRef;
757 mCurStencilBackRef = stencilBackRef;
758 }
759
760 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000761}
762
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000763void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000764{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000765 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
766 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000767 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000768 if (enabled)
769 {
770 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000771 rect.left = std::max(0, scissor.x);
772 rect.top = std::max(0, scissor.y);
773 rect.right = scissor.x + std::max(0, scissor.width);
774 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000775
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000776 mDeviceContext->RSSetScissorRects(1, &rect);
777 }
778
779 if (enabled != mScissorEnabled)
780 {
781 mForceSetRasterState = true;
782 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000783
784 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000785 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000786 }
787
788 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000789}
790
daniel@transgaming.com12985182012-12-20 20:56:31 +0000791bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000792 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000793{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000794 gl::Rectangle actualViewport = viewport;
795 float actualZNear = gl::clamp01(zNear);
796 float actualZFar = gl::clamp01(zFar);
797 if (ignoreViewport)
798 {
799 actualViewport.x = 0;
800 actualViewport.y = 0;
801 actualViewport.width = mRenderTargetDesc.width;
802 actualViewport.height = mRenderTargetDesc.height;
803 actualZNear = 0.0f;
804 actualZFar = 1.0f;
805 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000806
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000807 // Get D3D viewport bounds, which depends on the feature level
808 const Range& viewportBounds = getViewportBounds();
809
810 // 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 +0000811 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000812 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
813 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
814 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
815 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
816 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
817 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000818 dxViewport.MinDepth = actualZNear;
819 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000820
821 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
822 {
823 return false; // Nothing to render
824 }
825
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000826 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
827 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000828
daniel@transgaming.com53670042012-11-28 20:55:51 +0000829 if (viewportChanged)
830 {
831 mDeviceContext->RSSetViewports(1, &dxViewport);
832
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000833 mCurViewport = actualViewport;
834 mCurNear = actualZNear;
835 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000836
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000837 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
838 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
839 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
840 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000841
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000842 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
843 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000844
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000845 mVertexConstants.depthRange[0] = actualZNear;
846 mVertexConstants.depthRange[1] = actualZFar;
847 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000848
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000849 mPixelConstants.depthRange[0] = actualZNear;
850 mPixelConstants.depthRange[1] = actualZFar;
851 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000852 }
853
854 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000855 return true;
856}
857
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000858bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
859{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000860 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000861
Geoff Lang57e713e2013-07-31 17:01:58 -0400862 GLsizei minCount = 0;
863
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000864 switch (mode)
865 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400866 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
867 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
868 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
869 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
870 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
871 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000872 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400873 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000874 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000875 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000876 }
877
Geoff Lang4c095862013-07-22 10:43:36 -0400878 if (primitiveTopology != mCurrentPrimitiveTopology)
879 {
880 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
881 mCurrentPrimitiveTopology = primitiveTopology;
882 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000883
Geoff Lang57e713e2013-07-31 17:01:58 -0400884 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000885}
886
887bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000888{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000889 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000890 // Also extract the render target dimensions and view
891 unsigned int renderTargetWidth = 0;
892 unsigned int renderTargetHeight = 0;
893 GLenum renderTargetFormat = 0;
894 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
895 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
896 bool missingColorRenderTarget = true;
897
898 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000899 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000900 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
901
902 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000903 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000904 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
905 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
906
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000907 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000908
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000909 if (!colorbuffer)
910 {
911 ERR("render target pointer unexpectedly null.");
912 return false;
913 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000914
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000915 // check for zero-sized default framebuffer, which is a special case.
916 // in this case we do not wish to modify any state and just silently return false.
917 // this will not report any gl error but will cause the calling method to return.
918 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
919 {
920 return false;
921 }
922
923 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
924
925 // Extract the render target dimensions and view
926 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
927 if (!renderTarget)
928 {
929 ERR("render target pointer unexpectedly null.");
930 return false;
931 }
932
933 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
934 if (!framebufferRTVs[colorAttachment])
935 {
936 ERR("render target view pointer unexpectedly null.");
937 return false;
938 }
939
940 if (missingColorRenderTarget)
941 {
942 renderTargetWidth = colorbuffer->getWidth();
943 renderTargetHeight = colorbuffer->getHeight();
944 renderTargetFormat = colorbuffer->getActualFormat();
945 missingColorRenderTarget = false;
946 }
947 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000948 }
949
950 // Get the depth stencil render buffer and serials
951 gl::Renderbuffer *depthStencil = NULL;
952 unsigned int depthbufferSerial = 0;
953 unsigned int stencilbufferSerial = 0;
954 if (framebuffer->getDepthbufferType() != GL_NONE)
955 {
956 depthStencil = framebuffer->getDepthbuffer();
957 if (!depthStencil)
958 {
959 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000960 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000961 return false;
962 }
963
964 depthbufferSerial = depthStencil->getSerial();
965 }
966 else if (framebuffer->getStencilbufferType() != GL_NONE)
967 {
968 depthStencil = framebuffer->getStencilbuffer();
969 if (!depthStencil)
970 {
971 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000972 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000973 return false;
974 }
975
976 stencilbufferSerial = depthStencil->getSerial();
977 }
978
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000979 // Extract the depth stencil sizes and view
980 unsigned int depthSize = 0;
981 unsigned int stencilSize = 0;
982 ID3D11DepthStencilView* framebufferDSV = NULL;
983 if (depthStencil)
984 {
985 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
986 if (!depthStencilRenderTarget)
987 {
988 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000989 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000990 return false;
991 }
992
993 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
994 if (!framebufferDSV)
995 {
996 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000997 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000998 return false;
999 }
1000
1001 // If there is no render buffer, the width, height and format values come from
1002 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001003 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001004 {
1005 renderTargetWidth = depthStencil->getWidth();
1006 renderTargetHeight = depthStencil->getHeight();
1007 renderTargetFormat = depthStencil->getActualFormat();
1008 }
1009
1010 depthSize = depthStencil->getDepthSize();
1011 stencilSize = depthStencil->getStencilSize();
1012 }
1013
1014 // Apply the render target and depth stencil
1015 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001016 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001017 depthbufferSerial != mAppliedDepthbufferSerial ||
1018 stencilbufferSerial != mAppliedStencilbufferSerial)
1019 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001020 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001021
1022 mRenderTargetDesc.width = renderTargetWidth;
1023 mRenderTargetDesc.height = renderTargetHeight;
1024 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001025 mForceSetViewport = true;
1026 mForceSetScissor = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001027
1028 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1029 {
1030 mCurDepthSize = depthSize;
1031 mForceSetRasterState = true;
1032 }
1033
1034 mCurStencilSize = stencilSize;
1035
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001036 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1037 {
1038 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
1039 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001040 mAppliedDepthbufferSerial = depthbufferSerial;
1041 mAppliedStencilbufferSerial = stencilbufferSerial;
1042 mRenderTargetDescInitialized = true;
1043 mDepthStencilInitialized = true;
1044 }
1045
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001046 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001047}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001048
Jamie Madill57a89722013-07-02 11:57:03 -04001049GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001050 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001051{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001052 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001053 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001054 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001055 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001056 return err;
1057 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001058
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001059 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001060}
1061
daniel@transgaming.com31240482012-11-28 21:06:41 +00001062GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001063{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001064 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001065
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001066 if (err == GL_NO_ERROR)
1067 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001068 if (indexInfo->storage)
1069 {
1070 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1071 {
1072 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1073 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1074
shannonwoods@chromium.org675526e2013-05-30 00:04:49 +00001075 mDeviceContext->IASetIndexBuffer(storage->getBuffer(GL_ELEMENT_ARRAY_BUFFER), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001076
1077 mAppliedIBSerial = 0;
1078 mAppliedStorageIBSerial = storage->getSerial();
1079 mAppliedIBOffset = indexInfo->startOffset;
1080 }
1081 }
1082 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001083 {
1084 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1085
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001086 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001087
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001088 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001089 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001090 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001091 }
1092 }
1093
1094 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001095}
1096
1097void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1098{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001099 if (mode == GL_LINE_LOOP)
1100 {
1101 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1102 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001103 else if (mode == GL_TRIANGLE_FAN)
1104 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001105 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001106 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001107 else if (instances > 0)
1108 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001109 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001110 }
1111 else
1112 {
1113 mDeviceContext->Draw(count, 0);
1114 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001115}
1116
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001117void 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 +00001118{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001119 if (mode == GL_LINE_LOOP)
1120 {
1121 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1122 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001123 else if (mode == GL_TRIANGLE_FAN)
1124 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001125 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1126 }
1127 else if (instances > 0)
1128 {
1129 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001130 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001131 else
1132 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001133 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001134 }
1135}
1136
1137void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1138{
1139 // Get the raw indices for an indexed draw
1140 if (type != GL_NONE && elementArrayBuffer)
1141 {
1142 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001143 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001144 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001145 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001146 }
1147
1148 if (!mLineLoopIB)
1149 {
1150 mLineLoopIB = new StreamingIndexBufferInterface(this);
1151 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1152 {
1153 delete mLineLoopIB;
1154 mLineLoopIB = NULL;
1155
1156 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001157 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001158 }
1159 }
1160
Geoff Lang57e713e2013-07-31 17:01:58 -04001161 // Checked by Renderer11::applyPrimitiveType
1162 ASSERT(count >= 0);
1163
1164 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001165 {
1166 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1167 return gl::error(GL_OUT_OF_MEMORY);
1168 }
1169
Geoff Lang57e713e2013-07-31 17:01:58 -04001170 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001171 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1172 {
1173 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001174 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001175 }
1176
1177 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001178 unsigned int offset;
1179 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001180 {
1181 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001182 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001183 }
1184
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001185 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001186 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001187
1188 switch (type)
1189 {
1190 case GL_NONE: // Non-indexed draw
1191 for (int i = 0; i < count; i++)
1192 {
1193 data[i] = i;
1194 }
1195 data[count] = 0;
1196 break;
1197 case GL_UNSIGNED_BYTE:
1198 for (int i = 0; i < count; i++)
1199 {
1200 data[i] = static_cast<const GLubyte*>(indices)[i];
1201 }
1202 data[count] = static_cast<const GLubyte*>(indices)[0];
1203 break;
1204 case GL_UNSIGNED_SHORT:
1205 for (int i = 0; i < count; i++)
1206 {
1207 data[i] = static_cast<const GLushort*>(indices)[i];
1208 }
1209 data[count] = static_cast<const GLushort*>(indices)[0];
1210 break;
1211 case GL_UNSIGNED_INT:
1212 for (int i = 0; i < count; i++)
1213 {
1214 data[i] = static_cast<const GLuint*>(indices)[i];
1215 }
1216 data[count] = static_cast<const GLuint*>(indices)[0];
1217 break;
1218 default: UNREACHABLE();
1219 }
1220
1221 if (!mLineLoopIB->unmapBuffer())
1222 {
1223 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001224 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001225 }
1226
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001227 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001228 {
1229 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1230
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001231 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001232 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001233 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001234 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001235 }
1236
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001237 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001238}
1239
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001240void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001241{
1242 // Get the raw indices for an indexed draw
1243 if (type != GL_NONE && elementArrayBuffer)
1244 {
1245 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001246 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001247 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001248 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001249 }
1250
1251 if (!mTriangleFanIB)
1252 {
1253 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1254 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1255 {
1256 delete mTriangleFanIB;
1257 mTriangleFanIB = NULL;
1258
1259 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001260 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001261 }
1262 }
1263
Geoff Lang57e713e2013-07-31 17:01:58 -04001264 // Checked by Renderer11::applyPrimitiveType
1265 ASSERT(count >= 3);
1266
Geoff Langeadfd572013-07-09 15:55:07 -04001267 const unsigned int numTris = count - 2;
1268
Geoff Lang57e713e2013-07-31 17:01:58 -04001269 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001270 {
1271 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1272 return gl::error(GL_OUT_OF_MEMORY);
1273 }
1274
1275 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001276 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1277 {
1278 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001279 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001280 }
1281
1282 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001283 unsigned int offset;
1284 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001285 {
1286 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001287 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001288 }
1289
1290 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001291 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001292
1293 switch (type)
1294 {
1295 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001296 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001297 {
1298 data[i*3 + 0] = 0;
1299 data[i*3 + 1] = i + 1;
1300 data[i*3 + 2] = i + 2;
1301 }
1302 break;
1303 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001304 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001305 {
1306 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1307 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1308 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1309 }
1310 break;
1311 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001312 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001313 {
1314 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1315 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1316 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1317 }
1318 break;
1319 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001320 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001321 {
1322 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1323 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1324 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1325 }
1326 break;
1327 default: UNREACHABLE();
1328 }
1329
1330 if (!mTriangleFanIB->unmapBuffer())
1331 {
1332 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001333 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001334 }
1335
1336 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1337 {
1338 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1339
1340 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1341 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001342 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001343 mAppliedIBOffset = indexBufferOffset;
1344 }
1345
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001346 if (instances > 0)
1347 {
1348 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1349 }
1350 else
1351 {
1352 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1353 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001354}
1355
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001356void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1357{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001358 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001359 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1360
1361 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001362 {
1363 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1364 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001365
daniel@transgaming.come4991412012-12-20 20:55:34 +00001366 ID3D11VertexShader *vertexShader = NULL;
1367 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001368
daniel@transgaming.come4991412012-12-20 20:55:34 +00001369 ID3D11PixelShader *pixelShader = NULL;
1370 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001371
daniel@transgaming.come4991412012-12-20 20:55:34 +00001372 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1373 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001374
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001375 programBinary->dirtyAllUniforms();
1376
1377 mAppliedProgramBinarySerial = programBinarySerial;
1378 }
1379
1380 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001381 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001382
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001383 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001384 {
1385 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001386 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001387 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1388 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001389 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1390 }
1391 else
1392 {
1393 mDeviceContext->GSSetShader(NULL, NULL, 0);
1394 }
1395
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001396 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001397 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001398}
1399
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001400void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001401{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001402 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1403 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001404
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001405 unsigned int totalRegisterCountVS = 0;
1406 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001407
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001408 bool vertexUniformsDirty = false;
1409 bool pixelUniformsDirty = false;
1410
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001411 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1412 {
1413 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001414
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001415 if (uniform->isReferencedByVertexShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001416 {
1417 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001418 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001419 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001420
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001421 if (uniform->isReferencedByFragmentShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001422 {
1423 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001424 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001425 }
1426 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001427
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001428 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1429 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1430
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001431 float (*mapVS)[4] = NULL;
1432 float (*mapPS)[4] = NULL;
1433
Shannon Woods5ab33c82013-06-26 15:31:09 -04001434 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1435 {
1436 D3D11_MAPPED_SUBRESOURCE map = {0};
1437 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1438 ASSERT(SUCCEEDED(result));
1439 mapVS = (float(*)[4])map.pData;
1440 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001441
Shannon Woods5ab33c82013-06-26 15:31:09 -04001442 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1443 {
1444 D3D11_MAPPED_SUBRESOURCE map = {0};
1445 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1446 ASSERT(SUCCEEDED(result));
1447 mapPS = (float(*)[4])map.pData;
1448 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001449
Jamie Madill5b085dc2013-08-30 13:21:11 -04001450 for (size_t uniformIndex = 0; uniformIndex < uniformArray->size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001451 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001452 gl::Uniform *uniform = (*uniformArray)[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001453
Nicolas Capense6050882013-07-08 10:43:10 -04001454 if (!gl::IsSampler(uniform->type))
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001455 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001456 unsigned int componentCount = (4 - uniform->registerElement);
1457
1458 // we always assume that uniforms from structs are arranged in struct order in our uniforms list. otherwise we would
1459 // overwrite previously written regions of memory.
1460 if (uniformIndex > 0)
1461 {
1462 gl::Uniform *previousUniform = (*uniformArray)[uniformIndex-1];
1463 ASSERT(!uniform->isReferencedByVertexShader() || previousUniform->vsRegisterIndex != uniform->vsRegisterIndex || uniform->registerElement > previousUniform->registerElement);
1464 ASSERT(!uniform->isReferencedByFragmentShader() || previousUniform->psRegisterIndex != uniform->psRegisterIndex || uniform->registerElement > previousUniform->registerElement);
1465 }
1466
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001467 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001468 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001469 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001470 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001471
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001472 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001473 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001474 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001475 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001476 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001477
1478 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001479 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001480
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001481 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001482 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001483 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001484 }
1485
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001486 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001487 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001488 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001489 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001490
1491 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1492 {
1493 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1494 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1495 }
1496
1497 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1498 {
1499 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1500 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1501 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001502
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001503 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001504 if (!mDriverConstantBufferVS)
1505 {
1506 D3D11_BUFFER_DESC constantBufferDescription = {0};
1507 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1508 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1509 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1510 constantBufferDescription.CPUAccessFlags = 0;
1511 constantBufferDescription.MiscFlags = 0;
1512 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001513
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001514 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001515 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001516
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001517 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1518 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001519
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001520 if (!mDriverConstantBufferPS)
1521 {
1522 D3D11_BUFFER_DESC constantBufferDescription = {0};
1523 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1524 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1525 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1526 constantBufferDescription.CPUAccessFlags = 0;
1527 constantBufferDescription.MiscFlags = 0;
1528 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001529
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001530 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001531 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001532
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001533 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1534 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001535
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001536 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1537 {
1538 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1539 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1540 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001541
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001542 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1543 {
1544 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1545 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1546 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001547
1548 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001549 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1550 {
1551 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1552 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1553 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001554}
1555
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001556void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001557{
Geoff Langda507fe2013-08-20 12:01:42 -04001558 mClear->clearFramebuffer(clearParams, frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001559}
1560
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001561void Renderer11::markAllStateDirty()
1562{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001563 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1564 {
1565 mAppliedRenderTargetSerials[rtIndex] = 0;
1566 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001567 mAppliedDepthbufferSerial = 0;
1568 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001569 mDepthStencilInitialized = false;
1570 mRenderTargetDescInitialized = false;
1571
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001572 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001573 {
1574 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001575 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001576 }
1577 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1578 {
1579 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001580 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001581 }
1582
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001583 mForceSetBlendState = true;
1584 mForceSetRasterState = true;
1585 mForceSetDepthStencilState = true;
1586 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001587 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001588
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001589 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001590 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001591 mAppliedIBOffset = 0;
1592
daniel@transgaming.come4991412012-12-20 20:55:34 +00001593 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001594 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1595 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001596
1597 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001598
1599 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1600 {
1601 mCurrentConstantBufferVS[i] = -1;
1602 mCurrentConstantBufferPS[i] = -1;
1603 }
1604
1605 mCurrentVertexConstantBuffer = NULL;
1606 mCurrentPixelConstantBuffer = NULL;
1607 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001608
1609 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001610}
1611
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001612void Renderer11::releaseDeviceResources()
1613{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001614 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001615 mInputLayoutCache.clear();
1616
Geoff Langea228632013-07-30 15:17:12 -04001617 SafeDelete(mVertexDataManager);
1618 SafeDelete(mIndexDataManager);
1619 SafeDelete(mLineLoopIB);
1620 SafeDelete(mTriangleFanIB);
1621 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001622 SafeDelete(mClear);
Jamie Madilla21eea32013-09-18 14:36:25 -04001623 SafeDelete(mPixelTransfer);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001624
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001625 SafeRelease(mDriverConstantBufferVS);
1626 SafeRelease(mDriverConstantBufferPS);
1627 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001628}
1629
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001630void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001631{
1632 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001633 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001634}
1635
1636bool Renderer11::isDeviceLost()
1637{
1638 return mDeviceLost;
1639}
1640
1641// set notify to true to broadcast a message to all contexts of the device loss
1642bool Renderer11::testDeviceLost(bool notify)
1643{
1644 bool isLost = false;
1645
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001646 // GetRemovedReason is used to test if the device is removed
1647 HRESULT result = mDevice->GetDeviceRemovedReason();
1648 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001649
1650 if (isLost)
1651 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001652 // Log error if this is a new device lost event
1653 if (mDeviceLost == false)
1654 {
1655 ERR("The D3D11 device was removed: 0x%08X", result);
1656 }
1657
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001658 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001659 // we'll probably get this done again by notifyDeviceLost
1660 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001661 // Note that we don't want to clear the device loss status here
1662 // -- this needs to be done by resetDevice
1663 mDeviceLost = true;
1664 if (notify)
1665 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001666 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001667 }
1668 }
1669
1670 return isLost;
1671}
1672
1673bool Renderer11::testDeviceResettable()
1674{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001675 // determine if the device is resettable by creating a dummy device
1676 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001677
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001678 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001679 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001680 return false;
1681 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001682
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001683 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001684 {
1685 D3D_FEATURE_LEVEL_11_0,
1686 D3D_FEATURE_LEVEL_10_1,
1687 D3D_FEATURE_LEVEL_10_0,
1688 };
1689
1690 ID3D11Device* dummyDevice;
1691 D3D_FEATURE_LEVEL dummyFeatureLevel;
1692 ID3D11DeviceContext* dummyContext;
1693
1694 HRESULT result = D3D11CreateDevice(NULL,
1695 D3D_DRIVER_TYPE_HARDWARE,
1696 NULL,
1697 #if defined(_DEBUG)
1698 D3D11_CREATE_DEVICE_DEBUG,
1699 #else
1700 0,
1701 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001702 featureLevels,
1703 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001704 D3D11_SDK_VERSION,
1705 &dummyDevice,
1706 &dummyFeatureLevel,
1707 &dummyContext);
1708
1709 if (!mDevice || FAILED(result))
1710 {
1711 return false;
1712 }
1713
Geoff Langea228632013-07-30 15:17:12 -04001714 SafeRelease(dummyContext);
1715 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001716
1717 return true;
1718}
1719
1720void Renderer11::release()
1721{
1722 releaseDeviceResources();
1723
Geoff Langea228632013-07-30 15:17:12 -04001724 SafeRelease(mDxgiFactory);
1725 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001726
1727 if (mDeviceContext)
1728 {
1729 mDeviceContext->ClearState();
1730 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001731 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001732 }
1733
Geoff Langea228632013-07-30 15:17:12 -04001734 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001735
1736 if (mD3d11Module)
1737 {
1738 FreeLibrary(mD3d11Module);
1739 mD3d11Module = NULL;
1740 }
1741
1742 if (mDxgiModule)
1743 {
1744 FreeLibrary(mDxgiModule);
1745 mDxgiModule = NULL;
1746 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001747}
1748
1749bool Renderer11::resetDevice()
1750{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001751 // recreate everything
1752 release();
1753 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001754
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001755 if (result != EGL_SUCCESS)
1756 {
1757 ERR("Could not reinitialize D3D11 device: %08X", result);
1758 return false;
1759 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001760
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001761 mDeviceLost = false;
1762
1763 return true;
1764}
1765
1766DWORD Renderer11::getAdapterVendor() const
1767{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001768 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001769}
1770
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001771std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001772{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001773 std::ostringstream rendererString;
1774
1775 rendererString << mDescription;
1776 rendererString << " Direct3D11";
1777
1778 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1779 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1780
1781 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001782}
1783
1784GUID Renderer11::getAdapterIdentifier() const
1785{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001786 // Use the adapter LUID as our adapter ID
1787 // This number is local to a machine is only guaranteed to be unique between restarts
1788 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1789 GUID adapterId = {0};
1790 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1791 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001792}
1793
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001794bool Renderer11::getBGRATextureSupport() const
1795{
1796 return mBGRATextureSupport;
1797}
1798
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001799bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001800{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001801 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001802}
1803
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001804bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001805{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001806 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001807}
1808
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001809bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001810{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001811 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001812}
1813
1814bool Renderer11::getDepthTextureSupport() const
1815{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001816 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001817}
1818
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001819bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001820{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001821 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001822}
1823
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001824bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001825{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001826 return mFloat32FilterSupport;
1827}
1828
1829bool Renderer11::getFloat32TextureRenderingSupport() const
1830{
1831 return mFloat32RenderSupport;
1832}
1833
1834bool Renderer11::getFloat16TextureSupport() const
1835{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001836 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001837}
1838
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001839bool Renderer11::getFloat16TextureFilteringSupport() const
1840{
1841 return mFloat16FilterSupport;
1842}
1843
1844bool Renderer11::getFloat16TextureRenderingSupport() const
1845{
1846 return mFloat16RenderSupport;
1847}
1848
Geoff Langd42cf4e2013-06-05 16:09:17 -04001849bool Renderer11::getRGB565TextureSupport() const
1850{
1851 return false;
1852}
1853
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001854bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001855{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001856 return false;
1857}
1858
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001859bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001860{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001861 return false;
1862}
1863
1864bool Renderer11::getTextureFilterAnisotropySupport() const
1865{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001866 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001867}
1868
1869float Renderer11::getTextureMaxAnisotropy() const
1870{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001871 switch (mFeatureLevel)
1872 {
1873 case D3D_FEATURE_LEVEL_11_0:
1874 return D3D11_MAX_MAXANISOTROPY;
1875 case D3D_FEATURE_LEVEL_10_1:
1876 case D3D_FEATURE_LEVEL_10_0:
1877 return D3D10_MAX_MAXANISOTROPY;
1878 default: UNREACHABLE();
1879 return 0;
1880 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001881}
1882
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001883bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001884{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00001885 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001886}
1887
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001888Range Renderer11::getViewportBounds() const
1889{
1890 switch (mFeatureLevel)
1891 {
1892 case D3D_FEATURE_LEVEL_11_0:
1893 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1894 case D3D_FEATURE_LEVEL_10_1:
1895 case D3D_FEATURE_LEVEL_10_0:
1896 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1897 default: UNREACHABLE();
1898 return Range(0, 0);
1899 }
1900}
1901
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001902unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001903{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001904 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1905 switch (mFeatureLevel)
1906 {
1907 case D3D_FEATURE_LEVEL_11_0:
1908 case D3D_FEATURE_LEVEL_10_1:
1909 case D3D_FEATURE_LEVEL_10_0:
1910 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1911 default: UNREACHABLE();
1912 return 0;
1913 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001914}
1915
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001916unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1917{
1918 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1919}
1920
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001921unsigned int Renderer11::getReservedVertexUniformVectors() const
1922{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001923 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001924}
1925
1926unsigned int Renderer11::getReservedFragmentUniformVectors() const
1927{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001928 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001929}
1930
1931unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001932{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001933 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1934 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1935 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001936}
1937
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001938unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001939{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001940 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1941 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1942 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001943}
1944
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001945unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001946{
1947 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001948 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1949 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001950 switch (mFeatureLevel)
1951 {
1952 case D3D_FEATURE_LEVEL_11_0:
1953 return D3D11_VS_OUTPUT_REGISTER_COUNT;
1954 case D3D_FEATURE_LEVEL_10_1:
1955 case D3D_FEATURE_LEVEL_10_0:
1956 return D3D10_VS_OUTPUT_REGISTER_COUNT;
1957 default: UNREACHABLE();
1958 return 0;
1959 }
1960}
1961
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001962unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
1963{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001964 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1965 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1966
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001967 switch (mFeatureLevel)
1968 {
1969 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001970 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001971 case D3D_FEATURE_LEVEL_10_1:
1972 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001973 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001974 default: UNREACHABLE();
1975 return 0;
1976 }
1977}
1978
1979unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
1980{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001981 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1982 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1983
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001984 switch (mFeatureLevel)
1985 {
1986 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001987 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001988 case D3D_FEATURE_LEVEL_10_1:
1989 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001990 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001991 default: UNREACHABLE();
1992 return 0;
1993 }
1994}
1995
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001996unsigned int Renderer11::getReservedVertexUniformBuffers() const
1997{
1998 // we reserve one buffer for the application uniforms, and one for driver uniforms
1999 return 2;
2000}
2001
2002unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2003{
2004 // we reserve one buffer for the application uniforms, and one for driver uniforms
2005 return 2;
2006}
2007
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002008unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2009{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002010 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2011 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2012
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002013 switch (mFeatureLevel)
2014 {
2015 case D3D_FEATURE_LEVEL_11_0:
2016 return D3D11_SO_BUFFER_SLOT_COUNT;
2017 case D3D_FEATURE_LEVEL_10_1:
2018 case D3D_FEATURE_LEVEL_10_0:
2019 return D3D10_SO_BUFFER_SLOT_COUNT;
2020 default: UNREACHABLE();
2021 return 0;
2022 }
2023}
2024
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002025unsigned int Renderer11::getMaxUniformBufferSize() const
2026{
2027 // Each component is a 4-element vector of 4-byte units (floats)
2028 const unsigned int bytesPerComponent = 4 * sizeof(float);
2029
2030 switch (mFeatureLevel)
2031 {
2032 case D3D_FEATURE_LEVEL_11_0:
2033 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2034 case D3D_FEATURE_LEVEL_10_1:
2035 case D3D_FEATURE_LEVEL_10_0:
2036 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2037 default: UNREACHABLE();
2038 return 0;
2039 }
2040}
2041
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002042bool Renderer11::getNonPower2TextureSupport() const
2043{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002044 switch (mFeatureLevel)
2045 {
2046 case D3D_FEATURE_LEVEL_11_0:
2047 case D3D_FEATURE_LEVEL_10_1:
2048 case D3D_FEATURE_LEVEL_10_0:
2049 return true;
2050 default: UNREACHABLE();
2051 return false;
2052 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002053}
2054
2055bool Renderer11::getOcclusionQuerySupport() const
2056{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002057 switch (mFeatureLevel)
2058 {
2059 case D3D_FEATURE_LEVEL_11_0:
2060 case D3D_FEATURE_LEVEL_10_1:
2061 case D3D_FEATURE_LEVEL_10_0:
2062 return true;
2063 default: UNREACHABLE();
2064 return false;
2065 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002066}
2067
2068bool Renderer11::getInstancingSupport() const
2069{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002070 switch (mFeatureLevel)
2071 {
2072 case D3D_FEATURE_LEVEL_11_0:
2073 case D3D_FEATURE_LEVEL_10_1:
2074 case D3D_FEATURE_LEVEL_10_0:
2075 return true;
2076 default: UNREACHABLE();
2077 return false;
2078 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002079}
2080
2081bool Renderer11::getShareHandleSupport() const
2082{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002083 // We only currently support share handles with BGRA surfaces, because
2084 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002085 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002086 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002087}
2088
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002089bool Renderer11::getDerivativeInstructionSupport() const
2090{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002091 switch (mFeatureLevel)
2092 {
2093 case D3D_FEATURE_LEVEL_11_0:
2094 case D3D_FEATURE_LEVEL_10_1:
2095 case D3D_FEATURE_LEVEL_10_0:
2096 return true;
2097 default: UNREACHABLE();
2098 return false;
2099 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002100}
2101
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002102bool Renderer11::getPostSubBufferSupport() const
2103{
2104 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2105 return false;
2106}
2107
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002108int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002109{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002110 switch (mFeatureLevel)
2111 {
2112 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002113 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002114 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2115 default: UNREACHABLE(); return 0;
2116 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002117}
2118
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002119int Renderer11::getMinorShaderModel() const
2120{
2121 switch (mFeatureLevel)
2122 {
2123 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2124 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2125 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2126 default: UNREACHABLE(); return 0;
2127 }
2128}
2129
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002130float Renderer11::getMaxPointSize() const
2131{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002132 // choose a reasonable maximum. we enforce this in the shader.
2133 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2134 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002135}
2136
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002137int Renderer11::getMaxViewportDimension() const
2138{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002139 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2140 // In our case return the maximum texture size, which is the maximum render buffer size.
2141 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2142 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2143
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002144 switch (mFeatureLevel)
2145 {
2146 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002147 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002148 case D3D_FEATURE_LEVEL_10_1:
2149 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002150 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002151 default: UNREACHABLE();
2152 return 0;
2153 }
2154}
2155
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002156int Renderer11::getMaxTextureWidth() const
2157{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002158 switch (mFeatureLevel)
2159 {
2160 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2161 case D3D_FEATURE_LEVEL_10_1:
2162 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2163 default: UNREACHABLE(); return 0;
2164 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002165}
2166
2167int Renderer11::getMaxTextureHeight() const
2168{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002169 switch (mFeatureLevel)
2170 {
2171 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2172 case D3D_FEATURE_LEVEL_10_1:
2173 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2174 default: UNREACHABLE(); return 0;
2175 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002176}
2177
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002178int Renderer11::getMaxTextureDepth() const
2179{
2180 switch (mFeatureLevel)
2181 {
2182 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2183 case D3D_FEATURE_LEVEL_10_1:
2184 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2185 default: UNREACHABLE(); return 0;
2186 }
2187}
2188
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002189int Renderer11::getMaxTextureArrayLayers() const
2190{
2191 switch (mFeatureLevel)
2192 {
2193 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2194 case D3D_FEATURE_LEVEL_10_1:
2195 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2196 default: UNREACHABLE(); return 0;
2197 }
2198}
2199
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002200bool Renderer11::get32BitIndexSupport() const
2201{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002202 switch (mFeatureLevel)
2203 {
2204 case D3D_FEATURE_LEVEL_11_0:
2205 case D3D_FEATURE_LEVEL_10_1:
2206 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2207 default: UNREACHABLE(); return false;
2208 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002209}
2210
2211int Renderer11::getMinSwapInterval() const
2212{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002213 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002214}
2215
2216int Renderer11::getMaxSwapInterval() const
2217{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002218 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002219}
2220
2221int Renderer11::getMaxSupportedSamples() const
2222{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002223 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002224}
2225
Geoff Lang0e120e32013-05-29 10:23:55 -04002226GLsizei Renderer11::getMaxSupportedFormatSamples(GLint internalFormat) const
2227{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002228 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002229 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2230 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2231}
2232
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002233GLsizei Renderer11::getNumSampleCounts(GLint internalFormat) const
2234{
2235 unsigned int numCounts = 0;
2236
2237 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002238 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2239 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002240 {
2241 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2242 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2243
2244 if (iter != mMultisampleSupportMap.end())
2245 {
2246 const MultisampleSupportInfo& info = iter->second;
2247 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2248 {
2249 if (info.qualityLevels[i] > 0)
2250 {
2251 numCounts++;
2252 }
2253 }
2254 }
2255 }
2256
2257 return numCounts;
2258}
2259
2260void Renderer11::getSampleCounts(GLint internalFormat, GLsizei bufSize, GLint *params) const
2261{
2262 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002263 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2264 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2265 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002266 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002267 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002268
2269 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2270 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2271
2272 if (iter != mMultisampleSupportMap.end())
2273 {
2274 const MultisampleSupportInfo& info = iter->second;
2275 int bufPos = 0;
2276 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2277 {
2278 if (info.qualityLevels[i] > 0)
2279 {
2280 params[bufPos++] = i + 1;
2281 }
2282 }
2283 }
2284}
2285
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002286int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2287{
2288 if (requested == 0)
2289 {
2290 return 0;
2291 }
2292
2293 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2294 if (iter != mMultisampleSupportMap.end())
2295 {
2296 const MultisampleSupportInfo& info = iter->second;
2297 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2298 {
2299 if (info.qualityLevels[i] > 0)
2300 {
2301 return i + 1;
2302 }
2303 }
2304 }
2305
2306 return -1;
2307}
2308
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002309unsigned int Renderer11::getMaxRenderTargets() const
2310{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002311 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2312 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2313
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002314 switch (mFeatureLevel)
2315 {
2316 case D3D_FEATURE_LEVEL_11_0:
2317 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2318 case D3D_FEATURE_LEVEL_10_1:
2319 case D3D_FEATURE_LEVEL_10_0:
2320 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2321 default:
2322 UNREACHABLE();
2323 return 1;
2324 }
2325}
2326
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002327bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002328{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002329 if (source && dest)
2330 {
2331 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2332 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2333
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002334 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002335 return true;
2336 }
2337
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002338 return false;
2339}
2340
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002341bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002342{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002343 if (source && dest)
2344 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002345 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2346 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002347
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002348 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002349 return true;
2350 }
2351
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002352 return false;
2353}
2354
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002355bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2356{
2357 if (source && dest)
2358 {
2359 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2360 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2361
2362 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2363 return true;
2364 }
2365
2366 return false;
2367}
2368
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002369bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2370{
2371 if (source && dest)
2372 {
2373 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2374 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2375
2376 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2377 return true;
2378 }
2379
2380 return false;
2381}
2382
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002383bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002384 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002385{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002386 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002387 if (!colorbuffer)
2388 {
2389 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002390 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002391 }
2392
2393 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2394 if (!sourceRenderTarget)
2395 {
2396 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002397 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002398 }
2399
2400 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2401 if (!source)
2402 {
2403 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002404 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002405 }
2406
2407 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2408 if (!storage11)
2409 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002410 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002411 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002412 }
2413
2414 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2415 if (!destRenderTarget)
2416 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002417 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002418 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002419 }
2420
2421 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2422 if (!dest)
2423 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002424 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002425 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002426 }
2427
Geoff Langb86b9792013-06-04 16:32:05 -04002428 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2429 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002430
Geoff Langb86b9792013-06-04 16:32:05 -04002431 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2432 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002433
Geoff Langb86b9792013-06-04 16:32:05 -04002434 // Use nearest filtering because source and destination are the same size for the direct
2435 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002436 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002437 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002438
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002439 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002440}
2441
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002442bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002443 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002444{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002445 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002446 if (!colorbuffer)
2447 {
2448 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002449 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002450 }
2451
2452 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2453 if (!sourceRenderTarget)
2454 {
2455 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002456 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002457 }
2458
2459 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2460 if (!source)
2461 {
2462 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002463 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002464 }
2465
2466 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2467 if (!storage11)
2468 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002469 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002470 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002471 }
2472
Nicolas Capensb13f8662013-06-04 13:30:19 -04002473 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002474 if (!destRenderTarget)
2475 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002476 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002477 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002478 }
2479
2480 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2481 if (!dest)
2482 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002483 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002484 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002485 }
2486
Geoff Langb86b9792013-06-04 16:32:05 -04002487 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2488 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002489
Geoff Langb86b9792013-06-04 16:32:05 -04002490 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2491 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002492
Geoff Langb86b9792013-06-04 16:32:05 -04002493 // Use nearest filtering because source and destination are the same size for the direct
2494 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002495 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002496 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002497
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002498 return ret;
2499}
2500
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002501bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2502 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2503{
2504 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2505 if (!colorbuffer)
2506 {
2507 ERR("Failed to retrieve the color buffer from the frame buffer.");
2508 return gl::error(GL_OUT_OF_MEMORY, false);
2509 }
2510
2511 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2512 if (!sourceRenderTarget)
2513 {
2514 ERR("Failed to retrieve the render target from the frame buffer.");
2515 return gl::error(GL_OUT_OF_MEMORY, false);
2516 }
2517
2518 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2519 if (!source)
2520 {
2521 ERR("Failed to retrieve the render target view from the render target.");
2522 return gl::error(GL_OUT_OF_MEMORY, false);
2523 }
2524
2525 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2526 if (!storage11)
2527 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002528 ERR("Failed to retrieve the texture storage from the destination.");
2529 return gl::error(GL_OUT_OF_MEMORY, false);
2530 }
2531
2532 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2533 if (!destRenderTarget)
2534 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002535 ERR("Failed to retrieve the render target from the destination storage.");
2536 return gl::error(GL_OUT_OF_MEMORY, false);
2537 }
2538
2539 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2540 if (!dest)
2541 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002542 ERR("Failed to retrieve the render target view from the destination render target.");
2543 return gl::error(GL_OUT_OF_MEMORY, false);
2544 }
2545
Geoff Langb86b9792013-06-04 16:32:05 -04002546 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2547 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002548
Geoff Langb86b9792013-06-04 16:32:05 -04002549 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2550 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002551
Geoff Langb86b9792013-06-04 16:32:05 -04002552 // Use nearest filtering because source and destination are the same size for the direct
2553 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002554 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002555 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002556
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002557 return ret;
2558}
2559
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002560bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2561 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2562{
2563 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2564 if (!colorbuffer)
2565 {
2566 ERR("Failed to retrieve the color buffer from the frame buffer.");
2567 return gl::error(GL_OUT_OF_MEMORY, false);
2568 }
2569
2570 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2571 if (!sourceRenderTarget)
2572 {
2573 ERR("Failed to retrieve the render target from the frame buffer.");
2574 return gl::error(GL_OUT_OF_MEMORY, false);
2575 }
2576
2577 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2578 if (!source)
2579 {
2580 ERR("Failed to retrieve the render target view from the render target.");
2581 return gl::error(GL_OUT_OF_MEMORY, false);
2582 }
2583
2584 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2585 if (!storage11)
2586 {
Geoff Langea228632013-07-30 15:17:12 -04002587 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002588 ERR("Failed to retrieve the texture storage from the destination.");
2589 return gl::error(GL_OUT_OF_MEMORY, false);
2590 }
2591
2592 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2593 if (!destRenderTarget)
2594 {
Geoff Langea228632013-07-30 15:17:12 -04002595 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002596 ERR("Failed to retrieve the render target from the destination storage.");
2597 return gl::error(GL_OUT_OF_MEMORY, false);
2598 }
2599
2600 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2601 if (!dest)
2602 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002603 ERR("Failed to retrieve the render target view from the destination render target.");
2604 return gl::error(GL_OUT_OF_MEMORY, false);
2605 }
2606
Geoff Langb86b9792013-06-04 16:32:05 -04002607 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2608 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002609
Geoff Langb86b9792013-06-04 16:32:05 -04002610 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2611 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002612
Geoff Langb86b9792013-06-04 16:32:05 -04002613 // Use nearest filtering because source and destination are the same size for the direct
2614 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002615 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002616 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002617
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002618 return ret;
2619}
2620
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002621void Renderer11::unapplyRenderTargets()
2622{
2623 setOneTimeRenderTarget(NULL);
2624}
2625
2626void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2627{
2628 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2629
2630 rtvArray[0] = renderTargetView;
2631
2632 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2633
2634 // Do not preserve the serial for this one-time-use render target
2635 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2636 {
2637 mAppliedRenderTargetSerials[rtIndex] = 0;
2638 }
2639}
2640
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002641RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2642{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002643 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002644 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002645
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002646 if (depth)
2647 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002648 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002649 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
2650 swapChain11->getDepthStencilTexture(), NULL,
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002651 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002652 }
2653 else
2654 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002655 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002656 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002657 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002658 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002659 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002660 }
2661 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002662}
2663
Geoff Langa2d97f12013-06-11 11:44:02 -04002664RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002665{
Geoff Langa2d97f12013-06-11 11:44:02 -04002666 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002667 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002668}
2669
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002670ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002671{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002672 ShaderExecutable11 *executable = NULL;
2673
2674 switch (type)
2675 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002676 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002677 {
2678 ID3D11VertexShader *vshader = NULL;
2679 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2680 ASSERT(SUCCEEDED(result));
2681
2682 if (vshader)
2683 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002684 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002685 }
2686 }
2687 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002688 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002689 {
2690 ID3D11PixelShader *pshader = NULL;
2691 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2692 ASSERT(SUCCEEDED(result));
2693
2694 if (pshader)
2695 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002696 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002697 }
2698 }
2699 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002700 case rx::SHADER_GEOMETRY:
2701 {
2702 ID3D11GeometryShader *gshader = NULL;
2703 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2704 ASSERT(SUCCEEDED(result));
2705
2706 if (gshader)
2707 {
2708 executable = new ShaderExecutable11(function, length, gshader);
2709 }
2710 }
2711 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002712 default:
2713 UNREACHABLE();
2714 break;
2715 }
2716
2717 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002718}
2719
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002720ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002721{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002722 const char *profile = NULL;
2723
2724 switch (type)
2725 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002726 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002727 profile = "vs_4_0";
2728 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002729 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002730 profile = "ps_4_0";
2731 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002732 case rx::SHADER_GEOMETRY:
2733 profile = "gs_4_0";
2734 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002735 default:
2736 UNREACHABLE();
2737 return NULL;
2738 }
2739
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00002740 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002741 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002742 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002743 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002744 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002745
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002746 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002747 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002748
2749 return executable;
2750}
2751
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002752VertexBuffer *Renderer11::createVertexBuffer()
2753{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002754 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002755}
2756
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002757IndexBuffer *Renderer11::createIndexBuffer()
2758{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002759 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002760}
2761
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002762BufferStorage *Renderer11::createBufferStorage()
2763{
2764 return new BufferStorage11(this);
2765}
2766
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002767QueryImpl *Renderer11::createQuery(GLenum type)
2768{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002769 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002770}
2771
2772FenceImpl *Renderer11::createFence()
2773{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002774 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002775}
2776
Jamie Madilla21eea32013-09-18 14:36:25 -04002777bool Renderer11::fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
2778 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
2779{
2780 ASSERT(gl::IsFastCopyBufferToTextureSupported(destinationFormat, getCurrentClientVersion()));
2781 return mPixelTransfer->copyBufferToTexture(unpack, offset, destRenderTarget, destinationFormat, sourcePixelsType, destArea);
2782}
2783
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002784bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002785{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002786 ASSERT(colorbuffer != NULL);
2787
2788 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2789 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002790 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002791 *subresourceIndex = renderTarget->getSubresourceIndex();
2792
2793 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2794 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002795 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002796 ID3D11Resource *textureResource = NULL;
2797 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002798
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002799 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002800 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002801 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002802 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002803
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002804 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002805 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002806 return true;
2807 }
2808 else
2809 {
2810 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2811 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002812 }
2813 }
2814 }
2815 }
2816
2817 return false;
2818}
2819
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002820bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002821 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002822{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002823 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002824 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002825 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002826
2827 if (!readBuffer)
2828 {
2829 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2830 return gl::error(GL_OUT_OF_MEMORY, false);
2831 }
2832
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002833 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002834
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002835 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002836 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002837 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2838 {
2839 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
2840
2841 if (!drawBuffer)
2842 {
2843 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2844 return gl::error(GL_OUT_OF_MEMORY, false);
2845 }
2846
2847 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2848
Geoff Lang125deab2013-08-09 13:34:16 -04002849 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002850 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002851 {
2852 return false;
2853 }
2854 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002855 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002856 }
2857
Geoff Lang685806d2013-06-12 11:16:36 -04002858 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002859 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002860 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
2861 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
2862
2863 if (!readBuffer)
2864 {
2865 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2866 return gl::error(GL_OUT_OF_MEMORY, false);
2867 }
2868
2869 if (!drawBuffer)
2870 {
2871 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2872 return gl::error(GL_OUT_OF_MEMORY, false);
2873 }
2874
2875 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2876 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2877
Geoff Lang125deab2013-08-09 13:34:16 -04002878 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002879 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002880 {
2881 return false;
2882 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002883 }
2884
2885 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002886}
2887
2888void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2889 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2890{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002891 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002892 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002893
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002894 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2895
2896 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002897 {
2898 gl::Rectangle area;
2899 area.x = x;
2900 area.y = y;
2901 area.width = width;
2902 area.height = height;
2903
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002904 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
2905 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002906
Geoff Langea228632013-07-30 15:17:12 -04002907 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002908 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002909}
2910
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002911Image *Renderer11::createImage()
2912{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002913 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002914}
2915
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002916void Renderer11::generateMipmap(Image *dest, Image *src)
2917{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002918 Image11 *dest11 = Image11::makeImage11(dest);
2919 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04002920 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002921}
2922
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002923TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
2924{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002925 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
2926 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002927}
2928
2929TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
2930{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002931 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002932}
2933
2934TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
2935{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002936 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002937}
2938
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00002939TextureStorage *Renderer11::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
2940{
2941 return new TextureStorage11_3D(this, levels, internalformat, usage, width, height, depth);
2942}
2943
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002944TextureStorage *Renderer11::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
2945{
2946 return new TextureStorage11_2DArray(this, levels, internalformat, usage, width, height, depth);
2947}
2948
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002949void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
2950 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
2951 GLint packAlignment, void *pixels)
2952{
2953 D3D11_TEXTURE2D_DESC textureDesc;
2954 texture->GetDesc(&textureDesc);
2955
2956 D3D11_TEXTURE2D_DESC stagingDesc;
2957 stagingDesc.Width = area.width;
2958 stagingDesc.Height = area.height;
2959 stagingDesc.MipLevels = 1;
2960 stagingDesc.ArraySize = 1;
2961 stagingDesc.Format = textureDesc.Format;
2962 stagingDesc.SampleDesc.Count = 1;
2963 stagingDesc.SampleDesc.Quality = 0;
2964 stagingDesc.Usage = D3D11_USAGE_STAGING;
2965 stagingDesc.BindFlags = 0;
2966 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
2967 stagingDesc.MiscFlags = 0;
2968
2969 ID3D11Texture2D* stagingTex = NULL;
2970 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
2971 if (FAILED(result))
2972 {
2973 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
2974 return;
2975 }
2976
2977 ID3D11Texture2D* srcTex = NULL;
2978 if (textureDesc.SampleDesc.Count > 1)
2979 {
2980 D3D11_TEXTURE2D_DESC resolveDesc;
2981 resolveDesc.Width = textureDesc.Width;
2982 resolveDesc.Height = textureDesc.Height;
2983 resolveDesc.MipLevels = 1;
2984 resolveDesc.ArraySize = 1;
2985 resolveDesc.Format = textureDesc.Format;
2986 resolveDesc.SampleDesc.Count = 1;
2987 resolveDesc.SampleDesc.Quality = 0;
2988 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
2989 resolveDesc.BindFlags = 0;
2990 resolveDesc.CPUAccessFlags = 0;
2991 resolveDesc.MiscFlags = 0;
2992
2993 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
2994 if (FAILED(result))
2995 {
2996 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04002997 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002998 return;
2999 }
3000
3001 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3002 subResource = 0;
3003 }
3004 else
3005 {
3006 srcTex = texture;
3007 srcTex->AddRef();
3008 }
3009
3010 D3D11_BOX srcBox;
3011 srcBox.left = area.x;
3012 srcBox.right = area.x + area.width;
3013 srcBox.top = area.y;
3014 srcBox.bottom = area.y + area.height;
3015 srcBox.front = 0;
3016 srcBox.back = 1;
3017
3018 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3019
Geoff Langea228632013-07-30 15:17:12 -04003020 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003021
3022 D3D11_MAPPED_SUBRESOURCE mapping;
3023 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3024
3025 unsigned char *source;
3026 int inputPitch;
3027 if (packReverseRowOrder)
3028 {
3029 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3030 inputPitch = -static_cast<int>(mapping.RowPitch);
3031 }
3032 else
3033 {
3034 source = static_cast<unsigned char*>(mapping.pData);
3035 inputPitch = static_cast<int>(mapping.RowPitch);
3036 }
3037
Geoff Lang697ad3e2013-06-04 10:11:28 -04003038 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003039
Jamie Madilld6cb2442013-07-10 15:13:38 -04003040 GLint sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003041 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3042 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3043
3044 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3045
3046 if (sourceFormat == format && sourceType == type)
3047 {
3048 // Direct copy possible
3049 unsigned char *dest = static_cast<unsigned char*>(pixels);
3050 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003051 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003052 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003053 }
3054 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003055 else
3056 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003057 GLint destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
3058 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3059
3060 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type, getCurrentClientVersion());
3061 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003062 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003063 // Fast copy is possible through some special function
3064 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003065 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003066 for (int x = 0; x < area.width; x++)
3067 {
3068 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3069 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3070
3071 fastCopyFunc(src, dest);
3072 }
3073 }
3074 }
3075 else
3076 {
Jamie Madilld6cb2442013-07-10 15:13:38 -04003077 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003078 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3079
3080 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3081 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3082 sizeof(temp) >= sizeof(gl::ColorUI) &&
3083 sizeof(temp) >= sizeof(gl::ColorI));
3084
3085 for (int y = 0; y < area.height; y++)
3086 {
3087 for (int x = 0; x < area.width; x++)
3088 {
3089 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3090 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3091
3092 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3093 // will not allow the copy otherwise.
3094 readFunc(src, temp);
3095 writeFunc(temp, dest);
3096 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003097 }
3098 }
3099 }
3100
3101 mDeviceContext->Unmap(stagingTex, 0);
3102
Geoff Langea228632013-07-30 15:17:12 -04003103 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003104}
3105
Geoff Lang758d5b22013-06-11 11:42:50 -04003106bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003107 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3108 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003109{
Geoff Lang975af372013-06-12 11:19:22 -04003110 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3111 // it should never be the case that both color and depth/stencil need to be blitted at
3112 // at the same time.
3113 ASSERT(colorBlit != (depthBlit || stencilBlit));
3114
Geoff Langc1f51be2013-06-11 11:49:14 -04003115 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003116
Geoff Lang4d782732013-07-22 10:44:18 -04003117 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3118 if (!drawRenderTarget)
3119 {
3120 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3121 return gl::error(GL_OUT_OF_MEMORY, false);
3122 }
3123
3124 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3125 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3126 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3127 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3128
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003129 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3130 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003131 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003132 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003133 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003134 }
3135
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003136 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003137 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003138 unsigned int readSubresource = 0;
3139 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003140 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003141 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3142 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003143
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003144 if (unresolvedTexture)
3145 {
3146 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3147 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003148
Geoff Langea228632013-07-30 15:17:12 -04003149 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003150
3151 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3152 if (FAILED(result))
3153 {
Geoff Langea228632013-07-30 15:17:12 -04003154 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003155 return gl::error(GL_OUT_OF_MEMORY, false);
3156 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003157 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003158 }
3159 else
3160 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003161 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003162 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003163 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003164 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003165 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003166 }
3167
Geoff Lang4d782732013-07-22 10:44:18 -04003168 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003169 {
Geoff Lang4d782732013-07-22 10:44:18 -04003170 SafeRelease(readTexture);
3171 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003172 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003173 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003174 }
3175
Geoff Lang125deab2013-08-09 13:34:16 -04003176 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3177 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3178
3179 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3180
3181 bool wholeBufferCopy = !scissorNeeded &&
3182 readRect.x == 0 && readRect.width == readSize.width &&
3183 readRect.y == 0 && readRect.height == readSize.height &&
3184 drawRect.x == 0 && drawRect.width == drawSize.width &&
3185 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003186
Geoff Langc1f51be2013-06-11 11:49:14 -04003187 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003188
Geoff Lang125deab2013-08-09 13:34:16 -04003189 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || readRect.height < 0;
3190
3191 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3192 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3193 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3194 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3195
3196 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3197 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3198 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3199
Geoff Langc1f51be2013-06-11 11:49:14 -04003200 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003201 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3202 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003203 {
Geoff Lang125deab2013-08-09 13:34:16 -04003204 UINT dstX = drawRect.x;
3205 UINT dstY = drawRect.y;
3206
Geoff Langc1f51be2013-06-11 11:49:14 -04003207 D3D11_BOX readBox;
3208 readBox.left = readRect.x;
3209 readBox.right = readRect.x + readRect.width;
3210 readBox.top = readRect.y;
3211 readBox.bottom = readRect.y + readRect.height;
3212 readBox.front = 0;
3213 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003214
Geoff Lang125deab2013-08-09 13:34:16 -04003215 if (scissorNeeded)
3216 {
3217 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3218 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3219
3220 if (drawRect.x < scissor->x)
3221 {
3222 dstX = scissor->x;
3223 readBox.left += (scissor->x - drawRect.x);
3224 }
3225 if (drawRect.y < scissor->y)
3226 {
3227 dstY = scissor->y;
3228 readBox.top += (scissor->y - drawRect.y);
3229 }
3230 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3231 {
3232 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3233 }
3234 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3235 {
3236 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3237 }
3238 }
3239
Geoff Langc1f51be2013-06-11 11:49:14 -04003240 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3241 // We also require complete framebuffer copies for depth-stencil blit.
3242 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003243
Geoff Lang125deab2013-08-09 13:34:16 -04003244 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003245 readTexture, readSubresource, pSrcBox);
3246 result = true;
3247 }
3248 else
3249 {
3250 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003251 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003252
Geoff Lang975af372013-06-12 11:19:22 -04003253 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003254 {
Geoff Lang975af372013-06-12 11:19:22 -04003255 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003256 drawTexture, drawSubresource, drawArea, drawSize,
3257 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003258 }
3259 else if (depthBlit)
3260 {
Geoff Lang125deab2013-08-09 13:34:16 -04003261 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3262 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003263 }
3264 else if (stencilBlit)
3265 {
3266 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003267 drawTexture, drawSubresource, drawArea, drawSize,
3268 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003269 }
3270 else
3271 {
Geoff Lang685806d2013-06-12 11:16:36 -04003272 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003273 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3274 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003275 }
3276 }
3277
3278 SafeRelease(readTexture);
3279 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003280
3281 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003282}
3283
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003284ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3285{
3286 D3D11_TEXTURE2D_DESC textureDesc;
3287 source->GetDesc(&textureDesc);
3288
3289 if (textureDesc.SampleDesc.Count > 1)
3290 {
3291 D3D11_TEXTURE2D_DESC resolveDesc;
3292 resolveDesc.Width = textureDesc.Width;
3293 resolveDesc.Height = textureDesc.Height;
3294 resolveDesc.MipLevels = 1;
3295 resolveDesc.ArraySize = 1;
3296 resolveDesc.Format = textureDesc.Format;
3297 resolveDesc.SampleDesc.Count = 1;
3298 resolveDesc.SampleDesc.Quality = 0;
3299 resolveDesc.Usage = textureDesc.Usage;
3300 resolveDesc.BindFlags = textureDesc.BindFlags;
3301 resolveDesc.CPUAccessFlags = 0;
3302 resolveDesc.MiscFlags = 0;
3303
3304 ID3D11Texture2D *resolveTexture = NULL;
3305 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3306 if (FAILED(result))
3307 {
3308 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3309 return NULL;
3310 }
3311
3312 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3313 return resolveTexture;
3314 }
3315 else
3316 {
3317 source->AddRef();
3318 return source;
3319 }
3320}
3321
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003322bool Renderer11::getLUID(LUID *adapterLuid) const
3323{
3324 adapterLuid->HighPart = 0;
3325 adapterLuid->LowPart = 0;
3326
3327 if (!mDxgiAdapter)
3328 {
3329 return false;
3330 }
3331
3332 DXGI_ADAPTER_DESC adapterDesc;
3333 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3334 {
3335 return false;
3336 }
3337
3338 *adapterLuid = adapterDesc.AdapterLuid;
3339 return true;
3340}
3341
Geoff Lang61e49a52013-05-29 10:22:58 -04003342Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3343{
3344 MultisampleSupportInfo supportInfo = { 0 };
3345
3346 UINT formatSupport;
3347 HRESULT result;
3348
3349 result = mDevice->CheckFormatSupport(format, &formatSupport);
3350 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3351 {
3352 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3353 {
3354 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3355 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3356 {
3357 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3358 }
3359 else
3360 {
3361 supportInfo.qualityLevels[i - 1] = 0;
3362 }
3363 }
3364 }
3365
3366 return supportInfo;
3367}
3368
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003369}