blob: da6d389ae07e1051d1f2580491ab7e79f4d59939 [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"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000011#include "libGLESv2/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"
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +000019#include "libGLESv2/renderer/ShaderExecutable11.h"
daniel@transgaming.coma60160b2012-11-28 19:41:15 +000020#include "libGLESv2/renderer/SwapChain11.h"
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000021#include "libGLESv2/renderer/Image11.h"
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +000022#include "libGLESv2/renderer/VertexBuffer11.h"
daniel@transgaming.com11c2af52012-12-20 21:10:01 +000023#include "libGLESv2/renderer/IndexBuffer11.h"
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +000024#include "libGLESv2/renderer/BufferStorage11.h"
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000025#include "libGLESv2/renderer/VertexDataManager.h"
26#include "libGLESv2/renderer/IndexDataManager.h"
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +000027#include "libGLESv2/renderer/TextureStorage11.h"
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +000028#include "libGLESv2/renderer/Query11.h"
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +000029#include "libGLESv2/renderer/Fence11.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000030
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000031#include "libGLESv2/renderer/shaders/compiled/passthrough11vs.h"
32#include "libGLESv2/renderer/shaders/compiled/passthroughrgba11ps.h"
33#include "libGLESv2/renderer/shaders/compiled/passthroughrgb11ps.h"
34#include "libGLESv2/renderer/shaders/compiled/passthroughlum11ps.h"
35#include "libGLESv2/renderer/shaders/compiled/passthroughlumalpha11ps.h"
36
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000037#include "libGLESv2/renderer/shaders/compiled/clear11vs.h"
38#include "libGLESv2/renderer/shaders/compiled/clear11ps.h"
39
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000040#include "libEGL/Display.h"
41
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000042#ifdef _DEBUG
43// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
44// and conformance tests. to enable all warnings, remove this define.
45#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
46#endif
47
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000048namespace rx
49{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000050static const DXGI_FORMAT RenderTargetFormats[] =
51 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000052 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000053 DXGI_FORMAT_R8G8B8A8_UNORM
54 };
55
56static const DXGI_FORMAT DepthStencilFormats[] =
57 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000058 DXGI_FORMAT_UNKNOWN,
59 DXGI_FORMAT_D24_UNORM_S8_UINT,
60 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000061 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000062
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000063enum
64{
65 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
66};
67
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000068Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
69{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000070 mVertexDataManager = NULL;
71 mIndexDataManager = NULL;
72
daniel@transgaming.comc5114302012-12-20 21:11:36 +000073 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000074 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000075
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000076 mCopyResourcesInitialized = false;
77 mCopyVB = NULL;
78 mCopySampler = NULL;
79 mCopyIL = NULL;
80 mCopyVS = NULL;
81 mCopyRGBAPS = NULL;
82 mCopyRGBPS = NULL;
83 mCopyLumPS = NULL;
84 mCopyLumAlphaPS = NULL;
85
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000086 mClearResourcesInitialized = false;
87 mClearVB = NULL;
88 mClearIL = NULL;
89 mClearVS = NULL;
90 mClearPS = NULL;
91 mClearScissorRS = NULL;
92 mClearNoScissorRS = NULL;
93
daniel@transgaming.combdf787f2013-02-01 03:20:36 +000094 mSyncQuery = NULL;
95
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000096 mD3d11Module = NULL;
97 mDxgiModule = NULL;
98
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000099 mDeviceLost = false;
100
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000101 mMaxSupportedSamples = 0;
102
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000103 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000104 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000105 mDxgiAdapter = NULL;
106 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000107
108 mDriverConstantBufferVS = NULL;
109 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000110
111 mBGRATextureSupport = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +0000112
113 mIsGeometryShaderActive = false;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000114}
115
116Renderer11::~Renderer11()
117{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000118 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000119}
120
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000121Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
122{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000123 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000124 return static_cast<rx::Renderer11*>(renderer);
125}
126
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000127#ifndef __d3d11_1_h__
128#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
129#endif
130
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000131EGLint Renderer11::initialize()
132{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000133 if (!initializeCompiler())
134 {
135 return EGL_NOT_INITIALIZED;
136 }
137
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000138 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
139 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000140
141 if (mD3d11Module == NULL || mDxgiModule == NULL)
142 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000143 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000144 return EGL_NOT_INITIALIZED;
145 }
146
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000147 // create the D3D11 device
148 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000149 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000150
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000151 if (D3D11CreateDevice == NULL)
152 {
153 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
154 return EGL_NOT_INITIALIZED;
155 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000156
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000157 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000158 {
159 D3D_FEATURE_LEVEL_11_0,
160 D3D_FEATURE_LEVEL_10_1,
161 D3D_FEATURE_LEVEL_10_0,
162 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000163
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000164 HRESULT result = D3D11CreateDevice(NULL,
165 D3D_DRIVER_TYPE_HARDWARE,
166 NULL,
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000167 #if defined(_DEBUG)
168 D3D11_CREATE_DEVICE_DEBUG,
169 #else
170 0,
171 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000172 featureLevels,
173 ArraySize(featureLevels),
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000174 D3D11_SDK_VERSION,
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000175 &mDevice,
176 &mFeatureLevel,
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000177 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000178
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000179 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000180 {
181 ERR("Could not create D3D11 device - aborting!\n");
182 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
183 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000184
185 IDXGIDevice *dxgiDevice = NULL;
186 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
187
188 if (FAILED(result))
189 {
190 ERR("Could not query DXGI device - aborting!\n");
191 return EGL_NOT_INITIALIZED;
192 }
193
194 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
195
196 if (FAILED(result))
197 {
198 ERR("Could not retrieve DXGI adapter - aborting!\n");
199 return EGL_NOT_INITIALIZED;
200 }
201
202 dxgiDevice->Release();
203
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000204 mDxgiAdapter->GetDesc(&mAdapterDescription);
205 memset(mDescription, 0, sizeof(mDescription));
206 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
207
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000208 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
209
210 if (!mDxgiFactory || FAILED(result))
211 {
212 ERR("Could not create DXGI factory - aborting!\n");
213 return EGL_NOT_INITIALIZED;
214 }
215
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000216 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
217#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
218 ID3D11InfoQueue *infoQueue;
219 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
220
221 if (SUCCEEDED(result))
222 {
223 D3D11_MESSAGE_ID hideMessages[] =
224 {
225 D3D11_MESSAGE_ID_DEVICE_OMSETRENDERTARGETS_HAZARD,
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000226 D3D11_MESSAGE_ID_DEVICE_PSSETSHADERRESOURCES_HAZARD,
227 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000228 };
229
230 D3D11_INFO_QUEUE_FILTER filter = {0};
231 filter.DenyList.NumIDs = ArraySize(hideMessages);
232 filter.DenyList.pIDList = hideMessages;
233
234 infoQueue->AddStorageFilterEntries(&filter);
235
236 infoQueue->Release();
237 }
238#endif
239
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000240 unsigned int maxSupportedSamples = 0;
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000241 unsigned int rtFormatCount = ArraySize(RenderTargetFormats);
242 unsigned int dsFormatCount = ArraySize(DepthStencilFormats);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000243 for (unsigned int i = 0; i < rtFormatCount + dsFormatCount; ++i)
244 {
245 DXGI_FORMAT format = (i < rtFormatCount) ? RenderTargetFormats[i] : DepthStencilFormats[i - rtFormatCount];
246 if (format != DXGI_FORMAT_UNKNOWN)
247 {
248 UINT formatSupport;
249 result = mDevice->CheckFormatSupport(format, &formatSupport);
250 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
251 {
252 MultisampleSupportInfo supportInfo;
253
254 for (unsigned int j = 1; j <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; j++)
255 {
256 result = mDevice->CheckMultisampleQualityLevels(format, j, &supportInfo.qualityLevels[j - 1]);
257 if (SUCCEEDED(result) && supportInfo.qualityLevels[j - 1] > 0)
258 {
259 maxSupportedSamples = std::max(j, maxSupportedSamples);
260 }
261 else
262 {
263 supportInfo.qualityLevels[j - 1] = 0;
264 }
265 }
266
267 mMultisampleSupportMap.insert(std::make_pair(format, supportInfo));
268 }
269 }
270 }
271 mMaxSupportedSamples = maxSupportedSamples;
272
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000273 initializeDevice();
274
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000275 // BGRA texture support is optional in feature levels 10 and 10_1
276 UINT formatSupport;
277 result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport);
278 if (FAILED(result))
279 {
280 ERR("Error checking BGRA format support: 0x%08X", result);
281 }
282 else
283 {
284 const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET);
285 mBGRATextureSupport = (formatSupport & flags) == flags;
286 }
287
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000288 // Check floating point texture support
289 static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE;
290 static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET;
291 static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE;
292
293 DXGI_FORMAT float16Formats[] =
294 {
295 DXGI_FORMAT_R16_FLOAT,
296 DXGI_FORMAT_R16G16_FLOAT,
297 DXGI_FORMAT_R16G16B16A16_FLOAT,
298 };
299
300 DXGI_FORMAT float32Formats[] =
301 {
302 DXGI_FORMAT_R32_FLOAT,
303 DXGI_FORMAT_R32G32_FLOAT,
304 DXGI_FORMAT_R32G32B32_FLOAT,
305 DXGI_FORMAT_R32G32B32A32_FLOAT,
306 };
307
308 mFloat16TextureSupport = true;
309 mFloat16FilterSupport = true;
310 mFloat16RenderSupport = true;
311 for (unsigned int i = 0; i < ArraySize(float16Formats); i++)
312 {
313 if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport)))
314 {
315 mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
316 mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
317 mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
318 }
319 else
320 {
321 mFloat16TextureSupport = false;
322 mFloat16RenderSupport = false;
323 mFloat16FilterSupport = false;
324 }
325 }
326
327 mFloat32TextureSupport = true;
328 mFloat32FilterSupport = true;
329 mFloat32RenderSupport = true;
330 for (unsigned int i = 0; i < ArraySize(float32Formats); i++)
331 {
332 if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport)))
333 {
334 mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
335 mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
336 mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
337 }
338 else
339 {
340 mFloat32TextureSupport = false;
341 mFloat32FilterSupport = false;
342 mFloat32RenderSupport = false;
343 }
344 }
345
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +0000346 // Check compressed texture support
347 const unsigned int requiredCompressedTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D;
348
349 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &formatSupport)))
350 {
351 mDXT1TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
352 }
353 else
354 {
355 mDXT1TextureSupport = false;
356 }
357
358 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport)))
359 {
360 mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
361 }
362 else
363 {
364 mDXT3TextureSupport = false;
365 }
366
367 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC5_UNORM, &formatSupport)))
368 {
369 mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
370 }
371 else
372 {
373 mDXT5TextureSupport = false;
374 }
375
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +0000376 // Check depth texture support
377 DXGI_FORMAT depthTextureFormats[] =
378 {
379 DXGI_FORMAT_D16_UNORM,
380 DXGI_FORMAT_D24_UNORM_S8_UINT,
381 };
382
383 static const unsigned int requiredDepthTextureFlags = D3D11_FORMAT_SUPPORT_DEPTH_STENCIL |
384 D3D11_FORMAT_SUPPORT_TEXTURE2D;
385
386 mDepthTextureSupport = true;
387 for (unsigned int i = 0; i < ArraySize(depthTextureFormats); i++)
388 {
389 if (SUCCEEDED(mDevice->CheckFormatSupport(depthTextureFormats[i], &formatSupport)))
390 {
391 mDepthTextureSupport = mDepthTextureSupport && ((formatSupport & requiredDepthTextureFlags) == requiredDepthTextureFlags);
392 }
393 else
394 {
395 mDepthTextureSupport = false;
396 }
397 }
398
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000399 return EGL_SUCCESS;
400}
401
402// do any one-time device initialization
403// NOTE: this is also needed after a device lost/reset
404// to reset the scene status and ensure the default states are reset.
405void Renderer11::initializeDevice()
406{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000407 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000408 mInputLayoutCache.initialize(mDevice, mDeviceContext);
409
410 ASSERT(!mVertexDataManager && !mIndexDataManager);
411 mVertexDataManager = new VertexDataManager(this);
412 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000413
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000414 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000415}
416
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000417int Renderer11::generateConfigs(ConfigDesc **configDescList)
418{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000419 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
420 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000421 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
422 int numConfigs = 0;
423
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000424 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000425 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000426 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000427 {
428 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
429
430 UINT formatSupport = 0;
431 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000432
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000433 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
434 {
435 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
436
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000437 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000438
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000439 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
440 {
441 UINT formatSupport = 0;
442 result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
443 depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
444 }
445
446 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000447 {
448 ConfigDesc newConfig;
449 newConfig.renderTargetFormat = d3d11_gl::ConvertBackBufferFormat(renderTargetFormat);
450 newConfig.depthStencilFormat = d3d11_gl::ConvertDepthStencilFormat(depthStencilFormat);
451 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
452 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000453 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000454
455 (*configDescList)[numConfigs++] = newConfig;
456 }
457 }
458 }
459 }
460
461 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000462}
463
464void Renderer11::deleteConfigs(ConfigDesc *configDescList)
465{
466 delete [] (configDescList);
467}
468
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000469void Renderer11::sync(bool block)
470{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000471 if (block)
472 {
473 HRESULT result;
474
475 if (!mSyncQuery)
476 {
477 D3D11_QUERY_DESC queryDesc;
478 queryDesc.Query = D3D11_QUERY_EVENT;
479 queryDesc.MiscFlags = 0;
480
481 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
482 ASSERT(SUCCEEDED(result));
483 }
484
485 mDeviceContext->End(mSyncQuery);
486 mDeviceContext->Flush();
487
488 do
489 {
490 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
491
492 // Keep polling, but allow other threads to do something useful first
493 Sleep(0);
494
495 if (testDeviceLost(true))
496 {
497 return;
498 }
499 }
500 while (result == S_FALSE);
501 }
502 else
503 {
504 mDeviceContext->Flush();
505 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000506}
507
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000508SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
509{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000510 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000511}
512
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000513void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
514{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000515 if (type == gl::SAMPLER_PIXEL)
516 {
517 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
518 {
519 ERR("Pixel shader sampler index %i is not valid.", index);
520 return;
521 }
522
523 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
524 {
525 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
526
527 if (!dxSamplerState)
528 {
529 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
530 "sampler state for pixel shaders at slot %i.", index);
531 }
532
533 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
534
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000535 mCurPixelSamplerStates[index] = samplerState;
536 }
537
538 mForceSetPixelSamplerStates[index] = false;
539 }
540 else if (type == gl::SAMPLER_VERTEX)
541 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000542 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000543 {
544 ERR("Vertex shader sampler index %i is not valid.", index);
545 return;
546 }
547
548 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
549 {
550 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
551
552 if (!dxSamplerState)
553 {
554 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
555 "sampler state for vertex shaders at slot %i.", index);
556 }
557
558 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
559
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000560 mCurVertexSamplerStates[index] = samplerState;
561 }
562
563 mForceSetVertexSamplerStates[index] = false;
564 }
565 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000566}
567
568void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
569{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000570 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000571 unsigned int serial = 0;
572 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000573
574 if (texture)
575 {
576 TextureStorageInterface *texStorage = texture->getNativeTexture();
577 if (texStorage)
578 {
579 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
580 textureSRV = storage11->getSRV();
581 }
582
583 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
584 // missing the shader resource view
585 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000586
587 serial = texture->getTextureSerial();
588 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000589 }
590
591 if (type == gl::SAMPLER_PIXEL)
592 {
593 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
594 {
595 ERR("Pixel shader sampler index %i is not valid.", index);
596 return;
597 }
598
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000599 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
600 {
601 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
602 }
603
604 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000605 }
606 else if (type == gl::SAMPLER_VERTEX)
607 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000608 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000609 {
610 ERR("Vertex shader sampler index %i is not valid.", index);
611 return;
612 }
613
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000614 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
615 {
616 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
617 }
618
619 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000620 }
621 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000622}
623
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000624void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000625{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000626 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000627 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000628 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
629 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000630 if (!dxRasterState)
631 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000632 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000633 "rasterizer state.");
634 }
635
636 mDeviceContext->RSSetState(dxRasterState);
637
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000638 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000639 }
640
641 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000642}
643
644void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor,
645 unsigned int sampleMask)
646{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000647 if (mForceSetBlendState ||
648 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
649 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0 ||
650 sampleMask != mCurSampleMask)
651 {
652 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
653 if (!dxBlendState)
654 {
655 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
656 "blend state.");
657 }
658
659 const float blendColors[] = { blendColor.red, blendColor.green, blendColor.blue, blendColor.alpha };
660 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
661
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000662 mCurBlendState = blendState;
663 mCurBlendColor = blendColor;
664 mCurSampleMask = sampleMask;
665 }
666
667 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000668}
669
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000670void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000671 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000672{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000673 if (mForceSetDepthStencilState ||
674 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
675 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
676 {
677 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
678 stencilRef != stencilBackRef ||
679 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
680 {
681 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
682 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000683 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000684 }
685
686 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
687 if (!dxDepthStencilState)
688 {
689 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
690 "setting the default depth stencil state.");
691 }
692
693 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
694
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000695 mCurDepthStencilState = depthStencilState;
696 mCurStencilRef = stencilRef;
697 mCurStencilBackRef = stencilBackRef;
698 }
699
700 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000701}
702
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000703void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000704{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000705 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
706 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000707 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000708 if (enabled)
709 {
710 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000711 rect.left = std::max(0, scissor.x);
712 rect.top = std::max(0, scissor.y);
713 rect.right = scissor.x + std::max(0, scissor.width);
714 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000715
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000716 mDeviceContext->RSSetScissorRects(1, &rect);
717 }
718
719 if (enabled != mScissorEnabled)
720 {
721 mForceSetRasterState = true;
722 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000723
724 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000725 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000726 }
727
728 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000729}
730
daniel@transgaming.com12985182012-12-20 20:56:31 +0000731bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000732 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000733{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000734 gl::Rectangle actualViewport = viewport;
735 float actualZNear = gl::clamp01(zNear);
736 float actualZFar = gl::clamp01(zFar);
737 if (ignoreViewport)
738 {
739 actualViewport.x = 0;
740 actualViewport.y = 0;
741 actualViewport.width = mRenderTargetDesc.width;
742 actualViewport.height = mRenderTargetDesc.height;
743 actualZNear = 0.0f;
744 actualZFar = 1.0f;
745 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000746
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000747 // Get D3D viewport bounds, which depends on the feature level
748 const Range& viewportBounds = getViewportBounds();
749
750 // 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 +0000751 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000752 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
753 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
754 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
755 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
756 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
757 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000758 dxViewport.MinDepth = actualZNear;
759 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000760
761 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
762 {
763 return false; // Nothing to render
764 }
765
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000766 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
767 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000768
daniel@transgaming.com53670042012-11-28 20:55:51 +0000769 if (viewportChanged)
770 {
771 mDeviceContext->RSSetViewports(1, &dxViewport);
772
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000773 mCurViewport = actualViewport;
774 mCurNear = actualZNear;
775 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000776
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000777 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
778 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
779 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
780 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000781
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000782 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
783 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000784
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000785 mVertexConstants.depthRange[0] = actualZNear;
786 mVertexConstants.depthRange[1] = actualZFar;
787 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000788
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000789 mPixelConstants.depthRange[0] = actualZNear;
790 mPixelConstants.depthRange[1] = actualZFar;
791 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000792 }
793
794 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000795 return true;
796}
797
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000798bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
799{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000800 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000801
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000802 switch (mode)
803 {
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000804 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break;
805 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break;
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000806 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000807 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
808 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
809 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000810 // emulate fans via rewriting index buffer
811 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000812 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000813 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000814 }
815
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000816 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000817
818 return count > 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000819}
820
821bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000822{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000823 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000824 // Also extract the render target dimensions and view
825 unsigned int renderTargetWidth = 0;
826 unsigned int renderTargetHeight = 0;
827 GLenum renderTargetFormat = 0;
828 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
829 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
830 bool missingColorRenderTarget = true;
831
832 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000833 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000834 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
835
836 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000837 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000838 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
839 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
840
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000841 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000842
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000843 if (!colorbuffer)
844 {
845 ERR("render target pointer unexpectedly null.");
846 return false;
847 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000848
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000849 // check for zero-sized default framebuffer, which is a special case.
850 // in this case we do not wish to modify any state and just silently return false.
851 // this will not report any gl error but will cause the calling method to return.
852 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
853 {
854 return false;
855 }
856
857 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
858
859 // Extract the render target dimensions and view
860 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
861 if (!renderTarget)
862 {
863 ERR("render target pointer unexpectedly null.");
864 return false;
865 }
866
867 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
868 if (!framebufferRTVs[colorAttachment])
869 {
870 ERR("render target view pointer unexpectedly null.");
871 return false;
872 }
873
874 if (missingColorRenderTarget)
875 {
876 renderTargetWidth = colorbuffer->getWidth();
877 renderTargetHeight = colorbuffer->getHeight();
878 renderTargetFormat = colorbuffer->getActualFormat();
879 missingColorRenderTarget = false;
880 }
881 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000882 }
883
884 // Get the depth stencil render buffer and serials
885 gl::Renderbuffer *depthStencil = NULL;
886 unsigned int depthbufferSerial = 0;
887 unsigned int stencilbufferSerial = 0;
888 if (framebuffer->getDepthbufferType() != GL_NONE)
889 {
890 depthStencil = framebuffer->getDepthbuffer();
891 if (!depthStencil)
892 {
893 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000894 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000895 return false;
896 }
897
898 depthbufferSerial = depthStencil->getSerial();
899 }
900 else if (framebuffer->getStencilbufferType() != GL_NONE)
901 {
902 depthStencil = framebuffer->getStencilbuffer();
903 if (!depthStencil)
904 {
905 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000906 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000907 return false;
908 }
909
910 stencilbufferSerial = depthStencil->getSerial();
911 }
912
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000913 // Extract the depth stencil sizes and view
914 unsigned int depthSize = 0;
915 unsigned int stencilSize = 0;
916 ID3D11DepthStencilView* framebufferDSV = NULL;
917 if (depthStencil)
918 {
919 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
920 if (!depthStencilRenderTarget)
921 {
922 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000923 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000924 return false;
925 }
926
927 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
928 if (!framebufferDSV)
929 {
930 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000931 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000932 return false;
933 }
934
935 // If there is no render buffer, the width, height and format values come from
936 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000937 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000938 {
939 renderTargetWidth = depthStencil->getWidth();
940 renderTargetHeight = depthStencil->getHeight();
941 renderTargetFormat = depthStencil->getActualFormat();
942 }
943
944 depthSize = depthStencil->getDepthSize();
945 stencilSize = depthStencil->getStencilSize();
946 }
947
948 // Apply the render target and depth stencil
949 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000950 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000951 depthbufferSerial != mAppliedDepthbufferSerial ||
952 stencilbufferSerial != mAppliedStencilbufferSerial)
953 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000954 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000955
956 mRenderTargetDesc.width = renderTargetWidth;
957 mRenderTargetDesc.height = renderTargetHeight;
958 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000959 mForceSetViewport = true;
960 mForceSetScissor = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000961
962 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
963 {
964 mCurDepthSize = depthSize;
965 mForceSetRasterState = true;
966 }
967
968 mCurStencilSize = stencilSize;
969
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000970 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
971 {
972 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
973 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000974 mAppliedDepthbufferSerial = depthbufferSerial;
975 mAppliedStencilbufferSerial = stencilbufferSerial;
976 mRenderTargetDescInitialized = true;
977 mDepthStencilInitialized = true;
978 }
979
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000980 SafeRelease(framebufferRTVs);
981 SafeRelease(framebufferDSV);
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000982
983 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000984}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000985
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000986GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000987{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000988 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
989 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
990 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000991 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000992 return err;
993 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000994
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000995 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000996}
997
daniel@transgaming.com31240482012-11-28 21:06:41 +0000998GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000999{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001000 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001001
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001002 if (err == GL_NO_ERROR)
1003 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001004 if (indexInfo->storage)
1005 {
1006 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1007 {
1008 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1009 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1010
1011 mDeviceContext->IASetIndexBuffer(storage->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
1012
1013 mAppliedIBSerial = 0;
1014 mAppliedStorageIBSerial = storage->getSerial();
1015 mAppliedIBOffset = indexInfo->startOffset;
1016 }
1017 }
1018 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001019 {
1020 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1021
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001022 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001023
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001024 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001025 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001026 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001027 }
1028 }
1029
1030 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001031}
1032
1033void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1034{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001035 if (mode == GL_LINE_LOOP)
1036 {
1037 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1038 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001039 else if (mode == GL_TRIANGLE_FAN)
1040 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001041 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001042 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001043 else if (instances > 0)
1044 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001045 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001046 }
1047 else
1048 {
1049 mDeviceContext->Draw(count, 0);
1050 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001051}
1052
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001053void 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 +00001054{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001055 if (mode == GL_LINE_LOOP)
1056 {
1057 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1058 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001059 else if (mode == GL_TRIANGLE_FAN)
1060 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001061 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1062 }
1063 else if (instances > 0)
1064 {
1065 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001066 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001067 else
1068 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001069 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001070 }
1071}
1072
1073void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1074{
1075 // Get the raw indices for an indexed draw
1076 if (type != GL_NONE && elementArrayBuffer)
1077 {
1078 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001079 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001080 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001081 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001082 }
1083
1084 if (!mLineLoopIB)
1085 {
1086 mLineLoopIB = new StreamingIndexBufferInterface(this);
1087 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1088 {
1089 delete mLineLoopIB;
1090 mLineLoopIB = NULL;
1091
1092 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001093 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001094 }
1095 }
1096
1097 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
1098 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1099 {
1100 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001101 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001102 }
1103
1104 void* mappedMemory = NULL;
1105 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1106 if (offset == -1 || mappedMemory == NULL)
1107 {
1108 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001109 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001110 }
1111
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001112 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001113 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001114
1115 switch (type)
1116 {
1117 case GL_NONE: // Non-indexed draw
1118 for (int i = 0; i < count; i++)
1119 {
1120 data[i] = i;
1121 }
1122 data[count] = 0;
1123 break;
1124 case GL_UNSIGNED_BYTE:
1125 for (int i = 0; i < count; i++)
1126 {
1127 data[i] = static_cast<const GLubyte*>(indices)[i];
1128 }
1129 data[count] = static_cast<const GLubyte*>(indices)[0];
1130 break;
1131 case GL_UNSIGNED_SHORT:
1132 for (int i = 0; i < count; i++)
1133 {
1134 data[i] = static_cast<const GLushort*>(indices)[i];
1135 }
1136 data[count] = static_cast<const GLushort*>(indices)[0];
1137 break;
1138 case GL_UNSIGNED_INT:
1139 for (int i = 0; i < count; i++)
1140 {
1141 data[i] = static_cast<const GLuint*>(indices)[i];
1142 }
1143 data[count] = static_cast<const GLuint*>(indices)[0];
1144 break;
1145 default: UNREACHABLE();
1146 }
1147
1148 if (!mLineLoopIB->unmapBuffer())
1149 {
1150 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001151 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001152 }
1153
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001154 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001155 {
1156 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1157
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001158 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001159 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001160 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001161 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001162 }
1163
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001164 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001165}
1166
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001167void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001168{
1169 // Get the raw indices for an indexed draw
1170 if (type != GL_NONE && elementArrayBuffer)
1171 {
1172 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001173 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001174 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001175 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001176 }
1177
1178 if (!mTriangleFanIB)
1179 {
1180 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1181 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1182 {
1183 delete mTriangleFanIB;
1184 mTriangleFanIB = NULL;
1185
1186 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001187 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001188 }
1189 }
1190
1191 const int numTris = count - 2;
1192 const int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
1193 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1194 {
1195 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001196 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001197 }
1198
1199 void* mappedMemory = NULL;
1200 int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory);
1201 if (offset == -1 || mappedMemory == NULL)
1202 {
1203 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001204 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001205 }
1206
1207 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1208 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
1209
1210 switch (type)
1211 {
1212 case GL_NONE: // Non-indexed draw
1213 for (int i = 0; i < numTris; i++)
1214 {
1215 data[i*3 + 0] = 0;
1216 data[i*3 + 1] = i + 1;
1217 data[i*3 + 2] = i + 2;
1218 }
1219 break;
1220 case GL_UNSIGNED_BYTE:
1221 for (int i = 0; i < numTris; i++)
1222 {
1223 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1224 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1225 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1226 }
1227 break;
1228 case GL_UNSIGNED_SHORT:
1229 for (int i = 0; i < numTris; i++)
1230 {
1231 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1232 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1233 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1234 }
1235 break;
1236 case GL_UNSIGNED_INT:
1237 for (int i = 0; i < numTris; i++)
1238 {
1239 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1240 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1241 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1242 }
1243 break;
1244 default: UNREACHABLE();
1245 }
1246
1247 if (!mTriangleFanIB->unmapBuffer())
1248 {
1249 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001250 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001251 }
1252
1253 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1254 {
1255 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1256
1257 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1258 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001259 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001260 mAppliedIBOffset = indexBufferOffset;
1261 }
1262
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001263 if (instances > 0)
1264 {
1265 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1266 }
1267 else
1268 {
1269 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1270 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001271}
1272
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001273void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1274{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001275 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001276 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1277
1278 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001279 {
1280 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1281 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001282
daniel@transgaming.come4991412012-12-20 20:55:34 +00001283 ID3D11VertexShader *vertexShader = NULL;
1284 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001285
daniel@transgaming.come4991412012-12-20 20:55:34 +00001286 ID3D11PixelShader *pixelShader = NULL;
1287 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001288
daniel@transgaming.come4991412012-12-20 20:55:34 +00001289 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1290 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001291
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001292 programBinary->dirtyAllUniforms();
1293
1294 mAppliedProgramBinarySerial = programBinarySerial;
1295 }
1296
1297 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001298 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001299
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001300 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001301 {
1302 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001303 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001304 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1305 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001306 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1307 }
1308 else
1309 {
1310 mDeviceContext->GSSetShader(NULL, NULL, 0);
1311 }
1312
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001313 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001314 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001315}
1316
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001317void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001318{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001319 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1320 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001321
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001322 unsigned int totalRegisterCountVS = 0;
1323 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001324
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001325 bool vertexUniformsDirty = false;
1326 bool pixelUniformsDirty = false;
1327
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001328 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1329 {
1330 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001331
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001332 if (uniform->vsRegisterIndex >= 0)
1333 {
1334 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001335 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001336 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001337
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001338 if (uniform->psRegisterIndex >= 0)
1339 {
1340 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001341 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001342 }
1343 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001344
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001345 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1346 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1347
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001348 float (*mapVS)[4] = NULL;
1349 float (*mapPS)[4] = NULL;
1350
1351 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1352 {
1353 D3D11_MAPPED_SUBRESOURCE map = {0};
1354 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1355 ASSERT(SUCCEEDED(result));
1356 mapVS = (float(*)[4])map.pData;
1357 }
1358
1359 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1360 {
1361 D3D11_MAPPED_SUBRESOURCE map = {0};
1362 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1363 ASSERT(SUCCEEDED(result));
1364 mapPS = (float(*)[4])map.pData;
1365 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001366
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001367 for (gl::UniformArray::iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001368 {
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001369 gl::Uniform *uniform = *uniform_iterator;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001370
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001371 if (uniform->type != GL_SAMPLER_2D && uniform->type != GL_SAMPLER_CUBE)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001372 {
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001373 if (uniform->vsRegisterIndex >= 0 && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001374 {
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001375 memcpy(mapVS + uniform->vsRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4]));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001376 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001377
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001378 if (uniform->psRegisterIndex >= 0 && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001379 {
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001380 memcpy(mapPS + uniform->psRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4]));
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001381 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001382 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001383
1384 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001385 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001386
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001387 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001388 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001389 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001390 }
1391
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001392 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001393 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001394 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001395 }
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001396
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001397 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1398 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001399
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001400 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001401 if (!mDriverConstantBufferVS)
1402 {
1403 D3D11_BUFFER_DESC constantBufferDescription = {0};
1404 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1405 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1406 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1407 constantBufferDescription.CPUAccessFlags = 0;
1408 constantBufferDescription.MiscFlags = 0;
1409 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001410
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001411 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001412 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001413
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001414 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1415 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001416
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001417 if (!mDriverConstantBufferPS)
1418 {
1419 D3D11_BUFFER_DESC constantBufferDescription = {0};
1420 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1421 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1422 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1423 constantBufferDescription.CPUAccessFlags = 0;
1424 constantBufferDescription.MiscFlags = 0;
1425 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001426
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001427 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001428 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001429
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001430 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1431 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001432
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001433 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1434 {
1435 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1436 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1437 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001438
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001439 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1440 {
1441 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1442 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1443 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001444
1445 // needed for the point sprite geometry shader
1446 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001447}
1448
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001449void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001450{
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001451 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1452 bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1453 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1454 clearParams.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001455
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001456 unsigned int stencilUnmasked = 0x0;
1457 if (frameBuffer->hasStencil())
1458 {
1459 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1460 stencilUnmasked = (0x1 << stencilSize) - 1;
1461 }
1462 bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1463 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001464
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001465 bool needScissoredClear = mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1466 mCurScissor.x + mCurScissor.width < mRenderTargetDesc.width ||
1467 mCurScissor.y + mCurScissor.height < mRenderTargetDesc.height);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001468
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001469 if (needMaskedColorClear || needMaskedStencilClear || needScissoredClear)
1470 {
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001471 maskedClear(clearParams);
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001472 }
1473 else
1474 {
1475 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1476 {
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001477 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001478 {
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001479 if (frameBuffer->isEnabledColorAttachment(colorAttachment))
1480 {
1481 gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer(colorAttachment);
1482 if (renderbufferObject)
1483 {
1484 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
1485 if (!renderTarget)
1486 {
1487 ERR("render target pointer unexpectedly null.");
1488 return;
1489 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001490
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001491 ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView();
1492 if (!framebufferRTV)
1493 {
1494 ERR("render target view pointer unexpectedly null.");
1495 return;
1496 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001497
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001498 const float clearValues[4] = { clearParams.colorClearValue.red,
1499 clearParams.colorClearValue.green,
1500 clearParams.colorClearValue.blue,
1501 clearParams.colorClearValue.alpha };
1502 mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001503
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001504 framebufferRTV->Release();
1505 }
1506 }
1507 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001508 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001509 if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001510 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001511 gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer();
1512 if (renderbufferObject)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001513 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001514 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil());
1515 if (!renderTarget)
1516 {
1517 ERR("render target pointer unexpectedly null.");
1518 return;
1519 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001520
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001521 ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView();
1522 if (!framebufferDSV)
1523 {
1524 ERR("depth stencil view pointer unexpectedly null.");
1525 return;
1526 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001527
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001528 UINT clearFlags = 0;
1529 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1530 {
1531 clearFlags |= D3D11_CLEAR_DEPTH;
1532 }
1533 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1534 {
1535 clearFlags |= D3D11_CLEAR_STENCIL;
1536 }
1537
shannon.woods@transgaming.combe211b32013-02-28 23:17:16 +00001538 float depthClear = gl::clamp01(clearParams.depthClearValue);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001539 UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF;
1540
1541 mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001542
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001543 framebufferDSV->Release();
1544 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001545 }
1546 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001547}
1548
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001549void Renderer11::maskedClear(const gl::ClearParameters &clearParams)
1550{
1551 HRESULT result;
1552
1553 if (!mClearResourcesInitialized)
1554 {
1555 ASSERT(!mClearVB && !mClearVS && !mClearPS && !mClearScissorRS && !mClearNoScissorRS);
1556
1557 D3D11_BUFFER_DESC vbDesc;
1558 vbDesc.ByteWidth = sizeof(d3d11::PositionDepthColorVertex) * 4;
1559 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
1560 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
1561 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1562 vbDesc.MiscFlags = 0;
1563 vbDesc.StructureByteStride = 0;
1564
1565 result = mDevice->CreateBuffer(&vbDesc, NULL, &mClearVB);
1566 ASSERT(SUCCEEDED(result));
1567 d3d11::SetDebugName(mClearVB, "Renderer11 masked clear vertex buffer");
1568
1569 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
1570 {
1571 { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1572 { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1573 };
1574
1575 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Clear, sizeof(g_VS_Clear), &mClearIL);
1576 ASSERT(SUCCEEDED(result));
1577 d3d11::SetDebugName(mClearIL, "Renderer11 masked clear input layout");
1578
1579 result = mDevice->CreateVertexShader(g_VS_Clear, sizeof(g_VS_Clear), NULL, &mClearVS);
1580 ASSERT(SUCCEEDED(result));
1581 d3d11::SetDebugName(mClearVS, "Renderer11 masked clear vertex shader");
1582
1583 result = mDevice->CreatePixelShader(g_PS_Clear, sizeof(g_PS_Clear), NULL, &mClearPS);
1584 ASSERT(SUCCEEDED(result));
1585 d3d11::SetDebugName(mClearPS, "Renderer11 masked clear pixel shader");
1586
1587 D3D11_RASTERIZER_DESC rsScissorDesc;
1588 rsScissorDesc.FillMode = D3D11_FILL_SOLID;
1589 rsScissorDesc.CullMode = D3D11_CULL_NONE;
1590 rsScissorDesc.FrontCounterClockwise = FALSE;
1591 rsScissorDesc.DepthBias = 0;
1592 rsScissorDesc.DepthBiasClamp = 0.0f;
1593 rsScissorDesc.SlopeScaledDepthBias = 0.0f;
1594 rsScissorDesc.DepthClipEnable = FALSE;
1595 rsScissorDesc.ScissorEnable = TRUE;
1596 rsScissorDesc.MultisampleEnable = FALSE;
1597 rsScissorDesc.AntialiasedLineEnable = FALSE;
1598
1599 result = mDevice->CreateRasterizerState(&rsScissorDesc, &mClearScissorRS);
1600 ASSERT(SUCCEEDED(result));
1601 d3d11::SetDebugName(mClearScissorRS, "Renderer11 masked clear scissor rasterizer state");
1602
1603 D3D11_RASTERIZER_DESC rsNoScissorDesc;
1604 rsNoScissorDesc.FillMode = D3D11_FILL_SOLID;
1605 rsNoScissorDesc.CullMode = D3D11_CULL_NONE;
1606 rsNoScissorDesc.FrontCounterClockwise = FALSE;
1607 rsNoScissorDesc.DepthBias = 0;
1608 rsNoScissorDesc.DepthBiasClamp = 0.0f;
1609 rsNoScissorDesc.SlopeScaledDepthBias = 0.0f;
1610 rsNoScissorDesc.DepthClipEnable = FALSE;
1611 rsNoScissorDesc.ScissorEnable = FALSE;
1612 rsNoScissorDesc.MultisampleEnable = FALSE;
1613 rsNoScissorDesc.AntialiasedLineEnable = FALSE;
1614
1615 result = mDevice->CreateRasterizerState(&rsNoScissorDesc, &mClearNoScissorRS);
1616 ASSERT(SUCCEEDED(result));
1617 d3d11::SetDebugName(mClearNoScissorRS, "Renderer11 masked clear no scissor rasterizer state");
1618
1619 mClearResourcesInitialized = true;
1620 }
1621
1622 // Prepare the depth stencil state to write depth values if the depth should be cleared
1623 // and stencil values if the stencil should be cleared
1624 gl::DepthStencilState glDSState;
1625 glDSState.depthTest = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1626 glDSState.depthFunc = GL_ALWAYS;
1627 glDSState.depthMask = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1628 glDSState.stencilTest = (clearParams.mask & GL_STENCIL_BUFFER_BIT) != 0;
1629 glDSState.stencilFunc = GL_ALWAYS;
1630 glDSState.stencilMask = 0;
1631 glDSState.stencilFail = GL_REPLACE;
1632 glDSState.stencilPassDepthFail = GL_REPLACE;
1633 glDSState.stencilPassDepthPass = GL_REPLACE;
1634 glDSState.stencilWritemask = clearParams.stencilWriteMask;
1635 glDSState.stencilBackFunc = GL_ALWAYS;
1636 glDSState.stencilBackMask = 0;
1637 glDSState.stencilBackFail = GL_REPLACE;
1638 glDSState.stencilBackPassDepthFail = GL_REPLACE;
1639 glDSState.stencilBackPassDepthPass = GL_REPLACE;
1640 glDSState.stencilBackWritemask = clearParams.stencilWriteMask;
1641
1642 int stencilClear = clearParams.stencilClearValue & 0x000000FF;
1643
1644 ID3D11DepthStencilState *dsState = mStateCache.getDepthStencilState(glDSState);
1645
1646 // Prepare the blend state to use a write mask if the color buffer should be cleared
1647 gl::BlendState glBlendState;
1648 glBlendState.blend = false;
1649 glBlendState.sourceBlendRGB = GL_ONE;
1650 glBlendState.destBlendRGB = GL_ZERO;
1651 glBlendState.sourceBlendAlpha = GL_ONE;
1652 glBlendState.destBlendAlpha = GL_ZERO;
1653 glBlendState.blendEquationRGB = GL_FUNC_ADD;
1654 glBlendState.blendEquationAlpha = GL_FUNC_ADD;
1655 glBlendState.colorMaskRed = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskRed : false;
1656 glBlendState.colorMaskGreen = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskGreen : false;
1657 glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false;
shannon.woods@transgaming.com50ea9932013-02-28 23:13:40 +00001658 glBlendState.colorMaskAlpha = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskAlpha : false;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001659 glBlendState.sampleAlphaToCoverage = false;
1660 glBlendState.dither = false;
1661
1662 static const float blendFactors[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
1663 static const UINT sampleMask = 0xFFFFFFFF;
1664
1665 ID3D11BlendState *blendState = mStateCache.getBlendState(glBlendState);
1666
1667 // Set the vertices
1668 D3D11_MAPPED_SUBRESOURCE mappedResource;
1669 result = mDeviceContext->Map(mClearVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
1670 if (FAILED(result))
1671 {
1672 ERR("Failed to map masked clear vertex buffer, HRESULT: 0x%X.", result);
1673 return;
1674 }
1675
1676 d3d11::PositionDepthColorVertex *vertices = reinterpret_cast<d3d11::PositionDepthColorVertex*>(mappedResource.pData);
1677
1678 float depthClear = gl::clamp01(clearParams.depthClearValue);
1679 d3d11::SetPositionDepthColorVertex(&vertices[0], -1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1680 d3d11::SetPositionDepthColorVertex(&vertices[1], -1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1681 d3d11::SetPositionDepthColorVertex(&vertices[2], 1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1682 d3d11::SetPositionDepthColorVertex(&vertices[3], 1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1683
1684 mDeviceContext->Unmap(mClearVB, 0);
1685
1686 // Apply state
1687 mDeviceContext->OMSetBlendState(blendState, blendFactors, sampleMask);
1688 mDeviceContext->OMSetDepthStencilState(dsState, stencilClear);
1689 mDeviceContext->RSSetState(mScissorEnabled ? mClearScissorRS : mClearNoScissorRS);
1690
1691 // Apply shaders
1692 mDeviceContext->IASetInputLayout(mClearIL);
1693 mDeviceContext->VSSetShader(mClearVS, NULL, 0);
1694 mDeviceContext->PSSetShader(mClearPS, NULL, 0);
shannon.woods@transgaming.com2c53e472013-02-28 23:13:56 +00001695 mDeviceContext->GSSetShader(NULL, NULL, 0);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001696
1697 // Apply vertex buffer
1698 static UINT stride = sizeof(d3d11::PositionDepthColorVertex);
1699 static UINT startIdx = 0;
1700 mDeviceContext->IASetVertexBuffers(0, 1, &mClearVB, &stride, &startIdx);
1701 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
1702
1703 // Draw the clear quad
1704 mDeviceContext->Draw(4, 0);
1705
1706 // Clean up
1707 markAllStateDirty();
1708}
1709
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001710void Renderer11::markAllStateDirty()
1711{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001712 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1713 {
1714 mAppliedRenderTargetSerials[rtIndex] = 0;
1715 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001716 mAppliedDepthbufferSerial = 0;
1717 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001718 mDepthStencilInitialized = false;
1719 mRenderTargetDescInitialized = false;
1720
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001721 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001722 {
1723 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001724 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001725 }
1726 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1727 {
1728 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001729 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001730 }
1731
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001732 mForceSetBlendState = true;
1733 mForceSetRasterState = true;
1734 mForceSetDepthStencilState = true;
1735 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001736 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001737
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001738 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001739 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001740 mAppliedIBOffset = 0;
1741
daniel@transgaming.come4991412012-12-20 20:55:34 +00001742 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001743 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1744 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001745}
1746
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001747void Renderer11::releaseDeviceResources()
1748{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001749 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001750 mInputLayoutCache.clear();
1751
1752 delete mVertexDataManager;
1753 mVertexDataManager = NULL;
1754
1755 delete mIndexDataManager;
1756 mIndexDataManager = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001757
1758 delete mLineLoopIB;
1759 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001760
1761 delete mTriangleFanIB;
1762 mTriangleFanIB = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001763
1764 if (mCopyVB)
1765 {
1766 mCopyVB->Release();
1767 mCopyVB = NULL;
1768 }
1769
1770 if (mCopySampler)
1771 {
1772 mCopySampler->Release();
1773 mCopySampler = NULL;
1774 }
1775
1776 if (mCopyIL)
1777 {
1778 mCopyIL->Release();
1779 mCopyIL = NULL;
1780 }
1781
1782 if (mCopyVS)
1783 {
1784 mCopyVS->Release();
1785 mCopyVS = NULL;
1786 }
1787
1788 if (mCopyRGBAPS)
1789 {
1790 mCopyRGBAPS->Release();
1791 mCopyRGBAPS = NULL;
1792 }
1793
1794 if (mCopyRGBPS)
1795 {
1796 mCopyRGBPS->Release();
1797 mCopyRGBPS = NULL;
1798 }
1799
1800 if (mCopyLumPS)
1801 {
1802 mCopyLumPS->Release();
1803 mCopyLumPS = NULL;
1804 }
1805
1806 if (mCopyLumAlphaPS)
1807 {
1808 mCopyLumAlphaPS->Release();
1809 mCopyLumAlphaPS = NULL;
1810 }
1811
1812 mCopyResourcesInitialized = false;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001813
1814 if (mClearVB)
1815 {
1816 mClearVB->Release();
1817 mClearVB = NULL;
1818 }
1819
1820 if (mClearIL)
1821 {
1822 mClearIL->Release();
1823 mClearIL = NULL;
1824 }
1825
1826 if (mClearVS)
1827 {
1828 mClearVS->Release();
1829 mClearVS = NULL;
1830 }
1831
1832 if (mClearPS)
1833 {
1834 mClearPS->Release();
1835 mClearPS = NULL;
1836 }
1837
1838 if (mClearScissorRS)
1839 {
1840 mClearScissorRS->Release();
1841 mClearScissorRS = NULL;
1842 }
1843
1844 if (mClearNoScissorRS)
1845 {
1846 mClearNoScissorRS->Release();
1847 mClearNoScissorRS = NULL;
1848 }
1849
1850 mClearResourcesInitialized = false;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001851
1852 if (mDriverConstantBufferVS)
1853 {
1854 mDriverConstantBufferVS->Release();
1855 mDriverConstantBufferVS = NULL;
1856 }
1857
1858 if (mDriverConstantBufferPS)
1859 {
1860 mDriverConstantBufferPS->Release();
1861 mDriverConstantBufferPS = NULL;
1862 }
daniel@transgaming.combdf787f2013-02-01 03:20:36 +00001863
1864 if (mSyncQuery)
1865 {
1866 mSyncQuery->Release();
1867 mSyncQuery = NULL;
1868 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001869}
1870
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001871void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001872{
1873 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001874 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001875}
1876
1877bool Renderer11::isDeviceLost()
1878{
1879 return mDeviceLost;
1880}
1881
1882// set notify to true to broadcast a message to all contexts of the device loss
1883bool Renderer11::testDeviceLost(bool notify)
1884{
1885 bool isLost = false;
1886
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001887 // GetRemovedReason is used to test if the device is removed
1888 HRESULT result = mDevice->GetDeviceRemovedReason();
1889 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001890
1891 if (isLost)
1892 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001893 // Log error if this is a new device lost event
1894 if (mDeviceLost == false)
1895 {
1896 ERR("The D3D11 device was removed: 0x%08X", result);
1897 }
1898
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001899 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001900 // we'll probably get this done again by notifyDeviceLost
1901 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001902 // Note that we don't want to clear the device loss status here
1903 // -- this needs to be done by resetDevice
1904 mDeviceLost = true;
1905 if (notify)
1906 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001907 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001908 }
1909 }
1910
1911 return isLost;
1912}
1913
1914bool Renderer11::testDeviceResettable()
1915{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001916 // determine if the device is resettable by creating a dummy device
1917 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001918
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001919 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001920 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001921 return false;
1922 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001923
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001924 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001925 {
1926 D3D_FEATURE_LEVEL_11_0,
1927 D3D_FEATURE_LEVEL_10_1,
1928 D3D_FEATURE_LEVEL_10_0,
1929 };
1930
1931 ID3D11Device* dummyDevice;
1932 D3D_FEATURE_LEVEL dummyFeatureLevel;
1933 ID3D11DeviceContext* dummyContext;
1934
1935 HRESULT result = D3D11CreateDevice(NULL,
1936 D3D_DRIVER_TYPE_HARDWARE,
1937 NULL,
1938 #if defined(_DEBUG)
1939 D3D11_CREATE_DEVICE_DEBUG,
1940 #else
1941 0,
1942 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001943 featureLevels,
1944 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001945 D3D11_SDK_VERSION,
1946 &dummyDevice,
1947 &dummyFeatureLevel,
1948 &dummyContext);
1949
1950 if (!mDevice || FAILED(result))
1951 {
1952 return false;
1953 }
1954
1955 dummyContext->Release();
1956 dummyDevice->Release();
1957
1958 return true;
1959}
1960
1961void Renderer11::release()
1962{
1963 releaseDeviceResources();
1964
1965 if (mDxgiFactory)
1966 {
1967 mDxgiFactory->Release();
1968 mDxgiFactory = NULL;
1969 }
1970
1971 if (mDxgiAdapter)
1972 {
1973 mDxgiAdapter->Release();
1974 mDxgiAdapter = NULL;
1975 }
1976
1977 if (mDeviceContext)
1978 {
1979 mDeviceContext->ClearState();
1980 mDeviceContext->Flush();
1981 mDeviceContext->Release();
1982 mDeviceContext = NULL;
1983 }
1984
1985 if (mDevice)
1986 {
1987 mDevice->Release();
1988 mDevice = NULL;
1989 }
1990
1991 if (mD3d11Module)
1992 {
1993 FreeLibrary(mD3d11Module);
1994 mD3d11Module = NULL;
1995 }
1996
1997 if (mDxgiModule)
1998 {
1999 FreeLibrary(mDxgiModule);
2000 mDxgiModule = NULL;
2001 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002002}
2003
2004bool Renderer11::resetDevice()
2005{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00002006 // recreate everything
2007 release();
2008 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002009
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00002010 if (result != EGL_SUCCESS)
2011 {
2012 ERR("Could not reinitialize D3D11 device: %08X", result);
2013 return false;
2014 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002015
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002016 mDeviceLost = false;
2017
2018 return true;
2019}
2020
2021DWORD Renderer11::getAdapterVendor() const
2022{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00002023 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002024}
2025
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002026std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002027{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002028 std::ostringstream rendererString;
2029
2030 rendererString << mDescription;
2031 rendererString << " Direct3D11";
2032
2033 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
2034 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
2035
2036 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002037}
2038
2039GUID Renderer11::getAdapterIdentifier() const
2040{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00002041 // Use the adapter LUID as our adapter ID
2042 // This number is local to a machine is only guaranteed to be unique between restarts
2043 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
2044 GUID adapterId = {0};
2045 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
2046 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002047}
2048
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002049bool Renderer11::getBGRATextureSupport() const
2050{
2051 return mBGRATextureSupport;
2052}
2053
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002054bool Renderer11::getDXT1TextureSupport()
2055{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002056 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002057}
2058
2059bool Renderer11::getDXT3TextureSupport()
2060{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002061 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002062}
2063
2064bool Renderer11::getDXT5TextureSupport()
2065{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002066 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002067}
2068
2069bool Renderer11::getDepthTextureSupport() const
2070{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00002071 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002072}
2073
2074bool Renderer11::getFloat32TextureSupport(bool *filtering, bool *renderable)
2075{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00002076 *renderable = mFloat32RenderSupport;
2077 *filtering = mFloat32FilterSupport;
2078 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002079}
2080
2081bool Renderer11::getFloat16TextureSupport(bool *filtering, bool *renderable)
2082{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00002083 *renderable = mFloat16RenderSupport;
2084 *filtering = mFloat16FilterSupport;
2085 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002086}
2087
2088bool Renderer11::getLuminanceTextureSupport()
2089{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002090 return false;
2091}
2092
2093bool Renderer11::getLuminanceAlphaTextureSupport()
2094{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002095 return false;
2096}
2097
2098bool Renderer11::getTextureFilterAnisotropySupport() const
2099{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002100 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002101}
2102
2103float Renderer11::getTextureMaxAnisotropy() const
2104{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002105 switch (mFeatureLevel)
2106 {
2107 case D3D_FEATURE_LEVEL_11_0:
2108 return D3D11_MAX_MAXANISOTROPY;
2109 case D3D_FEATURE_LEVEL_10_1:
2110 case D3D_FEATURE_LEVEL_10_0:
2111 return D3D10_MAX_MAXANISOTROPY;
2112 default: UNREACHABLE();
2113 return 0;
2114 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002115}
2116
2117bool Renderer11::getEventQuerySupport()
2118{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002119 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002120}
2121
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002122Range Renderer11::getViewportBounds() const
2123{
2124 switch (mFeatureLevel)
2125 {
2126 case D3D_FEATURE_LEVEL_11_0:
2127 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
2128 case D3D_FEATURE_LEVEL_10_1:
2129 case D3D_FEATURE_LEVEL_10_0:
2130 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
2131 default: UNREACHABLE();
2132 return Range(0, 0);
2133 }
2134}
2135
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002136unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002137{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002138 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2139 switch (mFeatureLevel)
2140 {
2141 case D3D_FEATURE_LEVEL_11_0:
2142 case D3D_FEATURE_LEVEL_10_1:
2143 case D3D_FEATURE_LEVEL_10_0:
2144 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
2145 default: UNREACHABLE();
2146 return 0;
2147 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002148}
2149
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002150unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
2151{
2152 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2153}
2154
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002155unsigned int Renderer11::getReservedVertexUniformVectors() const
2156{
2157 return 0; // Driver uniforms are stored in a separate constant buffer
2158}
2159
2160unsigned int Renderer11::getReservedFragmentUniformVectors() const
2161{
2162 return 0; // Driver uniforms are stored in a separate constant buffer
2163}
2164
2165unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002166{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002167 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2168 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2169 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002170}
2171
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002172unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002173{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002174 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2175 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2176 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002177}
2178
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002179unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002180{
2181 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
2182 switch (mFeatureLevel)
2183 {
2184 case D3D_FEATURE_LEVEL_11_0:
2185 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2186 case D3D_FEATURE_LEVEL_10_1:
2187 case D3D_FEATURE_LEVEL_10_0:
2188 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2189 default: UNREACHABLE();
2190 return 0;
2191 }
2192}
2193
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002194unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2195{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002196 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2197 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2198
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002199 switch (mFeatureLevel)
2200 {
2201 case D3D_FEATURE_LEVEL_11_0:
2202 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT;
2203 case D3D_FEATURE_LEVEL_10_1:
2204 case D3D_FEATURE_LEVEL_10_0:
2205 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT;
2206 default: UNREACHABLE();
2207 return 0;
2208 }
2209}
2210
2211unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2212{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002213 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2214 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2215
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002216 switch (mFeatureLevel)
2217 {
2218 case D3D_FEATURE_LEVEL_11_0:
2219 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT;
2220 case D3D_FEATURE_LEVEL_10_1:
2221 case D3D_FEATURE_LEVEL_10_0:
2222 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT;
2223 default: UNREACHABLE();
2224 return 0;
2225 }
2226}
2227
2228unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2229{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002230 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2231 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2232
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002233 switch (mFeatureLevel)
2234 {
2235 case D3D_FEATURE_LEVEL_11_0:
2236 return D3D11_SO_BUFFER_SLOT_COUNT;
2237 case D3D_FEATURE_LEVEL_10_1:
2238 case D3D_FEATURE_LEVEL_10_0:
2239 return D3D10_SO_BUFFER_SLOT_COUNT;
2240 default: UNREACHABLE();
2241 return 0;
2242 }
2243}
2244
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002245bool Renderer11::getNonPower2TextureSupport() const
2246{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002247 switch (mFeatureLevel)
2248 {
2249 case D3D_FEATURE_LEVEL_11_0:
2250 case D3D_FEATURE_LEVEL_10_1:
2251 case D3D_FEATURE_LEVEL_10_0:
2252 return true;
2253 default: UNREACHABLE();
2254 return false;
2255 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002256}
2257
2258bool Renderer11::getOcclusionQuerySupport() const
2259{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002260 switch (mFeatureLevel)
2261 {
2262 case D3D_FEATURE_LEVEL_11_0:
2263 case D3D_FEATURE_LEVEL_10_1:
2264 case D3D_FEATURE_LEVEL_10_0:
2265 return true;
2266 default: UNREACHABLE();
2267 return false;
2268 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002269}
2270
2271bool Renderer11::getInstancingSupport() const
2272{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002273 switch (mFeatureLevel)
2274 {
2275 case D3D_FEATURE_LEVEL_11_0:
2276 case D3D_FEATURE_LEVEL_10_1:
2277 case D3D_FEATURE_LEVEL_10_0:
2278 return true;
2279 default: UNREACHABLE();
2280 return false;
2281 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002282}
2283
2284bool Renderer11::getShareHandleSupport() const
2285{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002286 // We only currently support share handles with BGRA surfaces, because
2287 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002288 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002289 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002290}
2291
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002292bool Renderer11::getDerivativeInstructionSupport() const
2293{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002294 switch (mFeatureLevel)
2295 {
2296 case D3D_FEATURE_LEVEL_11_0:
2297 case D3D_FEATURE_LEVEL_10_1:
2298 case D3D_FEATURE_LEVEL_10_0:
2299 return true;
2300 default: UNREACHABLE();
2301 return false;
2302 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002303}
2304
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002305bool Renderer11::getPostSubBufferSupport() const
2306{
2307 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2308 return false;
2309}
2310
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002311int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002312{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002313 switch (mFeatureLevel)
2314 {
2315 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002316 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002317 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2318 default: UNREACHABLE(); return 0;
2319 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002320}
2321
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002322int Renderer11::getMinorShaderModel() const
2323{
2324 switch (mFeatureLevel)
2325 {
2326 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2327 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2328 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2329 default: UNREACHABLE(); return 0;
2330 }
2331}
2332
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002333float Renderer11::getMaxPointSize() const
2334{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002335 // choose a reasonable maximum. we enforce this in the shader.
2336 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2337 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002338}
2339
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002340int Renderer11::getMaxViewportDimension() const
2341{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002342 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2343 // In our case return the maximum texture size, which is the maximum render buffer size.
2344 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2345 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2346
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002347 switch (mFeatureLevel)
2348 {
2349 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002350 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002351 case D3D_FEATURE_LEVEL_10_1:
2352 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002353 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002354 default: UNREACHABLE();
2355 return 0;
2356 }
2357}
2358
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002359int Renderer11::getMaxTextureWidth() const
2360{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002361 switch (mFeatureLevel)
2362 {
2363 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2364 case D3D_FEATURE_LEVEL_10_1:
2365 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2366 default: UNREACHABLE(); return 0;
2367 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002368}
2369
2370int Renderer11::getMaxTextureHeight() const
2371{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002372 switch (mFeatureLevel)
2373 {
2374 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2375 case D3D_FEATURE_LEVEL_10_1:
2376 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2377 default: UNREACHABLE(); return 0;
2378 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002379}
2380
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002381int Renderer11::getMaxTextureDepth() const
2382{
2383 switch (mFeatureLevel)
2384 {
2385 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2386 case D3D_FEATURE_LEVEL_10_1:
2387 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2388 default: UNREACHABLE(); return 0;
2389 }
2390}
2391
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002392bool Renderer11::get32BitIndexSupport() const
2393{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002394 switch (mFeatureLevel)
2395 {
2396 case D3D_FEATURE_LEVEL_11_0:
2397 case D3D_FEATURE_LEVEL_10_1:
2398 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2399 default: UNREACHABLE(); return false;
2400 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002401}
2402
2403int Renderer11::getMinSwapInterval() const
2404{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002405 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002406}
2407
2408int Renderer11::getMaxSwapInterval() const
2409{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002410 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002411}
2412
2413int Renderer11::getMaxSupportedSamples() const
2414{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002415 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002416}
2417
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002418int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2419{
2420 if (requested == 0)
2421 {
2422 return 0;
2423 }
2424
2425 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2426 if (iter != mMultisampleSupportMap.end())
2427 {
2428 const MultisampleSupportInfo& info = iter->second;
2429 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2430 {
2431 if (info.qualityLevels[i] > 0)
2432 {
2433 return i + 1;
2434 }
2435 }
2436 }
2437
2438 return -1;
2439}
2440
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002441unsigned int Renderer11::getMaxRenderTargets() const
2442{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002443 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2444 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2445
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002446 switch (mFeatureLevel)
2447 {
2448 case D3D_FEATURE_LEVEL_11_0:
2449 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2450 case D3D_FEATURE_LEVEL_10_1:
2451 case D3D_FEATURE_LEVEL_10_0:
2452 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2453 default:
2454 UNREACHABLE();
2455 return 1;
2456 }
2457}
2458
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002459bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002460{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002461 if (source && dest)
2462 {
2463 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2464 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2465
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002466 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002467 return true;
2468 }
2469
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002470 return false;
2471}
2472
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002473bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002474{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002475 if (source && dest)
2476 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002477 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2478 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002479
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002480 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002481 return true;
2482 }
2483
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002484 return false;
2485}
2486
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002487bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2488{
2489 if (source && dest)
2490 {
2491 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2492 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2493
2494 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2495 return true;
2496 }
2497
2498 return false;
2499}
2500
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002501bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2502{
2503 if (source && dest)
2504 {
2505 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2506 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2507
2508 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2509 return true;
2510 }
2511
2512 return false;
2513}
2514
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002515bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002516 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002517{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002518 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002519 if (!colorbuffer)
2520 {
2521 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002522 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002523 }
2524
2525 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2526 if (!sourceRenderTarget)
2527 {
2528 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002529 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002530 }
2531
2532 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2533 if (!source)
2534 {
2535 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002536 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002537 }
2538
2539 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2540 if (!storage11)
2541 {
2542 source->Release();
2543 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002544 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002545 }
2546
2547 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2548 if (!destRenderTarget)
2549 {
2550 source->Release();
2551 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002552 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002553 }
2554
2555 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2556 if (!dest)
2557 {
2558 source->Release();
2559 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002560 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002561 }
2562
2563 gl::Rectangle destRect;
2564 destRect.x = xoffset;
2565 destRect.y = yoffset;
2566 destRect.width = sourceRect.width;
2567 destRect.height = sourceRect.height;
2568
2569 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
2570 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
2571
2572 source->Release();
2573 dest->Release();
2574
2575 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002576}
2577
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002578bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002579 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002580{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002581 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002582 if (!colorbuffer)
2583 {
2584 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002585 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002586 }
2587
2588 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2589 if (!sourceRenderTarget)
2590 {
2591 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002592 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002593 }
2594
2595 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2596 if (!source)
2597 {
2598 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002599 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002600 }
2601
2602 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2603 if (!storage11)
2604 {
2605 source->Release();
2606 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002607 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002608 }
2609
2610 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(target, level));
2611 if (!destRenderTarget)
2612 {
2613 source->Release();
2614 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002615 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002616 }
2617
2618 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2619 if (!dest)
2620 {
2621 source->Release();
2622 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002623 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002624 }
2625
2626 gl::Rectangle destRect;
2627 destRect.x = xoffset;
2628 destRect.y = yoffset;
2629 destRect.width = sourceRect.width;
2630 destRect.height = sourceRect.height;
2631
2632 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
2633 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
2634
2635 source->Release();
2636 dest->Release();
2637
2638 return ret;
2639}
2640
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002641bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2642 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2643{
2644 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2645 if (!colorbuffer)
2646 {
2647 ERR("Failed to retrieve the color buffer from the frame buffer.");
2648 return gl::error(GL_OUT_OF_MEMORY, false);
2649 }
2650
2651 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2652 if (!sourceRenderTarget)
2653 {
2654 ERR("Failed to retrieve the render target from the frame buffer.");
2655 return gl::error(GL_OUT_OF_MEMORY, false);
2656 }
2657
2658 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2659 if (!source)
2660 {
2661 ERR("Failed to retrieve the render target view from the render target.");
2662 return gl::error(GL_OUT_OF_MEMORY, false);
2663 }
2664
2665 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2666 if (!storage11)
2667 {
2668 source->Release();
2669 ERR("Failed to retrieve the texture storage from the destination.");
2670 return gl::error(GL_OUT_OF_MEMORY, false);
2671 }
2672
2673 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2674 if (!destRenderTarget)
2675 {
2676 source->Release();
2677 ERR("Failed to retrieve the render target from the destination storage.");
2678 return gl::error(GL_OUT_OF_MEMORY, false);
2679 }
2680
2681 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2682 if (!dest)
2683 {
2684 source->Release();
2685 ERR("Failed to retrieve the render target view from the destination render target.");
2686 return gl::error(GL_OUT_OF_MEMORY, false);
2687 }
2688
2689 gl::Rectangle destRect;
2690 destRect.x = xoffset;
2691 destRect.y = yoffset;
2692 destRect.width = sourceRect.width;
2693 destRect.height = sourceRect.height;
2694
2695 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
2696 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
2697
2698 source->Release();
2699 dest->Release();
2700
2701 return ret;
2702}
2703
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002704bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2705 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2706{
2707 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2708 if (!colorbuffer)
2709 {
2710 ERR("Failed to retrieve the color buffer from the frame buffer.");
2711 return gl::error(GL_OUT_OF_MEMORY, false);
2712 }
2713
2714 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2715 if (!sourceRenderTarget)
2716 {
2717 ERR("Failed to retrieve the render target from the frame buffer.");
2718 return gl::error(GL_OUT_OF_MEMORY, false);
2719 }
2720
2721 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2722 if (!source)
2723 {
2724 ERR("Failed to retrieve the render target view from the render target.");
2725 return gl::error(GL_OUT_OF_MEMORY, false);
2726 }
2727
2728 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2729 if (!storage11)
2730 {
2731 source->Release();
2732 ERR("Failed to retrieve the texture storage from the destination.");
2733 return gl::error(GL_OUT_OF_MEMORY, false);
2734 }
2735
2736 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2737 if (!destRenderTarget)
2738 {
2739 source->Release();
2740 ERR("Failed to retrieve the render target from the destination storage.");
2741 return gl::error(GL_OUT_OF_MEMORY, false);
2742 }
2743
2744 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2745 if (!dest)
2746 {
2747 source->Release();
2748 ERR("Failed to retrieve the render target view from the destination render target.");
2749 return gl::error(GL_OUT_OF_MEMORY, false);
2750 }
2751
2752 gl::Rectangle destRect;
2753 destRect.x = xoffset;
2754 destRect.y = yoffset;
2755 destRect.width = sourceRect.width;
2756 destRect.height = sourceRect.height;
2757
2758 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
2759 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
2760
2761 source->Release();
2762 dest->Release();
2763
2764 return ret;
2765}
2766
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002767bool Renderer11::copyTexture(ID3D11ShaderResourceView *source, const gl::Rectangle &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight,
2768 ID3D11RenderTargetView *dest, const gl::Rectangle &destArea, unsigned int destWidth, unsigned int destHeight, GLenum destFormat)
2769{
2770 HRESULT result;
2771
2772 if (!mCopyResourcesInitialized)
2773 {
2774 ASSERT(!mCopyVB && !mCopySampler && !mCopyIL && !mCopyVS && !mCopyRGBAPS && !mCopyRGBPS && !mCopyLumPS && !mCopyLumAlphaPS);
2775
2776 D3D11_BUFFER_DESC vbDesc;
2777 vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4;
2778 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
2779 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
2780 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
2781 vbDesc.MiscFlags = 0;
2782 vbDesc.StructureByteStride = 0;
2783
2784 result = mDevice->CreateBuffer(&vbDesc, NULL, &mCopyVB);
2785 ASSERT(SUCCEEDED(result));
2786 d3d11::SetDebugName(mCopyVB, "Renderer11 copy texture vertex buffer");
2787
2788 D3D11_SAMPLER_DESC samplerDesc;
2789 samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
2790 samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
2791 samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
2792 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
2793 samplerDesc.MipLODBias = 0.0f;
2794 samplerDesc.MaxAnisotropy = 0;
2795 samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
2796 samplerDesc.BorderColor[0] = 0.0f;
2797 samplerDesc.BorderColor[1] = 0.0f;
2798 samplerDesc.BorderColor[2] = 0.0f;
2799 samplerDesc.BorderColor[3] = 0.0f;
2800 samplerDesc.MinLOD = 0.0f;
2801 samplerDesc.MaxLOD = 0.0f;
2802
2803 result = mDevice->CreateSamplerState(&samplerDesc, &mCopySampler);
2804 ASSERT(SUCCEEDED(result));
2805 d3d11::SetDebugName(mCopySampler, "Renderer11 copy sampler");
2806
2807 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
2808 {
2809 { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2810 { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2811 };
2812
2813 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mCopyIL);
2814 ASSERT(SUCCEEDED(result));
2815 d3d11::SetDebugName(mCopyIL, "Renderer11 copy texture input layout");
2816
2817 result = mDevice->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mCopyVS);
2818 ASSERT(SUCCEEDED(result));
2819 d3d11::SetDebugName(mCopyVS, "Renderer11 copy texture vertex shader");
2820
2821 result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mCopyRGBAPS);
2822 ASSERT(SUCCEEDED(result));
2823 d3d11::SetDebugName(mCopyRGBAPS, "Renderer11 copy texture RGBA pixel shader");
2824
2825 result = mDevice->CreatePixelShader(g_PS_PassthroughRGB, sizeof(g_PS_PassthroughRGB), NULL, &mCopyRGBPS);
2826 ASSERT(SUCCEEDED(result));
2827 d3d11::SetDebugName(mCopyRGBPS, "Renderer11 copy texture RGB pixel shader");
2828
2829 result = mDevice->CreatePixelShader(g_PS_PassthroughLum, sizeof(g_PS_PassthroughLum), NULL, &mCopyLumPS);
2830 ASSERT(SUCCEEDED(result));
2831 d3d11::SetDebugName(mCopyLumPS, "Renderer11 copy texture luminance pixel shader");
2832
2833 result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha, sizeof(g_PS_PassthroughLumAlpha), NULL, &mCopyLumAlphaPS);
2834 ASSERT(SUCCEEDED(result));
2835 d3d11::SetDebugName(mCopyLumAlphaPS, "Renderer11 copy texture luminance alpha pixel shader");
2836
2837 mCopyResourcesInitialized = true;
2838 }
2839
2840 // Verify the source and destination area sizes
2841 if (sourceArea.x < 0 || sourceArea.x + sourceArea.width > static_cast<int>(sourceWidth) ||
2842 sourceArea.y < 0 || sourceArea.y + sourceArea.height > static_cast<int>(sourceHeight) ||
2843 destArea.x < 0 || destArea.x + destArea.width > static_cast<int>(destWidth) ||
2844 destArea.y < 0 || destArea.y + destArea.height > static_cast<int>(destHeight))
2845 {
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002846 return gl::error(GL_INVALID_VALUE, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002847 }
2848
2849 // Set vertices
2850 D3D11_MAPPED_SUBRESOURCE mappedResource;
2851 result = mDeviceContext->Map(mCopyVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
2852 if (FAILED(result))
2853 {
2854 ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002855 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002856 }
2857
2858 d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData);
2859
2860 // Create a quad in homogeneous coordinates
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00002861 float x1 = (destArea.x / float(destWidth)) * 2.0f - 1.0f;
shannon.woods@transgaming.comcfdfd462013-02-28 23:13:01 +00002862 float y1 = ((destHeight - destArea.y - destArea.height) / float(destHeight)) * 2.0f - 1.0f;
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00002863 float x2 = ((destArea.x + destArea.width) / float(destWidth)) * 2.0f - 1.0f;
shannon.woods@transgaming.comcfdfd462013-02-28 23:13:01 +00002864 float y2 = ((destHeight - destArea.y) / float(destHeight)) * 2.0f - 1.0f;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002865
2866 float u1 = sourceArea.x / float(sourceWidth);
2867 float v1 = sourceArea.y / float(sourceHeight);
2868 float u2 = (sourceArea.x + sourceArea.width) / float(sourceWidth);
2869 float v2 = (sourceArea.y + sourceArea.height) / float(sourceHeight);
2870
2871 d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2);
2872 d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1);
2873 d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2);
2874 d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1);
2875
2876 mDeviceContext->Unmap(mCopyVB, 0);
2877
2878 static UINT stride = sizeof(d3d11::PositionTexCoordVertex);
2879 static UINT startIdx = 0;
2880 mDeviceContext->IASetVertexBuffers(0, 1, &mCopyVB, &stride, &startIdx);
2881
2882 // Apply state
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00002883 mDeviceContext->OMSetBlendState(NULL, NULL, 0xFFFFFFF);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002884 mDeviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF);
2885 mDeviceContext->RSSetState(NULL);
2886
2887 // Apply shaders
2888 mDeviceContext->IASetInputLayout(mCopyIL);
2889 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
2890 mDeviceContext->VSSetShader(mCopyVS, NULL, 0);
2891
2892 ID3D11PixelShader *ps = NULL;
2893 switch(destFormat)
2894 {
2895 case GL_RGBA: ps = mCopyRGBAPS; break;
2896 case GL_RGB: ps = mCopyRGBPS; break;
2897 case GL_ALPHA: ps = mCopyRGBAPS; break;
2898 case GL_BGRA_EXT: ps = mCopyRGBAPS; break;
2899 case GL_LUMINANCE: ps = mCopyLumPS; break;
2900 case GL_LUMINANCE_ALPHA: ps = mCopyLumAlphaPS; break;
2901 default: UNREACHABLE(); ps = NULL; break;
2902 }
2903
2904 mDeviceContext->PSSetShader(ps, NULL, 0);
shannon.woods@transgaming.com28e7ba02013-02-28 23:09:28 +00002905 mDeviceContext->GSSetShader(NULL, NULL, 0);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002906
2907 // Unset the currently bound shader resource to avoid conflicts
2908 static ID3D11ShaderResourceView *const nullSRV = NULL;
2909 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
2910
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002911 // Apply render target
2912 setOneTimeRenderTarget(dest);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002913
2914 // Set the viewport
2915 D3D11_VIEWPORT viewport;
2916 viewport.TopLeftX = 0;
2917 viewport.TopLeftY = 0;
2918 viewport.Width = destWidth;
2919 viewport.Height = destHeight;
2920 viewport.MinDepth = 0.0f;
2921 viewport.MaxDepth = 1.0f;
2922 mDeviceContext->RSSetViewports(1, &viewport);
2923
2924 // Apply textures
2925 mDeviceContext->PSSetShaderResources(0, 1, &source);
2926 mDeviceContext->PSSetSamplers(0, 1, &mCopySampler);
2927
2928 // Draw the quad
2929 mDeviceContext->Draw(4, 0);
2930
2931 // Unbind textures and render targets and vertex buffer
2932 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
2933
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002934 unapplyRenderTargets();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002935
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002936 UINT zero = 0;
2937 ID3D11Buffer *const nullBuffer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002938 mDeviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
2939
2940 markAllStateDirty();
2941
2942 return true;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002943}
2944
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002945void Renderer11::unapplyRenderTargets()
2946{
2947 setOneTimeRenderTarget(NULL);
2948}
2949
2950void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2951{
2952 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2953
2954 rtvArray[0] = renderTargetView;
2955
2956 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2957
2958 // Do not preserve the serial for this one-time-use render target
2959 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2960 {
2961 mAppliedRenderTargetSerials[rtIndex] = 0;
2962 }
2963}
2964
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002965RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2966{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002967 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002968 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002969
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002970 if (depth)
2971 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002972 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002973 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
2974 swapChain11->getDepthStencilTexture(), NULL,
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002975 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002976 }
2977 else
2978 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002979 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002980 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002981 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002982 swapChain11->getRenderTargetShaderResource(),
2983 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002984 }
2985 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002986}
2987
2988RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2989{
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002990 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples, depth);
2991 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002992}
2993
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002994ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002995{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002996 ShaderExecutable11 *executable = NULL;
2997
2998 switch (type)
2999 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003000 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003001 {
3002 ID3D11VertexShader *vshader = NULL;
3003 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
3004 ASSERT(SUCCEEDED(result));
3005
3006 if (vshader)
3007 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003008 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003009 }
3010 }
3011 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003012 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003013 {
3014 ID3D11PixelShader *pshader = NULL;
3015 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
3016 ASSERT(SUCCEEDED(result));
3017
3018 if (pshader)
3019 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003020 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003021 }
3022 }
3023 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00003024 case rx::SHADER_GEOMETRY:
3025 {
3026 ID3D11GeometryShader *gshader = NULL;
3027 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
3028 ASSERT(SUCCEEDED(result));
3029
3030 if (gshader)
3031 {
3032 executable = new ShaderExecutable11(function, length, gshader);
3033 }
3034 }
3035 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003036 default:
3037 UNREACHABLE();
3038 break;
3039 }
3040
3041 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00003042}
3043
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003044ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003045{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003046 const char *profile = NULL;
3047
3048 switch (type)
3049 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003050 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003051 profile = "vs_4_0";
3052 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003053 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003054 profile = "ps_4_0";
3055 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00003056 case rx::SHADER_GEOMETRY:
3057 profile = "gs_4_0";
3058 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003059 default:
3060 UNREACHABLE();
3061 return NULL;
3062 }
3063
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00003064 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003065 if (!binary)
3066 return NULL;
3067
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003068 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003069 binary->Release();
3070
3071 return executable;
3072}
3073
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00003074VertexBuffer *Renderer11::createVertexBuffer()
3075{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00003076 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00003077}
3078
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00003079IndexBuffer *Renderer11::createIndexBuffer()
3080{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00003081 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003082}
3083
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00003084BufferStorage *Renderer11::createBufferStorage()
3085{
3086 return new BufferStorage11(this);
3087}
3088
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003089QueryImpl *Renderer11::createQuery(GLenum type)
3090{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00003091 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003092}
3093
3094FenceImpl *Renderer11::createFence()
3095{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00003096 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00003097}
3098
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003099bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003100{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003101 ASSERT(colorbuffer != NULL);
3102
3103 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
3104 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003105 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003106 *subresourceIndex = renderTarget->getSubresourceIndex();
3107
3108 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
3109 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003110 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003111 ID3D11Resource *textureResource = NULL;
3112 colorBufferRTV->GetResource(&textureResource);
3113 colorBufferRTV->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003114
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003115 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003116 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003117 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
3118 textureResource->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003119
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003120 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003121 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003122 return true;
3123 }
3124 else
3125 {
3126 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
3127 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003128 }
3129 }
3130 }
3131 }
3132
3133 return false;
3134}
3135
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003136bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003137 bool blitRenderTarget, bool blitDepthStencil)
3138{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003139 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003140 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003141 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003142
3143 if (!readBuffer)
3144 {
3145 ERR("Failed to retrieve the read buffer from the read framebuffer.");
3146 return gl::error(GL_OUT_OF_MEMORY, false);
3147 }
3148
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003149 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003150
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003151 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003152 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003153 if (drawTarget->isEnabledColorAttachment(colorAttachment))
3154 {
3155 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
3156
3157 if (!drawBuffer)
3158 {
3159 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
3160 return gl::error(GL_OUT_OF_MEMORY, false);
3161 }
3162
3163 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
3164
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003165 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003166 {
3167 return false;
3168 }
3169 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003170 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003171 }
3172
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003173 if (blitDepthStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003174 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003175 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
3176 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
3177
3178 if (!readBuffer)
3179 {
3180 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
3181 return gl::error(GL_OUT_OF_MEMORY, false);
3182 }
3183
3184 if (!drawBuffer)
3185 {
3186 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
3187 return gl::error(GL_OUT_OF_MEMORY, false);
3188 }
3189
3190 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3191 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3192
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003193 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, true))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003194 {
3195 return false;
3196 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003197 }
3198
3199 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003200}
3201
3202void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3203 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3204{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003205 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003206 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003207
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003208 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3209
3210 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003211 {
3212 gl::Rectangle area;
3213 area.x = x;
3214 area.y = y;
3215 area.width = width;
3216 area.height = height;
3217
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003218 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3219 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003220
3221 colorBufferTexture->Release();
3222 colorBufferTexture = NULL;
3223 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003224}
3225
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003226Image *Renderer11::createImage()
3227{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003228 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003229}
3230
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003231void Renderer11::generateMipmap(Image *dest, Image *src)
3232{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003233 Image11 *dest11 = Image11::makeImage11(dest);
3234 Image11 *src11 = Image11::makeImage11(src);
3235 Image11::generateMipmap(dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003236}
3237
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003238TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3239{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003240 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3241 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003242}
3243
3244TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3245{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003246 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003247}
3248
3249TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3250{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003251 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003252}
3253
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003254TextureStorage *Renderer11::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3255{
3256 return new TextureStorage11_3D(this, levels, internalformat, usage, width, height, depth);
3257}
3258
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003259TextureStorage *Renderer11::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3260{
3261 return new TextureStorage11_2DArray(this, levels, internalformat, usage, width, height, depth);
3262}
3263
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003264static inline unsigned int getFastPixelCopySize(DXGI_FORMAT sourceFormat, GLenum destFormat, GLenum destType)
3265{
3266 if (sourceFormat == DXGI_FORMAT_A8_UNORM &&
3267 destFormat == GL_ALPHA &&
3268 destType == GL_UNSIGNED_BYTE)
3269 {
3270 return 1;
3271 }
3272 else if (sourceFormat == DXGI_FORMAT_R8G8B8A8_UNORM &&
3273 destFormat == GL_RGBA &&
3274 destType == GL_UNSIGNED_BYTE)
3275 {
3276 return 4;
3277 }
3278 else if (sourceFormat == DXGI_FORMAT_B8G8R8A8_UNORM &&
3279 destFormat == GL_BGRA_EXT &&
3280 destType == GL_UNSIGNED_BYTE)
3281 {
3282 return 4;
3283 }
3284 else if (sourceFormat == DXGI_FORMAT_R16G16B16A16_FLOAT &&
3285 destFormat == GL_RGBA &&
3286 destType == GL_HALF_FLOAT_OES)
3287 {
3288 return 8;
3289 }
3290 else if (sourceFormat == DXGI_FORMAT_R32G32B32_FLOAT &&
3291 destFormat == GL_RGB &&
3292 destType == GL_FLOAT)
3293 {
3294 return 12;
3295 }
3296 else if (sourceFormat == DXGI_FORMAT_R32G32B32A32_FLOAT &&
3297 destFormat == GL_RGBA &&
3298 destType == GL_FLOAT)
3299 {
3300 return 16;
3301 }
3302 else
3303 {
3304 return 0;
3305 }
3306}
3307
3308static inline void readPixelColor(const unsigned char *data, DXGI_FORMAT format, unsigned int x,
3309 unsigned int y, int inputPitch, gl::Color *outColor)
3310{
3311 switch (format)
3312 {
3313 case DXGI_FORMAT_R8G8B8A8_UNORM:
shannon.woods%transgaming.com@gtempaccount.com8dce6512013-04-13 03:42:19 +00003314 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003315 {
3316 unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
shannon.woods@transgaming.com25950ea2013-02-28 23:04:15 +00003317 outColor->red = (rgba & 0x000000FF) * (1.0f / 0x000000FF);
3318 outColor->green = (rgba & 0x0000FF00) * (1.0f / 0x0000FF00);
3319 outColor->blue = (rgba & 0x00FF0000) * (1.0f / 0x00FF0000);
3320 outColor->alpha = (rgba & 0xFF000000) * (1.0f / 0xFF000000);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003321 }
3322 break;
3323
3324 case DXGI_FORMAT_A8_UNORM:
3325 {
3326 outColor->red = 0.0f;
3327 outColor->green = 0.0f;
3328 outColor->blue = 0.0f;
3329 outColor->alpha = *(data + x + y * inputPitch) / 255.0f;
3330 }
3331 break;
3332
3333 case DXGI_FORMAT_R32G32B32A32_FLOAT:
3334 {
3335 outColor->red = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 0);
3336 outColor->green = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 1);
3337 outColor->blue = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 2);
3338 outColor->alpha = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 3);
3339 }
3340 break;
3341
3342 case DXGI_FORMAT_R32G32B32_FLOAT:
3343 {
3344 outColor->red = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 0);
3345 outColor->green = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 1);
3346 outColor->blue = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 2);
3347 outColor->alpha = 1.0f;
3348 }
3349 break;
3350
3351 case DXGI_FORMAT_R16G16B16A16_FLOAT:
3352 {
3353 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 0));
3354 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 1));
3355 outColor->blue = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 2));
3356 outColor->alpha = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 3));
3357 }
3358 break;
3359
3360 case DXGI_FORMAT_B8G8R8A8_UNORM:
3361 {
3362 unsigned int bgra = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
shannon.woods@transgaming.com25950ea2013-02-28 23:04:15 +00003363 outColor->red = (bgra & 0x00FF0000) * (1.0f / 0x00FF0000);
3364 outColor->blue = (bgra & 0x000000FF) * (1.0f / 0x000000FF);
3365 outColor->green = (bgra & 0x0000FF00) * (1.0f / 0x0000FF00);
3366 outColor->alpha = (bgra & 0xFF000000) * (1.0f / 0xFF000000);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003367 }
3368 break;
3369
3370 case DXGI_FORMAT_R8_UNORM:
3371 {
3372 outColor->red = *(data + x + y * inputPitch) / 255.0f;
3373 outColor->green = 0.0f;
3374 outColor->blue = 0.0f;
3375 outColor->alpha = 1.0f;
3376 }
3377 break;
3378
3379 case DXGI_FORMAT_R8G8_UNORM:
3380 {
3381 unsigned short rg = *reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch);
3382
3383 outColor->red = (rg & 0xFF00) * (1.0f / 0xFF00);
3384 outColor->green = (rg & 0x00FF) * (1.0f / 0x00FF);
3385 outColor->blue = 0.0f;
3386 outColor->alpha = 1.0f;
3387 }
3388 break;
3389
3390 case DXGI_FORMAT_R16_FLOAT:
3391 {
3392 outColor->red = gl::float16ToFloat32(*reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch));
3393 outColor->green = 0.0f;
3394 outColor->blue = 0.0f;
3395 outColor->alpha = 1.0f;
3396 }
3397 break;
3398
3399 case DXGI_FORMAT_R16G16_FLOAT:
3400 {
3401 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 0));
3402 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 1));
3403 outColor->blue = 0.0f;
3404 outColor->alpha = 1.0f;
3405 }
3406 break;
3407
shannon.woods%transgaming.com@gtempaccount.com8dce6512013-04-13 03:42:19 +00003408 case DXGI_FORMAT_R10G10B10A2_UNORM:
3409 {
3410 unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
3411 outColor->red = (rgba & 0x000003FF) * (1.0f / 0x000003FF);
3412 outColor->green = (rgba & 0x000FFC00) * (1.0f / 0x000FFC00);
3413 outColor->blue = (rgba & 0x3FF00000) * (1.0f / 0x3FF00000);
3414 outColor->alpha = (rgba & 0xC0000000) * (1.0f / 0xC0000000);
3415 }
3416 break;
3417
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003418 default:
3419 ERR("ReadPixelColor not implemented for DXGI format %u.", format);
3420 UNIMPLEMENTED();
3421 break;
3422 }
3423}
3424
3425static inline void writePixelColor(const gl::Color &color, GLenum format, GLenum type, unsigned int x,
3426 unsigned int y, int outputPitch, void *outData)
3427{
3428 unsigned char* byteData = reinterpret_cast<unsigned char*>(outData);
3429 unsigned short* shortData = reinterpret_cast<unsigned short*>(outData);
3430
3431 switch (format)
3432 {
3433 case GL_RGBA:
3434 switch (type)
3435 {
3436 case GL_UNSIGNED_BYTE:
3437 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
3438 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3439 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3440 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
3441 break;
3442
3443 default:
3444 ERR("WritePixelColor not implemented for format GL_RGBA and type 0x%X.", type);
3445 UNIMPLEMENTED();
3446 break;
3447 }
3448 break;
3449
3450 case GL_BGRA_EXT:
3451 switch (type)
3452 {
3453 case GL_UNSIGNED_BYTE:
3454 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3455 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3456 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.red + 0.5f);
3457 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
3458 break;
3459
3460 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
3461 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
3462 // this type is packed as follows:
3463 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3464 // --------------------------------------------------------------------------------
3465 // | 4th | 3rd | 2nd | 1st component |
3466 // --------------------------------------------------------------------------------
3467 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
3468 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3469 (static_cast<unsigned short>(15 * color.alpha + 0.5f) << 12) |
3470 (static_cast<unsigned short>(15 * color.red + 0.5f) << 8) |
3471 (static_cast<unsigned short>(15 * color.green + 0.5f) << 4) |
3472 (static_cast<unsigned short>(15 * color.blue + 0.5f) << 0);
3473 break;
3474
3475 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
3476 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
3477 // this type is packed as follows:
3478 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3479 // --------------------------------------------------------------------------------
3480 // | 4th | 3rd | 2nd | 1st component |
3481 // --------------------------------------------------------------------------------
3482 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
3483 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3484 (static_cast<unsigned short>( color.alpha + 0.5f) << 15) |
3485 (static_cast<unsigned short>(31 * color.red + 0.5f) << 10) |
3486 (static_cast<unsigned short>(31 * color.green + 0.5f) << 5) |
3487 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0);
3488 break;
3489
3490 default:
3491 ERR("WritePixelColor not implemented for format GL_BGRA_EXT and type 0x%X.", type);
3492 UNIMPLEMENTED();
3493 break;
3494 }
3495 break;
3496
3497 case GL_RGB:
3498 switch (type)
3499 {
3500 case GL_UNSIGNED_SHORT_5_6_5:
3501 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3502 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0) |
3503 (static_cast<unsigned short>(63 * color.green + 0.5f) << 5) |
3504 (static_cast<unsigned short>(31 * color.red + 0.5f) << 11);
3505 break;
3506
3507 case GL_UNSIGNED_BYTE:
3508 byteData[3 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
3509 byteData[3 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3510 byteData[3 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3511 break;
3512
3513 default:
3514 ERR("WritePixelColor not implemented for format GL_RGB and type 0x%X.", type);
3515 UNIMPLEMENTED();
3516 break;
3517 }
3518 break;
3519
3520 default:
3521 ERR("WritePixelColor not implemented for format 0x%X.", format);
3522 UNIMPLEMENTED();
3523 break;
3524 }
3525}
3526
3527void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3528 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3529 GLint packAlignment, void *pixels)
3530{
3531 D3D11_TEXTURE2D_DESC textureDesc;
3532 texture->GetDesc(&textureDesc);
3533
3534 D3D11_TEXTURE2D_DESC stagingDesc;
3535 stagingDesc.Width = area.width;
3536 stagingDesc.Height = area.height;
3537 stagingDesc.MipLevels = 1;
3538 stagingDesc.ArraySize = 1;
3539 stagingDesc.Format = textureDesc.Format;
3540 stagingDesc.SampleDesc.Count = 1;
3541 stagingDesc.SampleDesc.Quality = 0;
3542 stagingDesc.Usage = D3D11_USAGE_STAGING;
3543 stagingDesc.BindFlags = 0;
3544 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3545 stagingDesc.MiscFlags = 0;
3546
3547 ID3D11Texture2D* stagingTex = NULL;
3548 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3549 if (FAILED(result))
3550 {
3551 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3552 return;
3553 }
3554
3555 ID3D11Texture2D* srcTex = NULL;
3556 if (textureDesc.SampleDesc.Count > 1)
3557 {
3558 D3D11_TEXTURE2D_DESC resolveDesc;
3559 resolveDesc.Width = textureDesc.Width;
3560 resolveDesc.Height = textureDesc.Height;
3561 resolveDesc.MipLevels = 1;
3562 resolveDesc.ArraySize = 1;
3563 resolveDesc.Format = textureDesc.Format;
3564 resolveDesc.SampleDesc.Count = 1;
3565 resolveDesc.SampleDesc.Quality = 0;
3566 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3567 resolveDesc.BindFlags = 0;
3568 resolveDesc.CPUAccessFlags = 0;
3569 resolveDesc.MiscFlags = 0;
3570
3571 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3572 if (FAILED(result))
3573 {
3574 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
3575 stagingTex->Release();
3576 return;
3577 }
3578
3579 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3580 subResource = 0;
3581 }
3582 else
3583 {
3584 srcTex = texture;
3585 srcTex->AddRef();
3586 }
3587
3588 D3D11_BOX srcBox;
3589 srcBox.left = area.x;
3590 srcBox.right = area.x + area.width;
3591 srcBox.top = area.y;
3592 srcBox.bottom = area.y + area.height;
3593 srcBox.front = 0;
3594 srcBox.back = 1;
3595
3596 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3597
3598 srcTex->Release();
3599 srcTex = NULL;
3600
3601 D3D11_MAPPED_SUBRESOURCE mapping;
3602 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3603
3604 unsigned char *source;
3605 int inputPitch;
3606 if (packReverseRowOrder)
3607 {
3608 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3609 inputPitch = -static_cast<int>(mapping.RowPitch);
3610 }
3611 else
3612 {
3613 source = static_cast<unsigned char*>(mapping.pData);
3614 inputPitch = static_cast<int>(mapping.RowPitch);
3615 }
3616
3617 unsigned int fastPixelSize = getFastPixelCopySize(textureDesc.Format, format, type);
3618 if (fastPixelSize != 0)
3619 {
3620 unsigned char *dest = static_cast<unsigned char*>(pixels);
3621 for (int j = 0; j < area.height; j++)
3622 {
3623 memcpy(dest + j * outputPitch, source + j * inputPitch, area.width * fastPixelSize);
3624 }
3625 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003626 else if (textureDesc.Format == DXGI_FORMAT_B8G8R8A8_UNORM &&
3627 format == GL_RGBA &&
3628 type == GL_UNSIGNED_BYTE)
3629 {
3630 // Fast path for swapping red with blue
3631 unsigned char *dest = static_cast<unsigned char*>(pixels);
3632
3633 for (int j = 0; j < area.height; j++)
3634 {
3635 for (int i = 0; i < area.width; i++)
3636 {
3637 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
3638 *(unsigned int*)(dest + 4 * i + j * outputPitch) =
3639 (argb & 0xFF00FF00) | // Keep alpha and green
3640 (argb & 0x00FF0000) >> 16 | // Move red to blue
3641 (argb & 0x000000FF) << 16; // Move blue to red
3642 }
3643 }
3644 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003645 else
3646 {
3647 gl::Color pixelColor;
3648 for (int j = 0; j < area.height; j++)
3649 {
3650 for (int i = 0; i < area.width; i++)
3651 {
3652 readPixelColor(source, textureDesc.Format, i, j, inputPitch, &pixelColor);
3653 writePixelColor(pixelColor, format, type, i, j, outputPitch, pixels);
3654 }
3655 }
3656 }
3657
3658 mDeviceContext->Unmap(stagingTex, 0);
3659
3660 stagingTex->Release();
3661 stagingTex = NULL;
3662}
3663
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003664bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
3665 RenderTarget *drawRenderTarget, bool wholeBufferCopy)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003666{
3667 ASSERT(readRect.width == drawRect.width && readRect.height == drawRect.height);
3668
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003669 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3670 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003671 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003672 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003673 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003674 }
3675
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003676 ID3D11Resource *readTexture = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003677 unsigned int readSubresource = 0;
3678 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003679 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003680 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3681 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
3682 unresolvedResource->Release();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003683
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003684 if (unresolvedTexture)
3685 {
3686 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3687 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003688
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003689 unresolvedTexture->Release();
3690 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003691 }
3692 else
3693 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003694 readTexture = readRenderTarget11->getTexture();
3695 readSubresource = readRenderTarget11->getSubresourceIndex();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003696 }
3697
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003698 if (!readTexture)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003699 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003700 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003701 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003702 }
3703
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003704 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003705 if (!drawRenderTarget)
3706 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003707 readTexture->Release();
3708 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003709 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003710 }
3711
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003712 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003713 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003714
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003715 D3D11_BOX readBox;
3716 readBox.left = readRect.x;
3717 readBox.right = readRect.x + readRect.width;
3718 readBox.top = readRect.y;
3719 readBox.bottom = readRect.y + readRect.height;
3720 readBox.front = 0;
3721 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003722
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003723 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3724 // We also require complete framebuffer copies for depth-stencil blit.
3725 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
3726
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003727 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, drawRect.x, drawRect.y, 0,
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003728 readTexture, readSubresource, pSrcBox);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003729
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003730 readTexture->Release();
3731 drawTexture->Release();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003732
3733 return true;
3734}
3735
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003736ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3737{
3738 D3D11_TEXTURE2D_DESC textureDesc;
3739 source->GetDesc(&textureDesc);
3740
3741 if (textureDesc.SampleDesc.Count > 1)
3742 {
3743 D3D11_TEXTURE2D_DESC resolveDesc;
3744 resolveDesc.Width = textureDesc.Width;
3745 resolveDesc.Height = textureDesc.Height;
3746 resolveDesc.MipLevels = 1;
3747 resolveDesc.ArraySize = 1;
3748 resolveDesc.Format = textureDesc.Format;
3749 resolveDesc.SampleDesc.Count = 1;
3750 resolveDesc.SampleDesc.Quality = 0;
3751 resolveDesc.Usage = textureDesc.Usage;
3752 resolveDesc.BindFlags = textureDesc.BindFlags;
3753 resolveDesc.CPUAccessFlags = 0;
3754 resolveDesc.MiscFlags = 0;
3755
3756 ID3D11Texture2D *resolveTexture = NULL;
3757 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3758 if (FAILED(result))
3759 {
3760 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3761 return NULL;
3762 }
3763
3764 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3765 return resolveTexture;
3766 }
3767 else
3768 {
3769 source->AddRef();
3770 return source;
3771 }
3772}
3773
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003774}