blob: 63110dbc59c60f7e9a0a379729bd693168da3060 [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"
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000033
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000034#include "libEGL/Display.h"
35
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000036#ifdef _DEBUG
37// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
38// and conformance tests. to enable all warnings, remove this define.
39#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
40#endif
41
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000042namespace rx
43{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000044static const DXGI_FORMAT RenderTargetFormats[] =
45 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000046 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000047 DXGI_FORMAT_R8G8B8A8_UNORM
48 };
49
50static const DXGI_FORMAT DepthStencilFormats[] =
51 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000052 DXGI_FORMAT_UNKNOWN,
53 DXGI_FORMAT_D24_UNORM_S8_UINT,
54 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000055 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000056
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000057enum
58{
59 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
60};
61
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000062Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
63{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000064 mVertexDataManager = NULL;
65 mIndexDataManager = NULL;
66
daniel@transgaming.comc5114302012-12-20 21:11:36 +000067 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000068 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000069
Geoff Langb86b9792013-06-04 16:32:05 -040070 mBlit = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000071
Geoff Langda507fe2013-08-20 12:01:42 -040072 mClear = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000073
daniel@transgaming.combdf787f2013-02-01 03:20:36 +000074 mSyncQuery = NULL;
75
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000076 mD3d11Module = NULL;
77 mDxgiModule = NULL;
78
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000079 mDeviceLost = false;
80
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +000081 mMaxSupportedSamples = 0;
82
daniel@transgaming.com25072f62012-11-28 19:31:32 +000083 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000084 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +000085 mDxgiAdapter = NULL;
86 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +000087
88 mDriverConstantBufferVS = NULL;
89 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +000090
91 mBGRATextureSupport = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +000092
93 mIsGeometryShaderActive = false;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000094}
95
96Renderer11::~Renderer11()
97{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +000098 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000099}
100
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000101Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
102{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000103 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000104 return static_cast<rx::Renderer11*>(renderer);
105}
106
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000107#ifndef __d3d11_1_h__
108#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
109#endif
110
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000111EGLint Renderer11::initialize()
112{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000113 if (!initializeCompiler())
114 {
115 return EGL_NOT_INITIALIZED;
116 }
117
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000118 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
119 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000120
121 if (mD3d11Module == NULL || mDxgiModule == NULL)
122 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000123 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000124 return EGL_NOT_INITIALIZED;
125 }
126
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000127 // create the D3D11 device
128 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000129 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000130
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000131 if (D3D11CreateDevice == NULL)
132 {
133 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
134 return EGL_NOT_INITIALIZED;
135 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000136
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000137 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000138 {
139 D3D_FEATURE_LEVEL_11_0,
140 D3D_FEATURE_LEVEL_10_1,
141 D3D_FEATURE_LEVEL_10_0,
142 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000143
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000144 HRESULT result = S_OK;
145
146#ifdef _DEBUG
147 result = D3D11CreateDevice(NULL,
148 D3D_DRIVER_TYPE_HARDWARE,
149 NULL,
150 D3D11_CREATE_DEVICE_DEBUG,
151 featureLevels,
152 ArraySize(featureLevels),
153 D3D11_SDK_VERSION,
154 &mDevice,
155 &mFeatureLevel,
156 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000157
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000158 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000159 {
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000160 ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n");
161 }
162
163 if (!mDevice || FAILED(result))
164#endif
165 {
166 result = D3D11CreateDevice(NULL,
167 D3D_DRIVER_TYPE_HARDWARE,
168 NULL,
169 0,
170 featureLevels,
171 ArraySize(featureLevels),
172 D3D11_SDK_VERSION,
173 &mDevice,
174 &mFeatureLevel,
175 &mDeviceContext);
176
177 if (!mDevice || FAILED(result))
178 {
179 ERR("Could not create D3D11 device - aborting!\n");
180 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
181 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000182 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000183
184 IDXGIDevice *dxgiDevice = NULL;
185 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
186
187 if (FAILED(result))
188 {
189 ERR("Could not query DXGI device - aborting!\n");
190 return EGL_NOT_INITIALIZED;
191 }
192
193 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
194
195 if (FAILED(result))
196 {
197 ERR("Could not retrieve DXGI adapter - aborting!\n");
198 return EGL_NOT_INITIALIZED;
199 }
200
Geoff Langea228632013-07-30 15:17:12 -0400201 SafeRelease(dxgiDevice);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000202
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000203 mDxgiAdapter->GetDesc(&mAdapterDescription);
204 memset(mDescription, 0, sizeof(mDescription));
205 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
206
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000207 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
208
209 if (!mDxgiFactory || FAILED(result))
210 {
211 ERR("Could not create DXGI factory - aborting!\n");
212 return EGL_NOT_INITIALIZED;
213 }
214
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000215 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
216#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
217 ID3D11InfoQueue *infoQueue;
218 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
219
220 if (SUCCEEDED(result))
221 {
222 D3D11_MESSAGE_ID hideMessages[] =
223 {
224 D3D11_MESSAGE_ID_DEVICE_OMSETRENDERTARGETS_HAZARD,
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000225 D3D11_MESSAGE_ID_DEVICE_PSSETSHADERRESOURCES_HAZARD,
226 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000227 };
228
229 D3D11_INFO_QUEUE_FILTER filter = {0};
230 filter.DenyList.NumIDs = ArraySize(hideMessages);
231 filter.DenyList.pIDList = hideMessages;
232
233 infoQueue->AddStorageFilterEntries(&filter);
Geoff Langea228632013-07-30 15:17:12 -0400234 SafeRelease(infoQueue);
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000235 }
236#endif
237
Geoff Lang61e49a52013-05-29 10:22:58 -0400238 mMaxSupportedSamples = 0;
239
240 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
241 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000242 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400243 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
244 mMultisampleSupportMap.insert(std::make_pair(*i, support));
245 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000246 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000247
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000248 initializeDevice();
249
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000250 // BGRA texture support is optional in feature levels 10 and 10_1
251 UINT formatSupport;
252 result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport);
253 if (FAILED(result))
254 {
255 ERR("Error checking BGRA format support: 0x%08X", result);
256 }
257 else
258 {
259 const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET);
260 mBGRATextureSupport = (formatSupport & flags) == flags;
261 }
262
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000263 // Check floating point texture support
264 static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE;
265 static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET;
266 static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE;
267
268 DXGI_FORMAT float16Formats[] =
269 {
270 DXGI_FORMAT_R16_FLOAT,
271 DXGI_FORMAT_R16G16_FLOAT,
272 DXGI_FORMAT_R16G16B16A16_FLOAT,
273 };
274
275 DXGI_FORMAT float32Formats[] =
276 {
277 DXGI_FORMAT_R32_FLOAT,
278 DXGI_FORMAT_R32G32_FLOAT,
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000279 DXGI_FORMAT_R32G32B32A32_FLOAT,
280 };
281
282 mFloat16TextureSupport = true;
283 mFloat16FilterSupport = true;
284 mFloat16RenderSupport = true;
285 for (unsigned int i = 0; i < ArraySize(float16Formats); i++)
286 {
287 if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport)))
288 {
289 mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
290 mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
291 mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
292 }
293 else
294 {
295 mFloat16TextureSupport = false;
296 mFloat16RenderSupport = false;
297 mFloat16FilterSupport = false;
298 }
299 }
300
301 mFloat32TextureSupport = true;
302 mFloat32FilterSupport = true;
303 mFloat32RenderSupport = true;
304 for (unsigned int i = 0; i < ArraySize(float32Formats); i++)
305 {
306 if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport)))
307 {
308 mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
309 mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
310 mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
311 }
312 else
313 {
314 mFloat32TextureSupport = false;
315 mFloat32FilterSupport = false;
316 mFloat32RenderSupport = false;
317 }
318 }
319
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +0000320 // Check compressed texture support
321 const unsigned int requiredCompressedTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D;
322
323 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &formatSupport)))
324 {
325 mDXT1TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
326 }
327 else
328 {
329 mDXT1TextureSupport = false;
330 }
331
332 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport)))
333 {
334 mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
335 }
336 else
337 {
338 mDXT3TextureSupport = false;
339 }
340
341 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC5_UNORM, &formatSupport)))
342 {
343 mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
344 }
345 else
346 {
347 mDXT5TextureSupport = false;
348 }
349
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +0000350 // Check depth texture support
351 DXGI_FORMAT depthTextureFormats[] =
352 {
353 DXGI_FORMAT_D16_UNORM,
354 DXGI_FORMAT_D24_UNORM_S8_UINT,
355 };
356
357 static const unsigned int requiredDepthTextureFlags = D3D11_FORMAT_SUPPORT_DEPTH_STENCIL |
358 D3D11_FORMAT_SUPPORT_TEXTURE2D;
359
360 mDepthTextureSupport = true;
361 for (unsigned int i = 0; i < ArraySize(depthTextureFormats); i++)
362 {
363 if (SUCCEEDED(mDevice->CheckFormatSupport(depthTextureFormats[i], &formatSupport)))
364 {
365 mDepthTextureSupport = mDepthTextureSupport && ((formatSupport & requiredDepthTextureFlags) == requiredDepthTextureFlags);
366 }
367 else
368 {
369 mDepthTextureSupport = false;
370 }
371 }
372
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000373 return EGL_SUCCESS;
374}
375
376// do any one-time device initialization
377// NOTE: this is also needed after a device lost/reset
378// to reset the scene status and ensure the default states are reset.
379void Renderer11::initializeDevice()
380{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000381 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000382 mInputLayoutCache.initialize(mDevice, mDeviceContext);
383
384 ASSERT(!mVertexDataManager && !mIndexDataManager);
385 mVertexDataManager = new VertexDataManager(this);
386 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000387
Geoff Langb86b9792013-06-04 16:32:05 -0400388 ASSERT(!mBlit);
389 mBlit = new Blit11(this);
390
Geoff Langda507fe2013-08-20 12:01:42 -0400391 ASSERT(!mClear);
392 mClear = new Clear11(this);
393
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000394 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000395}
396
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000397int Renderer11::generateConfigs(ConfigDesc **configDescList)
398{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000399 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
400 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000401 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
402 int numConfigs = 0;
403
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000404 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000405 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000406 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000407 {
408 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
409
410 UINT formatSupport = 0;
411 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000412
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000413 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
414 {
415 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
416
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000417 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000418
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000419 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
420 {
421 UINT formatSupport = 0;
422 result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
423 depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
424 }
425
426 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000427 {
Jamie Madilld6cb2442013-07-10 15:13:38 -0400428 // FIXME: parse types from context version
429 ASSERT(d3d11_gl::GetInternalFormat(renderTargetFormat, 2) == d3d11_gl::GetInternalFormat(renderTargetFormat, 3));
430 ASSERT(d3d11_gl::GetInternalFormat(depthStencilFormat, 2) == d3d11_gl::GetInternalFormat(depthStencilFormat, 3));
431
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000432 ConfigDesc newConfig;
Jamie Madilld6cb2442013-07-10 15:13:38 -0400433 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat, getCurrentClientVersion());
434 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat, getCurrentClientVersion());
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000435 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
436 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000437 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000438
439 (*configDescList)[numConfigs++] = newConfig;
440 }
441 }
442 }
443 }
444
445 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000446}
447
448void Renderer11::deleteConfigs(ConfigDesc *configDescList)
449{
450 delete [] (configDescList);
451}
452
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000453void Renderer11::sync(bool block)
454{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000455 if (block)
456 {
457 HRESULT result;
458
459 if (!mSyncQuery)
460 {
461 D3D11_QUERY_DESC queryDesc;
462 queryDesc.Query = D3D11_QUERY_EVENT;
463 queryDesc.MiscFlags = 0;
464
465 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
466 ASSERT(SUCCEEDED(result));
467 }
468
469 mDeviceContext->End(mSyncQuery);
470 mDeviceContext->Flush();
471
472 do
473 {
474 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
475
476 // Keep polling, but allow other threads to do something useful first
477 Sleep(0);
478
479 if (testDeviceLost(true))
480 {
481 return;
482 }
483 }
484 while (result == S_FALSE);
485 }
486 else
487 {
488 mDeviceContext->Flush();
489 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000490}
491
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000492SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
493{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000494 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000495}
496
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000497void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
498{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000499 if (type == gl::SAMPLER_PIXEL)
500 {
501 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
502 {
503 ERR("Pixel shader sampler index %i is not valid.", index);
504 return;
505 }
506
507 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
508 {
509 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
510
511 if (!dxSamplerState)
512 {
513 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
514 "sampler state for pixel shaders at slot %i.", index);
515 }
516
517 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
518
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000519 mCurPixelSamplerStates[index] = samplerState;
520 }
521
522 mForceSetPixelSamplerStates[index] = false;
523 }
524 else if (type == gl::SAMPLER_VERTEX)
525 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000526 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000527 {
528 ERR("Vertex shader sampler index %i is not valid.", index);
529 return;
530 }
531
532 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
533 {
534 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
535
536 if (!dxSamplerState)
537 {
538 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
539 "sampler state for vertex shaders at slot %i.", index);
540 }
541
542 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
543
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000544 mCurVertexSamplerStates[index] = samplerState;
545 }
546
547 mForceSetVertexSamplerStates[index] = false;
548 }
549 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000550}
551
552void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
553{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000554 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000555 unsigned int serial = 0;
556 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000557
558 if (texture)
559 {
560 TextureStorageInterface *texStorage = texture->getNativeTexture();
561 if (texStorage)
562 {
563 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
564 textureSRV = storage11->getSRV();
565 }
566
567 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
568 // missing the shader resource view
569 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000570
571 serial = texture->getTextureSerial();
572 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000573 }
574
575 if (type == gl::SAMPLER_PIXEL)
576 {
577 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
578 {
579 ERR("Pixel shader sampler index %i is not valid.", index);
580 return;
581 }
582
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000583 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
584 {
585 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
586 }
587
588 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000589 }
590 else if (type == gl::SAMPLER_VERTEX)
591 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000592 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000593 {
594 ERR("Vertex shader sampler index %i is not valid.", index);
595 return;
596 }
597
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000598 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
599 {
600 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
601 }
602
603 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000604 }
605 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000606}
607
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000608bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
609{
610 // convert buffers to ID3D11Buffer*
611 ID3D11Buffer *vertexConstantBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = { NULL };
612 ID3D11Buffer *pixelConstantBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = { NULL };
613
614 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
615 {
616 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
617 if (uniformBuffer)
618 {
619 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
620 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(GL_UNIFORM_BUFFER);
621
622 if (!constantBuffer)
623 {
624 return false;
625 }
626
Geoff Langc6354ee2013-07-22 10:40:07 -0400627 if (mCurrentConstantBufferVS[uniformBufferIndex] != bufferStorage->getSerial())
628 {
629 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers() + uniformBufferIndex,
630 1, &constantBuffer);
631 mCurrentConstantBufferVS[uniformBufferIndex] = bufferStorage->getSerial();
632 }
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000633 }
634 }
635
636 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
637 {
638 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
639 if (uniformBuffer)
640 {
641 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
642 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(GL_UNIFORM_BUFFER);
643
644 if (!constantBuffer)
645 {
646 return false;
647 }
648
Geoff Langc6354ee2013-07-22 10:40:07 -0400649 if (mCurrentConstantBufferPS[uniformBufferIndex] != bufferStorage->getSerial())
650 {
651 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers() + uniformBufferIndex,
652 1, &constantBuffer);
653 mCurrentConstantBufferPS[uniformBufferIndex] = bufferStorage->getSerial();
654 }
655
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000656 pixelConstantBuffers[uniformBufferIndex] = constantBuffer;
657 }
658 }
659
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000660 return true;
661}
662
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000663void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000664{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000665 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000666 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000667 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
668 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000669 if (!dxRasterState)
670 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000671 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000672 "rasterizer state.");
673 }
674
675 mDeviceContext->RSSetState(dxRasterState);
676
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000677 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000678 }
679
680 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000681}
682
Geoff Lang2a64ee42013-05-31 11:22:40 -0400683void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::ColorF &blendColor,
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000684 unsigned int sampleMask)
685{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000686 if (mForceSetBlendState ||
687 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
Geoff Lang2a64ee42013-05-31 11:22:40 -0400688 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::ColorF)) != 0 ||
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000689 sampleMask != mCurSampleMask)
690 {
691 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
692 if (!dxBlendState)
693 {
694 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
695 "blend state.");
696 }
697
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000698 float blendColors[4] = {0.0f};
699 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
700 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
701 {
702 blendColors[0] = blendColor.red;
703 blendColors[1] = blendColor.green;
704 blendColors[2] = blendColor.blue;
705 blendColors[3] = blendColor.alpha;
706 }
707 else
708 {
709 blendColors[0] = blendColor.alpha;
710 blendColors[1] = blendColor.alpha;
711 blendColors[2] = blendColor.alpha;
712 blendColors[3] = blendColor.alpha;
713 }
714
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000715 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
716
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000717 mCurBlendState = blendState;
718 mCurBlendColor = blendColor;
719 mCurSampleMask = sampleMask;
720 }
721
722 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000723}
724
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000725void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000726 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000727{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000728 if (mForceSetDepthStencilState ||
729 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
730 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
731 {
732 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
733 stencilRef != stencilBackRef ||
734 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
735 {
736 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
737 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000738 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000739 }
740
741 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
742 if (!dxDepthStencilState)
743 {
744 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
745 "setting the default depth stencil state.");
746 }
747
748 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
749
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000750 mCurDepthStencilState = depthStencilState;
751 mCurStencilRef = stencilRef;
752 mCurStencilBackRef = stencilBackRef;
753 }
754
755 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000756}
757
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000758void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000759{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000760 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
761 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000762 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000763 if (enabled)
764 {
765 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000766 rect.left = std::max(0, scissor.x);
767 rect.top = std::max(0, scissor.y);
768 rect.right = scissor.x + std::max(0, scissor.width);
769 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000770
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000771 mDeviceContext->RSSetScissorRects(1, &rect);
772 }
773
774 if (enabled != mScissorEnabled)
775 {
776 mForceSetRasterState = true;
777 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000778
779 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000780 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000781 }
782
783 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000784}
785
daniel@transgaming.com12985182012-12-20 20:56:31 +0000786bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000787 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000788{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000789 gl::Rectangle actualViewport = viewport;
790 float actualZNear = gl::clamp01(zNear);
791 float actualZFar = gl::clamp01(zFar);
792 if (ignoreViewport)
793 {
794 actualViewport.x = 0;
795 actualViewport.y = 0;
796 actualViewport.width = mRenderTargetDesc.width;
797 actualViewport.height = mRenderTargetDesc.height;
798 actualZNear = 0.0f;
799 actualZFar = 1.0f;
800 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000801
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000802 // Get D3D viewport bounds, which depends on the feature level
803 const Range& viewportBounds = getViewportBounds();
804
805 // Clamp width and height first to the gl maximum, then clamp further if we extend past the D3D maximum bounds
daniel@transgaming.com53670042012-11-28 20:55:51 +0000806 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000807 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
808 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
809 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
810 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
811 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
812 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000813 dxViewport.MinDepth = actualZNear;
814 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000815
816 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
817 {
818 return false; // Nothing to render
819 }
820
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000821 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
822 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000823
daniel@transgaming.com53670042012-11-28 20:55:51 +0000824 if (viewportChanged)
825 {
826 mDeviceContext->RSSetViewports(1, &dxViewport);
827
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000828 mCurViewport = actualViewport;
829 mCurNear = actualZNear;
830 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000831
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000832 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
833 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
834 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
835 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000836
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000837 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
838 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000839
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000840 mVertexConstants.depthRange[0] = actualZNear;
841 mVertexConstants.depthRange[1] = actualZFar;
842 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000843
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000844 mPixelConstants.depthRange[0] = actualZNear;
845 mPixelConstants.depthRange[1] = actualZFar;
846 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000847 }
848
849 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000850 return true;
851}
852
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000853bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
854{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000855 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000856
Geoff Lang57e713e2013-07-31 17:01:58 -0400857 GLsizei minCount = 0;
858
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000859 switch (mode)
860 {
Geoff Lang57e713e2013-07-31 17:01:58 -0400861 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; minCount = 1; break;
862 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; minCount = 2; break;
863 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
864 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; minCount = 2; break;
865 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
866 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; minCount = 3; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000867 // emulate fans via rewriting index buffer
Geoff Lang57e713e2013-07-31 17:01:58 -0400868 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; minCount = 3; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000869 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000870 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000871 }
872
Geoff Lang4c095862013-07-22 10:43:36 -0400873 if (primitiveTopology != mCurrentPrimitiveTopology)
874 {
875 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
876 mCurrentPrimitiveTopology = primitiveTopology;
877 }
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000878
Geoff Lang57e713e2013-07-31 17:01:58 -0400879 return count >= minCount;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000880}
881
882bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000883{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000884 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000885 // Also extract the render target dimensions and view
886 unsigned int renderTargetWidth = 0;
887 unsigned int renderTargetHeight = 0;
888 GLenum renderTargetFormat = 0;
889 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
890 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
891 bool missingColorRenderTarget = true;
892
893 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000894 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000895 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
896
897 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000898 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000899 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
900 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
901
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000902 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000903
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000904 if (!colorbuffer)
905 {
906 ERR("render target pointer unexpectedly null.");
907 return false;
908 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000909
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000910 // check for zero-sized default framebuffer, which is a special case.
911 // in this case we do not wish to modify any state and just silently return false.
912 // this will not report any gl error but will cause the calling method to return.
913 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
914 {
915 return false;
916 }
917
918 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
919
920 // Extract the render target dimensions and view
921 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
922 if (!renderTarget)
923 {
924 ERR("render target pointer unexpectedly null.");
925 return false;
926 }
927
928 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
929 if (!framebufferRTVs[colorAttachment])
930 {
931 ERR("render target view pointer unexpectedly null.");
932 return false;
933 }
934
935 if (missingColorRenderTarget)
936 {
937 renderTargetWidth = colorbuffer->getWidth();
938 renderTargetHeight = colorbuffer->getHeight();
939 renderTargetFormat = colorbuffer->getActualFormat();
940 missingColorRenderTarget = false;
941 }
942 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000943 }
944
945 // Get the depth stencil render buffer and serials
946 gl::Renderbuffer *depthStencil = NULL;
947 unsigned int depthbufferSerial = 0;
948 unsigned int stencilbufferSerial = 0;
949 if (framebuffer->getDepthbufferType() != GL_NONE)
950 {
951 depthStencil = framebuffer->getDepthbuffer();
952 if (!depthStencil)
953 {
954 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000955 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000956 return false;
957 }
958
959 depthbufferSerial = depthStencil->getSerial();
960 }
961 else if (framebuffer->getStencilbufferType() != GL_NONE)
962 {
963 depthStencil = framebuffer->getStencilbuffer();
964 if (!depthStencil)
965 {
966 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000967 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000968 return false;
969 }
970
971 stencilbufferSerial = depthStencil->getSerial();
972 }
973
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000974 // Extract the depth stencil sizes and view
975 unsigned int depthSize = 0;
976 unsigned int stencilSize = 0;
977 ID3D11DepthStencilView* framebufferDSV = NULL;
978 if (depthStencil)
979 {
980 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
981 if (!depthStencilRenderTarget)
982 {
983 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000984 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000985 return false;
986 }
987
988 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
989 if (!framebufferDSV)
990 {
991 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000992 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000993 return false;
994 }
995
996 // If there is no render buffer, the width, height and format values come from
997 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000998 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000999 {
1000 renderTargetWidth = depthStencil->getWidth();
1001 renderTargetHeight = depthStencil->getHeight();
1002 renderTargetFormat = depthStencil->getActualFormat();
1003 }
1004
1005 depthSize = depthStencil->getDepthSize();
1006 stencilSize = depthStencil->getStencilSize();
1007 }
1008
1009 // Apply the render target and depth stencil
1010 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001011 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001012 depthbufferSerial != mAppliedDepthbufferSerial ||
1013 stencilbufferSerial != mAppliedStencilbufferSerial)
1014 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001015 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001016
1017 mRenderTargetDesc.width = renderTargetWidth;
1018 mRenderTargetDesc.height = renderTargetHeight;
1019 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001020 mForceSetViewport = true;
1021 mForceSetScissor = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001022
1023 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1024 {
1025 mCurDepthSize = depthSize;
1026 mForceSetRasterState = true;
1027 }
1028
1029 mCurStencilSize = stencilSize;
1030
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001031 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1032 {
1033 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
1034 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001035 mAppliedDepthbufferSerial = depthbufferSerial;
1036 mAppliedStencilbufferSerial = stencilbufferSerial;
1037 mRenderTargetDescInitialized = true;
1038 mDepthStencilInitialized = true;
1039 }
1040
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001041 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001042}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001043
Jamie Madill57a89722013-07-02 11:57:03 -04001044GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], gl::VertexAttribCurrentValueData currentValues[],
Jamie Madilla857c362013-07-02 11:57:02 -04001045 GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001046{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001047 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
Jamie Madilla857c362013-07-02 11:57:02 -04001048 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, currentValues, programBinary, first, count, attributes, instances);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001049 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001050 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001051 return err;
1052 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001053
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001054 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001055}
1056
daniel@transgaming.com31240482012-11-28 21:06:41 +00001057GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001058{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001059 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001060
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001061 if (err == GL_NO_ERROR)
1062 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001063 if (indexInfo->storage)
1064 {
1065 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1066 {
1067 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1068 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1069
shannonwoods@chromium.org675526e2013-05-30 00:04:49 +00001070 mDeviceContext->IASetIndexBuffer(storage->getBuffer(GL_ELEMENT_ARRAY_BUFFER), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001071
1072 mAppliedIBSerial = 0;
1073 mAppliedStorageIBSerial = storage->getSerial();
1074 mAppliedIBOffset = indexInfo->startOffset;
1075 }
1076 }
1077 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001078 {
1079 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1080
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001081 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001082
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001083 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001084 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001085 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001086 }
1087 }
1088
1089 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001090}
1091
1092void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1093{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001094 if (mode == GL_LINE_LOOP)
1095 {
1096 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1097 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001098 else if (mode == GL_TRIANGLE_FAN)
1099 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001100 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001101 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001102 else if (instances > 0)
1103 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001104 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001105 }
1106 else
1107 {
1108 mDeviceContext->Draw(count, 0);
1109 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001110}
1111
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001112void 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 +00001113{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001114 if (mode == GL_LINE_LOOP)
1115 {
1116 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1117 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001118 else if (mode == GL_TRIANGLE_FAN)
1119 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001120 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1121 }
1122 else if (instances > 0)
1123 {
1124 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001125 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001126 else
1127 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001128 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001129 }
1130}
1131
1132void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1133{
1134 // Get the raw indices for an indexed draw
1135 if (type != GL_NONE && elementArrayBuffer)
1136 {
1137 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001138 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001139 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001140 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001141 }
1142
1143 if (!mLineLoopIB)
1144 {
1145 mLineLoopIB = new StreamingIndexBufferInterface(this);
1146 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1147 {
1148 delete mLineLoopIB;
1149 mLineLoopIB = NULL;
1150
1151 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001152 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001153 }
1154 }
1155
Geoff Lang57e713e2013-07-31 17:01:58 -04001156 // Checked by Renderer11::applyPrimitiveType
1157 ASSERT(count >= 0);
1158
1159 if (static_cast<unsigned int>(count) + 1 > (std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)))
Geoff Langeadfd572013-07-09 15:55:07 -04001160 {
1161 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP, too many indices required.");
1162 return gl::error(GL_OUT_OF_MEMORY);
1163 }
1164
Geoff Lang57e713e2013-07-31 17:01:58 -04001165 const unsigned int spaceNeeded = (static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001166 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1167 {
1168 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001169 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001170 }
1171
1172 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001173 unsigned int offset;
1174 if (!mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001175 {
1176 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001177 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001178 }
1179
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001180 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001181 unsigned int indexBufferOffset = offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001182
1183 switch (type)
1184 {
1185 case GL_NONE: // Non-indexed draw
1186 for (int i = 0; i < count; i++)
1187 {
1188 data[i] = i;
1189 }
1190 data[count] = 0;
1191 break;
1192 case GL_UNSIGNED_BYTE:
1193 for (int i = 0; i < count; i++)
1194 {
1195 data[i] = static_cast<const GLubyte*>(indices)[i];
1196 }
1197 data[count] = static_cast<const GLubyte*>(indices)[0];
1198 break;
1199 case GL_UNSIGNED_SHORT:
1200 for (int i = 0; i < count; i++)
1201 {
1202 data[i] = static_cast<const GLushort*>(indices)[i];
1203 }
1204 data[count] = static_cast<const GLushort*>(indices)[0];
1205 break;
1206 case GL_UNSIGNED_INT:
1207 for (int i = 0; i < count; i++)
1208 {
1209 data[i] = static_cast<const GLuint*>(indices)[i];
1210 }
1211 data[count] = static_cast<const GLuint*>(indices)[0];
1212 break;
1213 default: UNREACHABLE();
1214 }
1215
1216 if (!mLineLoopIB->unmapBuffer())
1217 {
1218 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001219 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001220 }
1221
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001222 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001223 {
1224 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1225
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001226 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001227 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001228 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001229 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001230 }
1231
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001232 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001233}
1234
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001235void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001236{
1237 // Get the raw indices for an indexed draw
1238 if (type != GL_NONE && elementArrayBuffer)
1239 {
1240 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001241 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001242 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001243 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001244 }
1245
1246 if (!mTriangleFanIB)
1247 {
1248 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1249 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1250 {
1251 delete mTriangleFanIB;
1252 mTriangleFanIB = NULL;
1253
1254 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001255 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001256 }
1257 }
1258
Geoff Lang57e713e2013-07-31 17:01:58 -04001259 // Checked by Renderer11::applyPrimitiveType
1260 ASSERT(count >= 3);
1261
Geoff Langeadfd572013-07-09 15:55:07 -04001262 const unsigned int numTris = count - 2;
1263
Geoff Lang57e713e2013-07-31 17:01:58 -04001264 if (numTris > (std::numeric_limits<unsigned int>::max() / (sizeof(unsigned int) * 3)))
Geoff Langeadfd572013-07-09 15:55:07 -04001265 {
1266 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN, too many indices required.");
1267 return gl::error(GL_OUT_OF_MEMORY);
1268 }
1269
1270 const unsigned int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001271 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1272 {
1273 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001274 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001275 }
1276
1277 void* mappedMemory = NULL;
Geoff Langa36ead42013-08-02 11:54:08 -04001278 unsigned int offset;
1279 if (!mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory, &offset))
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001280 {
1281 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001282 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001283 }
1284
1285 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
Geoff Langa36ead42013-08-02 11:54:08 -04001286 unsigned int indexBufferOffset = offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001287
1288 switch (type)
1289 {
1290 case GL_NONE: // Non-indexed draw
Geoff Langeadfd572013-07-09 15:55:07 -04001291 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001292 {
1293 data[i*3 + 0] = 0;
1294 data[i*3 + 1] = i + 1;
1295 data[i*3 + 2] = i + 2;
1296 }
1297 break;
1298 case GL_UNSIGNED_BYTE:
Geoff Langeadfd572013-07-09 15:55:07 -04001299 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001300 {
1301 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1302 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1303 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1304 }
1305 break;
1306 case GL_UNSIGNED_SHORT:
Geoff Langeadfd572013-07-09 15:55:07 -04001307 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001308 {
1309 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1310 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1311 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1312 }
1313 break;
1314 case GL_UNSIGNED_INT:
Geoff Langeadfd572013-07-09 15:55:07 -04001315 for (unsigned int i = 0; i < numTris; i++)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001316 {
1317 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1318 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1319 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1320 }
1321 break;
1322 default: UNREACHABLE();
1323 }
1324
1325 if (!mTriangleFanIB->unmapBuffer())
1326 {
1327 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001328 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001329 }
1330
1331 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1332 {
1333 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1334
1335 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1336 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001337 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001338 mAppliedIBOffset = indexBufferOffset;
1339 }
1340
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001341 if (instances > 0)
1342 {
1343 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1344 }
1345 else
1346 {
1347 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1348 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001349}
1350
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001351void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1352{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001353 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001354 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1355
1356 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001357 {
1358 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1359 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001360
daniel@transgaming.come4991412012-12-20 20:55:34 +00001361 ID3D11VertexShader *vertexShader = NULL;
1362 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001363
daniel@transgaming.come4991412012-12-20 20:55:34 +00001364 ID3D11PixelShader *pixelShader = NULL;
1365 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001366
daniel@transgaming.come4991412012-12-20 20:55:34 +00001367 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1368 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001369
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001370 programBinary->dirtyAllUniforms();
1371
1372 mAppliedProgramBinarySerial = programBinarySerial;
1373 }
1374
1375 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001376 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001377
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001378 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001379 {
1380 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001381 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001382 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1383 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001384 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1385 }
1386 else
1387 {
1388 mDeviceContext->GSSetShader(NULL, NULL, 0);
1389 }
1390
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001391 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001392 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001393}
1394
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001395void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001396{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001397 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1398 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001399
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001400 unsigned int totalRegisterCountVS = 0;
1401 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001402
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001403 bool vertexUniformsDirty = false;
1404 bool pixelUniformsDirty = false;
1405
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001406 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1407 {
1408 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001409
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001410 if (uniform->isReferencedByVertexShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001411 {
1412 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001413 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001414 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001415
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001416 if (uniform->isReferencedByFragmentShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001417 {
1418 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001419 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001420 }
1421 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001422
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001423 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1424 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1425
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001426 float (*mapVS)[4] = NULL;
1427 float (*mapPS)[4] = NULL;
1428
Shannon Woods5ab33c82013-06-26 15:31:09 -04001429 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1430 {
1431 D3D11_MAPPED_SUBRESOURCE map = {0};
1432 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1433 ASSERT(SUCCEEDED(result));
1434 mapVS = (float(*)[4])map.pData;
1435 }
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001436
Shannon Woods5ab33c82013-06-26 15:31:09 -04001437 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1438 {
1439 D3D11_MAPPED_SUBRESOURCE map = {0};
1440 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1441 ASSERT(SUCCEEDED(result));
1442 mapPS = (float(*)[4])map.pData;
1443 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001444
Jamie Madill5b085dc2013-08-30 13:21:11 -04001445 for (size_t uniformIndex = 0; uniformIndex < uniformArray->size(); uniformIndex++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001446 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001447 gl::Uniform *uniform = (*uniformArray)[uniformIndex];
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001448
Nicolas Capense6050882013-07-08 10:43:10 -04001449 if (!gl::IsSampler(uniform->type))
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001450 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001451 unsigned int componentCount = (4 - uniform->registerElement);
1452
1453 // we always assume that uniforms from structs are arranged in struct order in our uniforms list. otherwise we would
1454 // overwrite previously written regions of memory.
1455 if (uniformIndex > 0)
1456 {
1457 gl::Uniform *previousUniform = (*uniformArray)[uniformIndex-1];
1458 ASSERT(!uniform->isReferencedByVertexShader() || previousUniform->vsRegisterIndex != uniform->vsRegisterIndex || uniform->registerElement > previousUniform->registerElement);
1459 ASSERT(!uniform->isReferencedByFragmentShader() || previousUniform->psRegisterIndex != uniform->psRegisterIndex || uniform->registerElement > previousUniform->registerElement);
1460 }
1461
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001462 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001463 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001464 memcpy(&mapVS[uniform->vsRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001465 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001466
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001467 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001468 {
Jamie Madill5b085dc2013-08-30 13:21:11 -04001469 memcpy(&mapPS[uniform->psRegisterIndex][uniform->registerElement], uniform->data, uniform->registerCount * sizeof(float) * componentCount);
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001470 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001471 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001472
1473 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001474 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001475
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001476 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001477 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001478 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001479 }
1480
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001481 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001482 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001483 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001484 }
Geoff Langc6354ee2013-07-22 10:40:07 -04001485
1486 if (mCurrentVertexConstantBuffer != vertexConstantBuffer)
1487 {
1488 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1489 mCurrentVertexConstantBuffer = vertexConstantBuffer;
1490 }
1491
1492 if (mCurrentPixelConstantBuffer != pixelConstantBuffer)
1493 {
1494 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
1495 mCurrentPixelConstantBuffer = pixelConstantBuffer;
1496 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001497
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001498 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001499 if (!mDriverConstantBufferVS)
1500 {
1501 D3D11_BUFFER_DESC constantBufferDescription = {0};
1502 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1503 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1504 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1505 constantBufferDescription.CPUAccessFlags = 0;
1506 constantBufferDescription.MiscFlags = 0;
1507 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001508
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001509 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001510 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001511
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001512 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1513 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001514
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001515 if (!mDriverConstantBufferPS)
1516 {
1517 D3D11_BUFFER_DESC constantBufferDescription = {0};
1518 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1519 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1520 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1521 constantBufferDescription.CPUAccessFlags = 0;
1522 constantBufferDescription.MiscFlags = 0;
1523 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001524
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001525 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001526 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001527
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001528 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1529 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001530
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001531 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1532 {
1533 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1534 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1535 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001536
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001537 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1538 {
1539 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1540 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1541 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001542
1543 // needed for the point sprite geometry shader
Geoff Langc6354ee2013-07-22 10:40:07 -04001544 if (mCurrentGeometryConstantBuffer != mDriverConstantBufferPS)
1545 {
1546 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
1547 mCurrentGeometryConstantBuffer = mDriverConstantBufferPS;
1548 }
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001549}
1550
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001551void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001552{
Geoff Langda507fe2013-08-20 12:01:42 -04001553 mClear->clearFramebuffer(clearParams, frameBuffer);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001554}
1555
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001556void Renderer11::markAllStateDirty()
1557{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001558 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1559 {
1560 mAppliedRenderTargetSerials[rtIndex] = 0;
1561 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001562 mAppliedDepthbufferSerial = 0;
1563 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001564 mDepthStencilInitialized = false;
1565 mRenderTargetDescInitialized = false;
1566
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001567 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001568 {
1569 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001570 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001571 }
1572 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1573 {
1574 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001575 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001576 }
1577
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001578 mForceSetBlendState = true;
1579 mForceSetRasterState = true;
1580 mForceSetDepthStencilState = true;
1581 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001582 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001583
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001584 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001585 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001586 mAppliedIBOffset = 0;
1587
daniel@transgaming.come4991412012-12-20 20:55:34 +00001588 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001589 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1590 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
Geoff Lang1f53cab2013-07-22 10:37:22 -04001591
1592 mInputLayoutCache.markDirty();
Geoff Langc6354ee2013-07-22 10:40:07 -04001593
1594 for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; i++)
1595 {
1596 mCurrentConstantBufferVS[i] = -1;
1597 mCurrentConstantBufferPS[i] = -1;
1598 }
1599
1600 mCurrentVertexConstantBuffer = NULL;
1601 mCurrentPixelConstantBuffer = NULL;
1602 mCurrentGeometryConstantBuffer = NULL;
Geoff Lang4c095862013-07-22 10:43:36 -04001603
1604 mCurrentPrimitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001605}
1606
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001607void Renderer11::releaseDeviceResources()
1608{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001609 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001610 mInputLayoutCache.clear();
1611
Geoff Langea228632013-07-30 15:17:12 -04001612 SafeDelete(mVertexDataManager);
1613 SafeDelete(mIndexDataManager);
1614 SafeDelete(mLineLoopIB);
1615 SafeDelete(mTriangleFanIB);
1616 SafeDelete(mBlit);
Geoff Langda507fe2013-08-20 12:01:42 -04001617 SafeDelete(mClear);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001618
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001619 SafeRelease(mDriverConstantBufferVS);
1620 SafeRelease(mDriverConstantBufferPS);
1621 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001622}
1623
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001624void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001625{
1626 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001627 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001628}
1629
1630bool Renderer11::isDeviceLost()
1631{
1632 return mDeviceLost;
1633}
1634
1635// set notify to true to broadcast a message to all contexts of the device loss
1636bool Renderer11::testDeviceLost(bool notify)
1637{
1638 bool isLost = false;
1639
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001640 // GetRemovedReason is used to test if the device is removed
1641 HRESULT result = mDevice->GetDeviceRemovedReason();
1642 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001643
1644 if (isLost)
1645 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001646 // Log error if this is a new device lost event
1647 if (mDeviceLost == false)
1648 {
1649 ERR("The D3D11 device was removed: 0x%08X", result);
1650 }
1651
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001652 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001653 // we'll probably get this done again by notifyDeviceLost
1654 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001655 // Note that we don't want to clear the device loss status here
1656 // -- this needs to be done by resetDevice
1657 mDeviceLost = true;
1658 if (notify)
1659 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001660 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001661 }
1662 }
1663
1664 return isLost;
1665}
1666
1667bool Renderer11::testDeviceResettable()
1668{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001669 // determine if the device is resettable by creating a dummy device
1670 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001671
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001672 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001673 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001674 return false;
1675 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001676
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001677 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001678 {
1679 D3D_FEATURE_LEVEL_11_0,
1680 D3D_FEATURE_LEVEL_10_1,
1681 D3D_FEATURE_LEVEL_10_0,
1682 };
1683
1684 ID3D11Device* dummyDevice;
1685 D3D_FEATURE_LEVEL dummyFeatureLevel;
1686 ID3D11DeviceContext* dummyContext;
1687
1688 HRESULT result = D3D11CreateDevice(NULL,
1689 D3D_DRIVER_TYPE_HARDWARE,
1690 NULL,
1691 #if defined(_DEBUG)
1692 D3D11_CREATE_DEVICE_DEBUG,
1693 #else
1694 0,
1695 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001696 featureLevels,
1697 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001698 D3D11_SDK_VERSION,
1699 &dummyDevice,
1700 &dummyFeatureLevel,
1701 &dummyContext);
1702
1703 if (!mDevice || FAILED(result))
1704 {
1705 return false;
1706 }
1707
Geoff Langea228632013-07-30 15:17:12 -04001708 SafeRelease(dummyContext);
1709 SafeRelease(dummyDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001710
1711 return true;
1712}
1713
1714void Renderer11::release()
1715{
1716 releaseDeviceResources();
1717
Geoff Langea228632013-07-30 15:17:12 -04001718 SafeRelease(mDxgiFactory);
1719 SafeRelease(mDxgiAdapter);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001720
1721 if (mDeviceContext)
1722 {
1723 mDeviceContext->ClearState();
1724 mDeviceContext->Flush();
Geoff Langea228632013-07-30 15:17:12 -04001725 SafeRelease(mDeviceContext);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001726 }
1727
Geoff Langea228632013-07-30 15:17:12 -04001728 SafeRelease(mDevice);
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001729
1730 if (mD3d11Module)
1731 {
1732 FreeLibrary(mD3d11Module);
1733 mD3d11Module = NULL;
1734 }
1735
1736 if (mDxgiModule)
1737 {
1738 FreeLibrary(mDxgiModule);
1739 mDxgiModule = NULL;
1740 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001741}
1742
1743bool Renderer11::resetDevice()
1744{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001745 // recreate everything
1746 release();
1747 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001748
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001749 if (result != EGL_SUCCESS)
1750 {
1751 ERR("Could not reinitialize D3D11 device: %08X", result);
1752 return false;
1753 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001754
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001755 mDeviceLost = false;
1756
1757 return true;
1758}
1759
1760DWORD Renderer11::getAdapterVendor() const
1761{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00001762 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001763}
1764
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001765std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001766{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00001767 std::ostringstream rendererString;
1768
1769 rendererString << mDescription;
1770 rendererString << " Direct3D11";
1771
1772 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1773 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
1774
1775 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001776}
1777
1778GUID Renderer11::getAdapterIdentifier() const
1779{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00001780 // Use the adapter LUID as our adapter ID
1781 // This number is local to a machine is only guaranteed to be unique between restarts
1782 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
1783 GUID adapterId = {0};
1784 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
1785 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001786}
1787
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00001788bool Renderer11::getBGRATextureSupport() const
1789{
1790 return mBGRATextureSupport;
1791}
1792
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001793bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001794{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001795 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001796}
1797
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001798bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001799{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001800 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001801}
1802
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001803bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001804{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00001805 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001806}
1807
1808bool Renderer11::getDepthTextureSupport() const
1809{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00001810 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001811}
1812
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001813bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001814{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001815 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001816}
1817
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001818bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001819{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001820 return mFloat32FilterSupport;
1821}
1822
1823bool Renderer11::getFloat32TextureRenderingSupport() const
1824{
1825 return mFloat32RenderSupport;
1826}
1827
1828bool Renderer11::getFloat16TextureSupport() const
1829{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00001830 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001831}
1832
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001833bool Renderer11::getFloat16TextureFilteringSupport() const
1834{
1835 return mFloat16FilterSupport;
1836}
1837
1838bool Renderer11::getFloat16TextureRenderingSupport() const
1839{
1840 return mFloat16RenderSupport;
1841}
1842
Geoff Langd42cf4e2013-06-05 16:09:17 -04001843bool Renderer11::getRGB565TextureSupport() const
1844{
1845 return false;
1846}
1847
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001848bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001849{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001850 return false;
1851}
1852
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001853bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001854{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001855 return false;
1856}
1857
1858bool Renderer11::getTextureFilterAnisotropySupport() const
1859{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001860 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001861}
1862
1863float Renderer11::getTextureMaxAnisotropy() const
1864{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00001865 switch (mFeatureLevel)
1866 {
1867 case D3D_FEATURE_LEVEL_11_0:
1868 return D3D11_MAX_MAXANISOTROPY;
1869 case D3D_FEATURE_LEVEL_10_1:
1870 case D3D_FEATURE_LEVEL_10_0:
1871 return D3D10_MAX_MAXANISOTROPY;
1872 default: UNREACHABLE();
1873 return 0;
1874 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001875}
1876
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00001877bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001878{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00001879 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001880}
1881
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001882Range Renderer11::getViewportBounds() const
1883{
1884 switch (mFeatureLevel)
1885 {
1886 case D3D_FEATURE_LEVEL_11_0:
1887 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
1888 case D3D_FEATURE_LEVEL_10_1:
1889 case D3D_FEATURE_LEVEL_10_0:
1890 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
1891 default: UNREACHABLE();
1892 return Range(0, 0);
1893 }
1894}
1895
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001896unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001897{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001898 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
1899 switch (mFeatureLevel)
1900 {
1901 case D3D_FEATURE_LEVEL_11_0:
1902 case D3D_FEATURE_LEVEL_10_1:
1903 case D3D_FEATURE_LEVEL_10_0:
1904 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
1905 default: UNREACHABLE();
1906 return 0;
1907 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001908}
1909
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001910unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
1911{
1912 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
1913}
1914
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001915unsigned int Renderer11::getReservedVertexUniformVectors() const
1916{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001917 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001918}
1919
1920unsigned int Renderer11::getReservedFragmentUniformVectors() const
1921{
Shannon Woods5ab33c82013-06-26 15:31:09 -04001922 return 0; // Driver uniforms are stored in a separate constant buffer
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001923}
1924
1925unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001926{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001927 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1928 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1929 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001930}
1931
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001932unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001933{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00001934 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
1935 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
1936 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00001937}
1938
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001939unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001940{
1941 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00001942 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
1943 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00001944 switch (mFeatureLevel)
1945 {
1946 case D3D_FEATURE_LEVEL_11_0:
1947 return D3D11_VS_OUTPUT_REGISTER_COUNT;
1948 case D3D_FEATURE_LEVEL_10_1:
1949 case D3D_FEATURE_LEVEL_10_0:
1950 return D3D10_VS_OUTPUT_REGISTER_COUNT;
1951 default: UNREACHABLE();
1952 return 0;
1953 }
1954}
1955
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001956unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
1957{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001958 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1959 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1960
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001961 switch (mFeatureLevel)
1962 {
1963 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001964 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001965 case D3D_FEATURE_LEVEL_10_1:
1966 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001967 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001968 default: UNREACHABLE();
1969 return 0;
1970 }
1971}
1972
1973unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
1974{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00001975 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
1976 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
1977
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001978 switch (mFeatureLevel)
1979 {
1980 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001981 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001982 case D3D_FEATURE_LEVEL_10_1:
1983 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001984 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00001985 default: UNREACHABLE();
1986 return 0;
1987 }
1988}
1989
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00001990unsigned int Renderer11::getReservedVertexUniformBuffers() const
1991{
1992 // we reserve one buffer for the application uniforms, and one for driver uniforms
1993 return 2;
1994}
1995
1996unsigned int Renderer11::getReservedFragmentUniformBuffers() const
1997{
1998 // we reserve one buffer for the application uniforms, and one for driver uniforms
1999 return 2;
2000}
2001
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002002unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2003{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002004 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2005 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2006
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002007 switch (mFeatureLevel)
2008 {
2009 case D3D_FEATURE_LEVEL_11_0:
2010 return D3D11_SO_BUFFER_SLOT_COUNT;
2011 case D3D_FEATURE_LEVEL_10_1:
2012 case D3D_FEATURE_LEVEL_10_0:
2013 return D3D10_SO_BUFFER_SLOT_COUNT;
2014 default: UNREACHABLE();
2015 return 0;
2016 }
2017}
2018
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002019unsigned int Renderer11::getMaxUniformBufferSize() const
2020{
2021 // Each component is a 4-element vector of 4-byte units (floats)
2022 const unsigned int bytesPerComponent = 4 * sizeof(float);
2023
2024 switch (mFeatureLevel)
2025 {
2026 case D3D_FEATURE_LEVEL_11_0:
2027 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2028 case D3D_FEATURE_LEVEL_10_1:
2029 case D3D_FEATURE_LEVEL_10_0:
2030 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2031 default: UNREACHABLE();
2032 return 0;
2033 }
2034}
2035
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002036bool Renderer11::getNonPower2TextureSupport() const
2037{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002038 switch (mFeatureLevel)
2039 {
2040 case D3D_FEATURE_LEVEL_11_0:
2041 case D3D_FEATURE_LEVEL_10_1:
2042 case D3D_FEATURE_LEVEL_10_0:
2043 return true;
2044 default: UNREACHABLE();
2045 return false;
2046 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002047}
2048
2049bool Renderer11::getOcclusionQuerySupport() const
2050{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002051 switch (mFeatureLevel)
2052 {
2053 case D3D_FEATURE_LEVEL_11_0:
2054 case D3D_FEATURE_LEVEL_10_1:
2055 case D3D_FEATURE_LEVEL_10_0:
2056 return true;
2057 default: UNREACHABLE();
2058 return false;
2059 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002060}
2061
2062bool Renderer11::getInstancingSupport() const
2063{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002064 switch (mFeatureLevel)
2065 {
2066 case D3D_FEATURE_LEVEL_11_0:
2067 case D3D_FEATURE_LEVEL_10_1:
2068 case D3D_FEATURE_LEVEL_10_0:
2069 return true;
2070 default: UNREACHABLE();
2071 return false;
2072 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002073}
2074
2075bool Renderer11::getShareHandleSupport() const
2076{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002077 // We only currently support share handles with BGRA surfaces, because
2078 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002079 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002080 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002081}
2082
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002083bool Renderer11::getDerivativeInstructionSupport() const
2084{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002085 switch (mFeatureLevel)
2086 {
2087 case D3D_FEATURE_LEVEL_11_0:
2088 case D3D_FEATURE_LEVEL_10_1:
2089 case D3D_FEATURE_LEVEL_10_0:
2090 return true;
2091 default: UNREACHABLE();
2092 return false;
2093 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002094}
2095
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002096bool Renderer11::getPostSubBufferSupport() const
2097{
2098 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2099 return false;
2100}
2101
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002102int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002103{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002104 switch (mFeatureLevel)
2105 {
2106 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002107 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002108 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2109 default: UNREACHABLE(); return 0;
2110 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002111}
2112
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002113int Renderer11::getMinorShaderModel() const
2114{
2115 switch (mFeatureLevel)
2116 {
2117 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2118 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2119 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2120 default: UNREACHABLE(); return 0;
2121 }
2122}
2123
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002124float Renderer11::getMaxPointSize() const
2125{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002126 // choose a reasonable maximum. we enforce this in the shader.
2127 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2128 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002129}
2130
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002131int Renderer11::getMaxViewportDimension() const
2132{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002133 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2134 // In our case return the maximum texture size, which is the maximum render buffer size.
2135 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2136 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2137
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002138 switch (mFeatureLevel)
2139 {
2140 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002141 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002142 case D3D_FEATURE_LEVEL_10_1:
2143 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002144 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002145 default: UNREACHABLE();
2146 return 0;
2147 }
2148}
2149
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002150int Renderer11::getMaxTextureWidth() const
2151{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002152 switch (mFeatureLevel)
2153 {
2154 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2155 case D3D_FEATURE_LEVEL_10_1:
2156 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2157 default: UNREACHABLE(); return 0;
2158 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002159}
2160
2161int Renderer11::getMaxTextureHeight() const
2162{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002163 switch (mFeatureLevel)
2164 {
2165 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2166 case D3D_FEATURE_LEVEL_10_1:
2167 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2168 default: UNREACHABLE(); return 0;
2169 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002170}
2171
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002172int Renderer11::getMaxTextureDepth() const
2173{
2174 switch (mFeatureLevel)
2175 {
2176 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2177 case D3D_FEATURE_LEVEL_10_1:
2178 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2179 default: UNREACHABLE(); return 0;
2180 }
2181}
2182
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002183int Renderer11::getMaxTextureArrayLayers() const
2184{
2185 switch (mFeatureLevel)
2186 {
2187 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2188 case D3D_FEATURE_LEVEL_10_1:
2189 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2190 default: UNREACHABLE(); return 0;
2191 }
2192}
2193
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002194bool Renderer11::get32BitIndexSupport() const
2195{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002196 switch (mFeatureLevel)
2197 {
2198 case D3D_FEATURE_LEVEL_11_0:
2199 case D3D_FEATURE_LEVEL_10_1:
2200 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2201 default: UNREACHABLE(); return false;
2202 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002203}
2204
2205int Renderer11::getMinSwapInterval() const
2206{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002207 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002208}
2209
2210int Renderer11::getMaxSwapInterval() const
2211{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002212 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002213}
2214
2215int Renderer11::getMaxSupportedSamples() const
2216{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002217 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002218}
2219
Geoff Lang0e120e32013-05-29 10:23:55 -04002220GLsizei Renderer11::getMaxSupportedFormatSamples(GLint internalFormat) const
2221{
Shannon Woodsdd4674f2013-07-08 10:32:15 -04002222 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
Geoff Lang0e120e32013-05-29 10:23:55 -04002223 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2224 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2225}
2226
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002227GLsizei Renderer11::getNumSampleCounts(GLint internalFormat) const
2228{
2229 unsigned int numCounts = 0;
2230
2231 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002232 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2233 if (componentType != GL_INT && componentType != GL_UNSIGNED_INT)
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002234 {
2235 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2236 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2237
2238 if (iter != mMultisampleSupportMap.end())
2239 {
2240 const MultisampleSupportInfo& info = iter->second;
2241 for (int i = 0; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2242 {
2243 if (info.qualityLevels[i] > 0)
2244 {
2245 numCounts++;
2246 }
2247 }
2248 }
2249 }
2250
2251 return numCounts;
2252}
2253
2254void Renderer11::getSampleCounts(GLint internalFormat, GLsizei bufSize, GLint *params) const
2255{
2256 // D3D11 supports multisampling for signed and unsigned format, but ES 3.0 does not
Geoff Langb2f3d052013-08-13 12:49:27 -04002257 GLenum componentType = gl::GetComponentType(internalFormat, getCurrentClientVersion());
2258 if (componentType == GL_INT || componentType == GL_UNSIGNED_INT)
2259 {
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002260 return;
Geoff Langb2f3d052013-08-13 12:49:27 -04002261 }
Shannon Woods52f1e7e2013-07-08 10:32:17 -04002262
2263 DXGI_FORMAT format = gl_d3d11::GetRenderableFormat(internalFormat, getCurrentClientVersion());
2264 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2265
2266 if (iter != mMultisampleSupportMap.end())
2267 {
2268 const MultisampleSupportInfo& info = iter->second;
2269 int bufPos = 0;
2270 for (int i = D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT - 1; i >= 0 && bufPos < bufSize; i--)
2271 {
2272 if (info.qualityLevels[i] > 0)
2273 {
2274 params[bufPos++] = i + 1;
2275 }
2276 }
2277 }
2278}
2279
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002280int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2281{
2282 if (requested == 0)
2283 {
2284 return 0;
2285 }
2286
2287 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2288 if (iter != mMultisampleSupportMap.end())
2289 {
2290 const MultisampleSupportInfo& info = iter->second;
2291 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2292 {
2293 if (info.qualityLevels[i] > 0)
2294 {
2295 return i + 1;
2296 }
2297 }
2298 }
2299
2300 return -1;
2301}
2302
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002303unsigned int Renderer11::getMaxRenderTargets() const
2304{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002305 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2306 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2307
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002308 switch (mFeatureLevel)
2309 {
2310 case D3D_FEATURE_LEVEL_11_0:
2311 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2312 case D3D_FEATURE_LEVEL_10_1:
2313 case D3D_FEATURE_LEVEL_10_0:
2314 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2315 default:
2316 UNREACHABLE();
2317 return 1;
2318 }
2319}
2320
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002321bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002322{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002323 if (source && dest)
2324 {
2325 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2326 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2327
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002328 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002329 return true;
2330 }
2331
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002332 return false;
2333}
2334
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002335bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002336{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002337 if (source && dest)
2338 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002339 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2340 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002341
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002342 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002343 return true;
2344 }
2345
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002346 return false;
2347}
2348
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002349bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2350{
2351 if (source && dest)
2352 {
2353 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2354 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2355
2356 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2357 return true;
2358 }
2359
2360 return false;
2361}
2362
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002363bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2364{
2365 if (source && dest)
2366 {
2367 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2368 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2369
2370 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2371 return true;
2372 }
2373
2374 return false;
2375}
2376
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002377bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002378 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002379{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002380 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002381 if (!colorbuffer)
2382 {
2383 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002384 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002385 }
2386
2387 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2388 if (!sourceRenderTarget)
2389 {
2390 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002391 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002392 }
2393
2394 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2395 if (!source)
2396 {
2397 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002398 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002399 }
2400
2401 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2402 if (!storage11)
2403 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002404 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002405 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002406 }
2407
2408 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2409 if (!destRenderTarget)
2410 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002411 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002412 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002413 }
2414
2415 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2416 if (!dest)
2417 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002418 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002419 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002420 }
2421
Geoff Langb86b9792013-06-04 16:32:05 -04002422 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2423 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002424
Geoff Langb86b9792013-06-04 16:32:05 -04002425 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2426 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002427
Geoff Langb86b9792013-06-04 16:32:05 -04002428 // Use nearest filtering because source and destination are the same size for the direct
2429 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002430 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002431 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002432
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002433 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002434}
2435
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002436bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002437 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002438{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002439 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002440 if (!colorbuffer)
2441 {
2442 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002443 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002444 }
2445
2446 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2447 if (!sourceRenderTarget)
2448 {
2449 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002450 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002451 }
2452
2453 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2454 if (!source)
2455 {
2456 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002457 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002458 }
2459
2460 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2461 if (!storage11)
2462 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002463 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002464 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002465 }
2466
Nicolas Capensb13f8662013-06-04 13:30:19 -04002467 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002468 if (!destRenderTarget)
2469 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002470 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002471 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002472 }
2473
2474 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2475 if (!dest)
2476 {
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002477 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002478 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002479 }
2480
Geoff Langb86b9792013-06-04 16:32:05 -04002481 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2482 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002483
Geoff Langb86b9792013-06-04 16:32:05 -04002484 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2485 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002486
Geoff Langb86b9792013-06-04 16:32:05 -04002487 // Use nearest filtering because source and destination are the same size for the direct
2488 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002489 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002490 destFormat, GL_NEAREST);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002491
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002492 return ret;
2493}
2494
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002495bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2496 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2497{
2498 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2499 if (!colorbuffer)
2500 {
2501 ERR("Failed to retrieve the color buffer from the frame buffer.");
2502 return gl::error(GL_OUT_OF_MEMORY, false);
2503 }
2504
2505 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2506 if (!sourceRenderTarget)
2507 {
2508 ERR("Failed to retrieve the render target from the frame buffer.");
2509 return gl::error(GL_OUT_OF_MEMORY, false);
2510 }
2511
2512 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2513 if (!source)
2514 {
2515 ERR("Failed to retrieve the render target view from the render target.");
2516 return gl::error(GL_OUT_OF_MEMORY, false);
2517 }
2518
2519 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2520 if (!storage11)
2521 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002522 ERR("Failed to retrieve the texture storage from the destination.");
2523 return gl::error(GL_OUT_OF_MEMORY, false);
2524 }
2525
2526 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2527 if (!destRenderTarget)
2528 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002529 ERR("Failed to retrieve the render target from the destination storage.");
2530 return gl::error(GL_OUT_OF_MEMORY, false);
2531 }
2532
2533 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2534 if (!dest)
2535 {
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002536 ERR("Failed to retrieve the render target view from the destination render target.");
2537 return gl::error(GL_OUT_OF_MEMORY, false);
2538 }
2539
Geoff Langb86b9792013-06-04 16:32:05 -04002540 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2541 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002542
Geoff Langb86b9792013-06-04 16:32:05 -04002543 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2544 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002545
Geoff Langb86b9792013-06-04 16:32:05 -04002546 // Use nearest filtering because source and destination are the same size for the direct
2547 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002548 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002549 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002550
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002551 return ret;
2552}
2553
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002554bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2555 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2556{
2557 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2558 if (!colorbuffer)
2559 {
2560 ERR("Failed to retrieve the color buffer from the frame buffer.");
2561 return gl::error(GL_OUT_OF_MEMORY, false);
2562 }
2563
2564 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2565 if (!sourceRenderTarget)
2566 {
2567 ERR("Failed to retrieve the render target from the frame buffer.");
2568 return gl::error(GL_OUT_OF_MEMORY, false);
2569 }
2570
2571 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2572 if (!source)
2573 {
2574 ERR("Failed to retrieve the render target view from the render target.");
2575 return gl::error(GL_OUT_OF_MEMORY, false);
2576 }
2577
2578 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2579 if (!storage11)
2580 {
Geoff Langea228632013-07-30 15:17:12 -04002581 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002582 ERR("Failed to retrieve the texture storage from the destination.");
2583 return gl::error(GL_OUT_OF_MEMORY, false);
2584 }
2585
2586 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2587 if (!destRenderTarget)
2588 {
Geoff Langea228632013-07-30 15:17:12 -04002589 SafeRelease(source);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002590 ERR("Failed to retrieve the render target from the destination storage.");
2591 return gl::error(GL_OUT_OF_MEMORY, false);
2592 }
2593
2594 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2595 if (!dest)
2596 {
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002597 ERR("Failed to retrieve the render target view from the destination render target.");
2598 return gl::error(GL_OUT_OF_MEMORY, false);
2599 }
2600
Geoff Langb86b9792013-06-04 16:32:05 -04002601 gl::Box sourceArea(sourceRect.x, sourceRect.y, 0, sourceRect.width, sourceRect.height, 1);
2602 gl::Extents sourceSize(sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002603
Geoff Langb86b9792013-06-04 16:32:05 -04002604 gl::Box destArea(xoffset, yoffset, 0, sourceRect.width, sourceRect.height, 1);
2605 gl::Extents destSize(destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002606
Geoff Langb86b9792013-06-04 16:32:05 -04002607 // Use nearest filtering because source and destination are the same size for the direct
2608 // copy
Geoff Lang125deab2013-08-09 13:34:16 -04002609 bool ret = mBlit->copyTexture(source, sourceArea, sourceSize, dest, destArea, destSize, NULL,
Geoff Langb86b9792013-06-04 16:32:05 -04002610 destFormat, GL_NEAREST);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002611
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002612 return ret;
2613}
2614
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002615void Renderer11::unapplyRenderTargets()
2616{
2617 setOneTimeRenderTarget(NULL);
2618}
2619
2620void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2621{
2622 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2623
2624 rtvArray[0] = renderTargetView;
2625
2626 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2627
2628 // Do not preserve the serial for this one-time-use render target
2629 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2630 {
2631 mAppliedRenderTargetSerials[rtIndex] = 0;
2632 }
2633}
2634
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002635RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2636{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002637 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002638 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002639
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002640 if (depth)
2641 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002642 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002643 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
2644 swapChain11->getDepthStencilTexture(), NULL,
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002645 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002646 }
2647 else
2648 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002649 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002650 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002651 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002652 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00002653 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002654 }
2655 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002656}
2657
Geoff Langa2d97f12013-06-11 11:44:02 -04002658RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples)
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002659{
Geoff Langa2d97f12013-06-11 11:44:02 -04002660 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002661 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002662}
2663
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002664ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002665{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002666 ShaderExecutable11 *executable = NULL;
2667
2668 switch (type)
2669 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002670 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002671 {
2672 ID3D11VertexShader *vshader = NULL;
2673 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2674 ASSERT(SUCCEEDED(result));
2675
2676 if (vshader)
2677 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002678 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002679 }
2680 }
2681 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002682 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002683 {
2684 ID3D11PixelShader *pshader = NULL;
2685 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2686 ASSERT(SUCCEEDED(result));
2687
2688 if (pshader)
2689 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002690 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002691 }
2692 }
2693 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002694 case rx::SHADER_GEOMETRY:
2695 {
2696 ID3D11GeometryShader *gshader = NULL;
2697 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2698 ASSERT(SUCCEEDED(result));
2699
2700 if (gshader)
2701 {
2702 executable = new ShaderExecutable11(function, length, gshader);
2703 }
2704 }
2705 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002706 default:
2707 UNREACHABLE();
2708 break;
2709 }
2710
2711 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002712}
2713
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002714ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002715{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002716 const char *profile = NULL;
2717
2718 switch (type)
2719 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002720 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002721 profile = "vs_4_0";
2722 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002723 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002724 profile = "ps_4_0";
2725 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002726 case rx::SHADER_GEOMETRY:
2727 profile = "gs_4_0";
2728 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002729 default:
2730 UNREACHABLE();
2731 return NULL;
2732 }
2733
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00002734 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002735 if (!binary)
Geoff Langea228632013-07-30 15:17:12 -04002736 {
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002737 return NULL;
Geoff Langea228632013-07-30 15:17:12 -04002738 }
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002739
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002740 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
Geoff Langea228632013-07-30 15:17:12 -04002741 SafeRelease(binary);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002742
2743 return executable;
2744}
2745
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002746VertexBuffer *Renderer11::createVertexBuffer()
2747{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002748 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002749}
2750
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002751IndexBuffer *Renderer11::createIndexBuffer()
2752{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002753 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002754}
2755
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002756BufferStorage *Renderer11::createBufferStorage()
2757{
2758 return new BufferStorage11(this);
2759}
2760
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002761QueryImpl *Renderer11::createQuery(GLenum type)
2762{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002763 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002764}
2765
2766FenceImpl *Renderer11::createFence()
2767{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002768 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002769}
2770
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002771bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002772{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002773 ASSERT(colorbuffer != NULL);
2774
2775 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2776 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002777 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002778 *subresourceIndex = renderTarget->getSubresourceIndex();
2779
2780 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2781 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002782 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002783 ID3D11Resource *textureResource = NULL;
2784 colorBufferRTV->GetResource(&textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002785
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002786 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002787 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002788 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
Geoff Langea228632013-07-30 15:17:12 -04002789 SafeRelease(textureResource);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002790
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002791 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002792 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002793 return true;
2794 }
2795 else
2796 {
2797 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2798 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002799 }
2800 }
2801 }
2802 }
2803
2804 return false;
2805}
2806
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002807bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
Geoff Lang125deab2013-08-09 13:34:16 -04002808 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter)
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002809{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002810 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002811 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002812 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002813
2814 if (!readBuffer)
2815 {
2816 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2817 return gl::error(GL_OUT_OF_MEMORY, false);
2818 }
2819
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002820 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002821
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002822 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002823 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002824 if (drawTarget->isEnabledColorAttachment(colorAttachment))
2825 {
2826 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
2827
2828 if (!drawBuffer)
2829 {
2830 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2831 return gl::error(GL_OUT_OF_MEMORY, false);
2832 }
2833
2834 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2835
Geoff Lang125deab2013-08-09 13:34:16 -04002836 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002837 blitRenderTarget, false, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00002838 {
2839 return false;
2840 }
2841 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002842 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002843 }
2844
Geoff Lang685806d2013-06-12 11:16:36 -04002845 if (blitDepth || blitStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002846 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002847 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
2848 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
2849
2850 if (!readBuffer)
2851 {
2852 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2853 return gl::error(GL_OUT_OF_MEMORY, false);
2854 }
2855
2856 if (!drawBuffer)
2857 {
2858 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2859 return gl::error(GL_OUT_OF_MEMORY, false);
2860 }
2861
2862 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2863 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2864
Geoff Lang125deab2013-08-09 13:34:16 -04002865 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, filter, scissor,
Geoff Lang685806d2013-06-12 11:16:36 -04002866 false, blitDepth, blitStencil))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002867 {
2868 return false;
2869 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002870 }
2871
2872 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002873}
2874
2875void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2876 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2877{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002878 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002879 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002880
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002881 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2882
2883 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002884 {
2885 gl::Rectangle area;
2886 area.x = x;
2887 area.y = y;
2888 area.width = width;
2889 area.height = height;
2890
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002891 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
2892 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002893
Geoff Langea228632013-07-30 15:17:12 -04002894 SafeRelease(colorBufferTexture);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002895 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002896}
2897
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002898Image *Renderer11::createImage()
2899{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002900 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002901}
2902
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002903void Renderer11::generateMipmap(Image *dest, Image *src)
2904{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002905 Image11 *dest11 = Image11::makeImage11(dest);
2906 Image11 *src11 = Image11::makeImage11(src);
Jamie Madilld6cb2442013-07-10 15:13:38 -04002907 Image11::generateMipmap(getCurrentClientVersion(), dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002908}
2909
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002910TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
2911{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002912 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
2913 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002914}
2915
2916TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
2917{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002918 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002919}
2920
2921TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
2922{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002923 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002924}
2925
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00002926TextureStorage *Renderer11::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
2927{
2928 return new TextureStorage11_3D(this, levels, internalformat, usage, width, height, depth);
2929}
2930
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002931TextureStorage *Renderer11::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
2932{
2933 return new TextureStorage11_2DArray(this, levels, internalformat, usage, width, height, depth);
2934}
2935
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002936void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
2937 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
2938 GLint packAlignment, void *pixels)
2939{
2940 D3D11_TEXTURE2D_DESC textureDesc;
2941 texture->GetDesc(&textureDesc);
2942
2943 D3D11_TEXTURE2D_DESC stagingDesc;
2944 stagingDesc.Width = area.width;
2945 stagingDesc.Height = area.height;
2946 stagingDesc.MipLevels = 1;
2947 stagingDesc.ArraySize = 1;
2948 stagingDesc.Format = textureDesc.Format;
2949 stagingDesc.SampleDesc.Count = 1;
2950 stagingDesc.SampleDesc.Quality = 0;
2951 stagingDesc.Usage = D3D11_USAGE_STAGING;
2952 stagingDesc.BindFlags = 0;
2953 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
2954 stagingDesc.MiscFlags = 0;
2955
2956 ID3D11Texture2D* stagingTex = NULL;
2957 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
2958 if (FAILED(result))
2959 {
2960 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
2961 return;
2962 }
2963
2964 ID3D11Texture2D* srcTex = NULL;
2965 if (textureDesc.SampleDesc.Count > 1)
2966 {
2967 D3D11_TEXTURE2D_DESC resolveDesc;
2968 resolveDesc.Width = textureDesc.Width;
2969 resolveDesc.Height = textureDesc.Height;
2970 resolveDesc.MipLevels = 1;
2971 resolveDesc.ArraySize = 1;
2972 resolveDesc.Format = textureDesc.Format;
2973 resolveDesc.SampleDesc.Count = 1;
2974 resolveDesc.SampleDesc.Quality = 0;
2975 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
2976 resolveDesc.BindFlags = 0;
2977 resolveDesc.CPUAccessFlags = 0;
2978 resolveDesc.MiscFlags = 0;
2979
2980 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
2981 if (FAILED(result))
2982 {
2983 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
Geoff Langea228632013-07-30 15:17:12 -04002984 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002985 return;
2986 }
2987
2988 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
2989 subResource = 0;
2990 }
2991 else
2992 {
2993 srcTex = texture;
2994 srcTex->AddRef();
2995 }
2996
2997 D3D11_BOX srcBox;
2998 srcBox.left = area.x;
2999 srcBox.right = area.x + area.width;
3000 srcBox.top = area.y;
3001 srcBox.bottom = area.y + area.height;
3002 srcBox.front = 0;
3003 srcBox.back = 1;
3004
3005 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3006
Geoff Langea228632013-07-30 15:17:12 -04003007 SafeRelease(srcTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003008
3009 D3D11_MAPPED_SUBRESOURCE mapping;
3010 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3011
3012 unsigned char *source;
3013 int inputPitch;
3014 if (packReverseRowOrder)
3015 {
3016 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3017 inputPitch = -static_cast<int>(mapping.RowPitch);
3018 }
3019 else
3020 {
3021 source = static_cast<unsigned char*>(mapping.pData);
3022 inputPitch = static_cast<int>(mapping.RowPitch);
3023 }
3024
Geoff Lang697ad3e2013-06-04 10:11:28 -04003025 GLuint clientVersion = getCurrentClientVersion();
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003026
Jamie Madilld6cb2442013-07-10 15:13:38 -04003027 GLint sourceInternalFormat = d3d11_gl::GetInternalFormat(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003028 GLenum sourceFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3029 GLenum sourceType = gl::GetType(sourceInternalFormat, clientVersion);
3030
3031 GLuint sourcePixelSize = gl::GetPixelBytes(sourceInternalFormat, clientVersion);
3032
3033 if (sourceFormat == format && sourceType == type)
3034 {
3035 // Direct copy possible
3036 unsigned char *dest = static_cast<unsigned char*>(pixels);
3037 for (int y = 0; y < area.height; y++)
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003038 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003039 memcpy(dest + y * outputPitch, source + y * inputPitch, area.width * sourcePixelSize);
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003040 }
3041 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003042 else
3043 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003044 GLint destInternalFormat = gl::GetSizedInternalFormat(format, type, clientVersion);
3045 GLuint destPixelSize = gl::GetPixelBytes(destInternalFormat, clientVersion);
3046
3047 ColorCopyFunction fastCopyFunc = d3d11::GetFastCopyFunction(textureDesc.Format, format, type, getCurrentClientVersion());
3048 if (fastCopyFunc)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003049 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003050 // Fast copy is possible through some special function
3051 for (int y = 0; y < area.height; y++)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003052 {
Geoff Lang697ad3e2013-06-04 10:11:28 -04003053 for (int x = 0; x < area.width; x++)
3054 {
3055 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3056 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3057
3058 fastCopyFunc(src, dest);
3059 }
3060 }
3061 }
3062 else
3063 {
Jamie Madilld6cb2442013-07-10 15:13:38 -04003064 ColorReadFunction readFunc = d3d11::GetColorReadFunction(textureDesc.Format, clientVersion);
Geoff Lang697ad3e2013-06-04 10:11:28 -04003065 ColorWriteFunction writeFunc = gl::GetColorWriteFunction(format, type, clientVersion);
3066
3067 unsigned char temp[16]; // Maximum size of any Color<T> type used.
3068 META_ASSERT(sizeof(temp) >= sizeof(gl::ColorF) &&
3069 sizeof(temp) >= sizeof(gl::ColorUI) &&
3070 sizeof(temp) >= sizeof(gl::ColorI));
3071
3072 for (int y = 0; y < area.height; y++)
3073 {
3074 for (int x = 0; x < area.width; x++)
3075 {
3076 void *dest = static_cast<unsigned char*>(pixels) + y * outputPitch + x * destPixelSize;
3077 void *src = static_cast<unsigned char*>(source) + y * inputPitch + x * sourcePixelSize;
3078
3079 // readFunc and writeFunc will be using the same type of color, CopyTexImage
3080 // will not allow the copy otherwise.
3081 readFunc(src, temp);
3082 writeFunc(temp, dest);
3083 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003084 }
3085 }
3086 }
3087
3088 mDeviceContext->Unmap(stagingTex, 0);
3089
Geoff Langea228632013-07-30 15:17:12 -04003090 SafeRelease(stagingTex);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003091}
3092
Geoff Lang758d5b22013-06-11 11:42:50 -04003093bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
Geoff Lang125deab2013-08-09 13:34:16 -04003094 RenderTarget *drawRenderTarget, GLenum filter, const gl::Rectangle *scissor,
3095 bool colorBlit, bool depthBlit, bool stencilBlit)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003096{
Geoff Lang975af372013-06-12 11:19:22 -04003097 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
3098 // it should never be the case that both color and depth/stencil need to be blitted at
3099 // at the same time.
3100 ASSERT(colorBlit != (depthBlit || stencilBlit));
3101
Geoff Langc1f51be2013-06-11 11:49:14 -04003102 bool result = true;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003103
Geoff Lang4d782732013-07-22 10:44:18 -04003104 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
3105 if (!drawRenderTarget)
3106 {
3107 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
3108 return gl::error(GL_OUT_OF_MEMORY, false);
3109 }
3110
3111 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
3112 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
3113 ID3D11RenderTargetView *drawRTV = drawRenderTarget11->getRenderTargetView();
3114 ID3D11DepthStencilView *drawDSV = drawRenderTarget11->getDepthStencilView();
3115
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003116 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3117 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003118 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003119 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003120 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003121 }
3122
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003123 ID3D11Resource *readTexture = NULL;
Geoff Langc1f51be2013-06-11 11:49:14 -04003124 ID3D11ShaderResourceView *readSRV = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003125 unsigned int readSubresource = 0;
3126 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003127 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003128 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3129 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003130
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003131 if (unresolvedTexture)
3132 {
3133 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3134 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003135
Geoff Langea228632013-07-30 15:17:12 -04003136 SafeRelease(unresolvedTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003137
3138 HRESULT result = mDevice->CreateShaderResourceView(readTexture, NULL, &readSRV);
3139 if (FAILED(result))
3140 {
Geoff Langea228632013-07-30 15:17:12 -04003141 SafeRelease(readTexture);
Geoff Langc1f51be2013-06-11 11:49:14 -04003142 return gl::error(GL_OUT_OF_MEMORY, false);
3143 }
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003144 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003145 }
3146 else
3147 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003148 readTexture = readRenderTarget11->getTexture();
Geoff Lang4d782732013-07-22 10:44:18 -04003149 readTexture->AddRef();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003150 readSubresource = readRenderTarget11->getSubresourceIndex();
Geoff Langc1f51be2013-06-11 11:49:14 -04003151 readSRV = readRenderTarget11->getShaderResourceView();
Geoff Lang4d782732013-07-22 10:44:18 -04003152 readSRV->AddRef();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003153 }
3154
Geoff Lang4d782732013-07-22 10:44:18 -04003155 if (!readTexture || !readSRV)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003156 {
Geoff Lang4d782732013-07-22 10:44:18 -04003157 SafeRelease(readTexture);
3158 SafeRelease(readSRV);
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003159 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003160 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003161 }
3162
Geoff Lang125deab2013-08-09 13:34:16 -04003163 gl::Extents readSize(readRenderTarget->getWidth(), readRenderTarget->getHeight(), 1);
3164 gl::Extents drawSize(drawRenderTarget->getWidth(), drawRenderTarget->getHeight(), 1);
3165
3166 bool scissorNeeded = scissor && gl::ClipRectangle(drawRect, *scissor, NULL);
3167
3168 bool wholeBufferCopy = !scissorNeeded &&
3169 readRect.x == 0 && readRect.width == readSize.width &&
3170 readRect.y == 0 && readRect.height == readSize.height &&
3171 drawRect.x == 0 && drawRect.width == drawSize.width &&
3172 drawRect.y == 0 && drawRect.height == drawSize.height;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003173
Geoff Langc1f51be2013-06-11 11:49:14 -04003174 bool stretchRequired = readRect.width != drawRect.width || readRect.height != drawRect.height;
Geoff Lang758d5b22013-06-11 11:42:50 -04003175
Geoff Lang125deab2013-08-09 13:34:16 -04003176 bool flipRequired = readRect.width < 0 || readRect.height < 0 || drawRect.width < 0 || readRect.height < 0;
3177
3178 bool outOfBounds = readRect.x < 0 || readRect.x + readRect.width > readSize.width ||
3179 readRect.y < 0 || readRect.y + readRect.height > readSize.height ||
3180 drawRect.x < 0 || drawRect.x + drawRect.width > drawSize.width ||
3181 drawRect.y < 0 || drawRect.y + drawRect.height > drawSize.height;
3182
3183 bool hasDepth = gl::GetDepthBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3184 bool hasStencil = gl::GetStencilBits(drawRenderTarget11->getActualFormat(), getCurrentClientVersion()) > 0;
3185 bool partialDSBlit = (hasDepth && depthBlit) != (hasStencil && stencilBlit);
3186
Geoff Langc1f51be2013-06-11 11:49:14 -04003187 if (readRenderTarget11->getActualFormat() == drawRenderTarget->getActualFormat() &&
Geoff Lang125deab2013-08-09 13:34:16 -04003188 !stretchRequired && !outOfBounds && !flipRequired && !partialDSBlit &&
3189 (!(depthBlit || stencilBlit) || wholeBufferCopy))
Geoff Langc1f51be2013-06-11 11:49:14 -04003190 {
Geoff Lang125deab2013-08-09 13:34:16 -04003191 UINT dstX = drawRect.x;
3192 UINT dstY = drawRect.y;
3193
Geoff Langc1f51be2013-06-11 11:49:14 -04003194 D3D11_BOX readBox;
3195 readBox.left = readRect.x;
3196 readBox.right = readRect.x + readRect.width;
3197 readBox.top = readRect.y;
3198 readBox.bottom = readRect.y + readRect.height;
3199 readBox.front = 0;
3200 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003201
Geoff Lang125deab2013-08-09 13:34:16 -04003202 if (scissorNeeded)
3203 {
3204 // drawRect is guaranteed to have positive width and height because stretchRequired is false.
3205 ASSERT(drawRect.width >= 0 || drawRect.height >= 0);
3206
3207 if (drawRect.x < scissor->x)
3208 {
3209 dstX = scissor->x;
3210 readBox.left += (scissor->x - drawRect.x);
3211 }
3212 if (drawRect.y < scissor->y)
3213 {
3214 dstY = scissor->y;
3215 readBox.top += (scissor->y - drawRect.y);
3216 }
3217 if (drawRect.x + drawRect.width > scissor->x + scissor->width)
3218 {
3219 readBox.right -= ((drawRect.x + drawRect.width) - (scissor->x + scissor->width));
3220 }
3221 if (drawRect.y + drawRect.height > scissor->y + scissor->height)
3222 {
3223 readBox.bottom -= ((drawRect.y + drawRect.height) - (scissor->y + scissor->height));
3224 }
3225 }
3226
Geoff Langc1f51be2013-06-11 11:49:14 -04003227 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3228 // We also require complete framebuffer copies for depth-stencil blit.
3229 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003230
Geoff Lang125deab2013-08-09 13:34:16 -04003231 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, dstX, dstY, 0,
Geoff Langc1f51be2013-06-11 11:49:14 -04003232 readTexture, readSubresource, pSrcBox);
3233 result = true;
3234 }
3235 else
3236 {
3237 gl::Box readArea(readRect.x, readRect.y, 0, readRect.width, readRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003238 gl::Box drawArea(drawRect.x, drawRect.y, 0, drawRect.width, drawRect.height, 1);
Geoff Langc1f51be2013-06-11 11:49:14 -04003239
Geoff Lang975af372013-06-12 11:19:22 -04003240 if (depthBlit && stencilBlit)
Geoff Langc1f51be2013-06-11 11:49:14 -04003241 {
Geoff Lang975af372013-06-12 11:19:22 -04003242 result = mBlit->copyDepthStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003243 drawTexture, drawSubresource, drawArea, drawSize,
3244 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003245 }
3246 else if (depthBlit)
3247 {
Geoff Lang125deab2013-08-09 13:34:16 -04003248 result = mBlit->copyDepth(readSRV, readArea, readSize, drawDSV, drawArea, drawSize,
3249 scissor);
Geoff Lang975af372013-06-12 11:19:22 -04003250 }
3251 else if (stencilBlit)
3252 {
3253 result = mBlit->copyStencil(readTexture, readSubresource, readArea, readSize,
Geoff Lang125deab2013-08-09 13:34:16 -04003254 drawTexture, drawSubresource, drawArea, drawSize,
3255 scissor);
Geoff Langc1f51be2013-06-11 11:49:14 -04003256 }
3257 else
3258 {
Geoff Lang685806d2013-06-12 11:16:36 -04003259 GLenum format = gl::GetFormat(drawRenderTarget->getInternalFormat(), getCurrentClientVersion());
Geoff Lang125deab2013-08-09 13:34:16 -04003260 result = mBlit->copyTexture(readSRV, readArea, readSize, drawRTV, drawArea, drawSize,
3261 scissor, format, filter);
Geoff Langc1f51be2013-06-11 11:49:14 -04003262 }
3263 }
3264
3265 SafeRelease(readTexture);
3266 SafeRelease(readSRV);
Geoff Langc1f51be2013-06-11 11:49:14 -04003267
3268 return result;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003269}
3270
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003271ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3272{
3273 D3D11_TEXTURE2D_DESC textureDesc;
3274 source->GetDesc(&textureDesc);
3275
3276 if (textureDesc.SampleDesc.Count > 1)
3277 {
3278 D3D11_TEXTURE2D_DESC resolveDesc;
3279 resolveDesc.Width = textureDesc.Width;
3280 resolveDesc.Height = textureDesc.Height;
3281 resolveDesc.MipLevels = 1;
3282 resolveDesc.ArraySize = 1;
3283 resolveDesc.Format = textureDesc.Format;
3284 resolveDesc.SampleDesc.Count = 1;
3285 resolveDesc.SampleDesc.Quality = 0;
3286 resolveDesc.Usage = textureDesc.Usage;
3287 resolveDesc.BindFlags = textureDesc.BindFlags;
3288 resolveDesc.CPUAccessFlags = 0;
3289 resolveDesc.MiscFlags = 0;
3290
3291 ID3D11Texture2D *resolveTexture = NULL;
3292 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3293 if (FAILED(result))
3294 {
3295 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3296 return NULL;
3297 }
3298
3299 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3300 return resolveTexture;
3301 }
3302 else
3303 {
3304 source->AddRef();
3305 return source;
3306 }
3307}
3308
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003309bool Renderer11::getLUID(LUID *adapterLuid) const
3310{
3311 adapterLuid->HighPart = 0;
3312 adapterLuid->LowPart = 0;
3313
3314 if (!mDxgiAdapter)
3315 {
3316 return false;
3317 }
3318
3319 DXGI_ADAPTER_DESC adapterDesc;
3320 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3321 {
3322 return false;
3323 }
3324
3325 *adapterLuid = adapterDesc.AdapterLuid;
3326 return true;
3327}
3328
Geoff Lang61e49a52013-05-29 10:22:58 -04003329Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3330{
3331 MultisampleSupportInfo supportInfo = { 0 };
3332
3333 UINT formatSupport;
3334 HRESULT result;
3335
3336 result = mDevice->CheckFormatSupport(format, &formatSupport);
3337 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
3338 {
3339 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
3340 {
3341 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
3342 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
3343 {
3344 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
3345 }
3346 else
3347 {
3348 supportInfo.qualityLevels[i - 1] = 0;
3349 }
3350 }
3351 }
3352
3353 return supportInfo;
3354}
3355
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003356}