blob: 6cc514516354f9609e3d71e6f4272f313e7d40b2 [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
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000127EGLint Renderer11::initialize()
128{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000129 if (!initializeCompiler())
130 {
131 return EGL_NOT_INITIALIZED;
132 }
133
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000134 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
135 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000136
137 if (mD3d11Module == NULL || mDxgiModule == NULL)
138 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000139 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000140 return EGL_NOT_INITIALIZED;
141 }
142
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000143 // create the D3D11 device
144 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000145 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000146
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000147 if (D3D11CreateDevice == NULL)
148 {
149 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
150 return EGL_NOT_INITIALIZED;
151 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000152
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000153 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000154 {
155 D3D_FEATURE_LEVEL_11_0,
156 D3D_FEATURE_LEVEL_10_1,
157 D3D_FEATURE_LEVEL_10_0,
158 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000159
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000160 HRESULT result = D3D11CreateDevice(NULL,
161 D3D_DRIVER_TYPE_HARDWARE,
162 NULL,
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000163 #if defined(_DEBUG)
164 D3D11_CREATE_DEVICE_DEBUG,
165 #else
166 0,
167 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000168 featureLevels,
169 ArraySize(featureLevels),
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000170 D3D11_SDK_VERSION,
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000171 &mDevice,
172 &mFeatureLevel,
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000173 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000174
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000175 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000176 {
177 ERR("Could not create D3D11 device - aborting!\n");
178 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
179 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000180
181 IDXGIDevice *dxgiDevice = NULL;
182 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
183
184 if (FAILED(result))
185 {
186 ERR("Could not query DXGI device - aborting!\n");
187 return EGL_NOT_INITIALIZED;
188 }
189
190 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
191
192 if (FAILED(result))
193 {
194 ERR("Could not retrieve DXGI adapter - aborting!\n");
195 return EGL_NOT_INITIALIZED;
196 }
197
198 dxgiDevice->Release();
199
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000200 mDxgiAdapter->GetDesc(&mAdapterDescription);
201 memset(mDescription, 0, sizeof(mDescription));
202 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
203
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000204 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
205
206 if (!mDxgiFactory || FAILED(result))
207 {
208 ERR("Could not create DXGI factory - aborting!\n");
209 return EGL_NOT_INITIALIZED;
210 }
211
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000212 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
213#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
214 ID3D11InfoQueue *infoQueue;
215 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
216
217 if (SUCCEEDED(result))
218 {
219 D3D11_MESSAGE_ID hideMessages[] =
220 {
221 D3D11_MESSAGE_ID_DEVICE_OMSETRENDERTARGETS_HAZARD,
222 D3D11_MESSAGE_ID_DEVICE_PSSETSHADERRESOURCES_HAZARD
223 };
224
225 D3D11_INFO_QUEUE_FILTER filter = {0};
226 filter.DenyList.NumIDs = ArraySize(hideMessages);
227 filter.DenyList.pIDList = hideMessages;
228
229 infoQueue->AddStorageFilterEntries(&filter);
230
231 infoQueue->Release();
232 }
233#endif
234
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000235 unsigned int maxSupportedSamples = 0;
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000236 unsigned int rtFormatCount = ArraySize(RenderTargetFormats);
237 unsigned int dsFormatCount = ArraySize(DepthStencilFormats);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000238 for (unsigned int i = 0; i < rtFormatCount + dsFormatCount; ++i)
239 {
240 DXGI_FORMAT format = (i < rtFormatCount) ? RenderTargetFormats[i] : DepthStencilFormats[i - rtFormatCount];
241 if (format != DXGI_FORMAT_UNKNOWN)
242 {
243 UINT formatSupport;
244 result = mDevice->CheckFormatSupport(format, &formatSupport);
245 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
246 {
247 MultisampleSupportInfo supportInfo;
248
249 for (unsigned int j = 1; j <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; j++)
250 {
251 result = mDevice->CheckMultisampleQualityLevels(format, j, &supportInfo.qualityLevels[j - 1]);
252 if (SUCCEEDED(result) && supportInfo.qualityLevels[j - 1] > 0)
253 {
254 maxSupportedSamples = std::max(j, maxSupportedSamples);
255 }
256 else
257 {
258 supportInfo.qualityLevels[j - 1] = 0;
259 }
260 }
261
262 mMultisampleSupportMap.insert(std::make_pair(format, supportInfo));
263 }
264 }
265 }
266 mMaxSupportedSamples = maxSupportedSamples;
267
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000268 initializeDevice();
269
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000270 // BGRA texture support is optional in feature levels 10 and 10_1
271 UINT formatSupport;
272 result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport);
273 if (FAILED(result))
274 {
275 ERR("Error checking BGRA format support: 0x%08X", result);
276 }
277 else
278 {
279 const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET);
280 mBGRATextureSupport = (formatSupport & flags) == flags;
281 }
282
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000283 // Check floating point texture support
284 static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE;
285 static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET;
286 static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE;
287
288 DXGI_FORMAT float16Formats[] =
289 {
290 DXGI_FORMAT_R16_FLOAT,
291 DXGI_FORMAT_R16G16_FLOAT,
292 DXGI_FORMAT_R16G16B16A16_FLOAT,
293 };
294
295 DXGI_FORMAT float32Formats[] =
296 {
297 DXGI_FORMAT_R32_FLOAT,
298 DXGI_FORMAT_R32G32_FLOAT,
299 DXGI_FORMAT_R32G32B32_FLOAT,
300 DXGI_FORMAT_R32G32B32A32_FLOAT,
301 };
302
303 mFloat16TextureSupport = true;
304 mFloat16FilterSupport = true;
305 mFloat16RenderSupport = true;
306 for (unsigned int i = 0; i < ArraySize(float16Formats); i++)
307 {
308 if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport)))
309 {
310 mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
311 mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
312 mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
313 }
314 else
315 {
316 mFloat16TextureSupport = false;
317 mFloat16RenderSupport = false;
318 mFloat16FilterSupport = false;
319 }
320 }
321
322 mFloat32TextureSupport = true;
323 mFloat32FilterSupport = true;
324 mFloat32RenderSupport = true;
325 for (unsigned int i = 0; i < ArraySize(float32Formats); i++)
326 {
327 if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport)))
328 {
329 mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
330 mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
331 mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
332 }
333 else
334 {
335 mFloat32TextureSupport = false;
336 mFloat32FilterSupport = false;
337 mFloat32RenderSupport = false;
338 }
339 }
340
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +0000341 // Check compressed texture support
342 const unsigned int requiredCompressedTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D;
343
344 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &formatSupport)))
345 {
346 mDXT1TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
347 }
348 else
349 {
350 mDXT1TextureSupport = false;
351 }
352
353 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport)))
354 {
355 mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
356 }
357 else
358 {
359 mDXT3TextureSupport = false;
360 }
361
362 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC5_UNORM, &formatSupport)))
363 {
364 mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
365 }
366 else
367 {
368 mDXT5TextureSupport = false;
369 }
370
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +0000371 // Check depth texture support
372 DXGI_FORMAT depthTextureFormats[] =
373 {
374 DXGI_FORMAT_D16_UNORM,
375 DXGI_FORMAT_D24_UNORM_S8_UINT,
376 };
377
378 static const unsigned int requiredDepthTextureFlags = D3D11_FORMAT_SUPPORT_DEPTH_STENCIL |
379 D3D11_FORMAT_SUPPORT_TEXTURE2D;
380
381 mDepthTextureSupport = true;
382 for (unsigned int i = 0; i < ArraySize(depthTextureFormats); i++)
383 {
384 if (SUCCEEDED(mDevice->CheckFormatSupport(depthTextureFormats[i], &formatSupport)))
385 {
386 mDepthTextureSupport = mDepthTextureSupport && ((formatSupport & requiredDepthTextureFlags) == requiredDepthTextureFlags);
387 }
388 else
389 {
390 mDepthTextureSupport = false;
391 }
392 }
393
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000394 return EGL_SUCCESS;
395}
396
397// do any one-time device initialization
398// NOTE: this is also needed after a device lost/reset
399// to reset the scene status and ensure the default states are reset.
400void Renderer11::initializeDevice()
401{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000402 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000403 mInputLayoutCache.initialize(mDevice, mDeviceContext);
404
405 ASSERT(!mVertexDataManager && !mIndexDataManager);
406 mVertexDataManager = new VertexDataManager(this);
407 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000408
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000409 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000410}
411
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000412int Renderer11::generateConfigs(ConfigDesc **configDescList)
413{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000414 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
415 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000416 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
417 int numConfigs = 0;
418
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000419 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000420 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000421 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000422 {
423 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
424
425 UINT formatSupport = 0;
426 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000427
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000428 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
429 {
430 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
431
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000432 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000433
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000434 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
435 {
436 UINT formatSupport = 0;
437 result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
438 depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
439 }
440
441 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000442 {
443 ConfigDesc newConfig;
444 newConfig.renderTargetFormat = d3d11_gl::ConvertBackBufferFormat(renderTargetFormat);
445 newConfig.depthStencilFormat = d3d11_gl::ConvertDepthStencilFormat(depthStencilFormat);
446 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
447 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000448 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000449
450 (*configDescList)[numConfigs++] = newConfig;
451 }
452 }
453 }
454 }
455
456 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000457}
458
459void Renderer11::deleteConfigs(ConfigDesc *configDescList)
460{
461 delete [] (configDescList);
462}
463
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000464void Renderer11::sync(bool block)
465{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000466 if (block)
467 {
468 HRESULT result;
469
470 if (!mSyncQuery)
471 {
472 D3D11_QUERY_DESC queryDesc;
473 queryDesc.Query = D3D11_QUERY_EVENT;
474 queryDesc.MiscFlags = 0;
475
476 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
477 ASSERT(SUCCEEDED(result));
478 }
479
480 mDeviceContext->End(mSyncQuery);
481 mDeviceContext->Flush();
482
483 do
484 {
485 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
486
487 // Keep polling, but allow other threads to do something useful first
488 Sleep(0);
489
490 if (testDeviceLost(true))
491 {
492 return;
493 }
494 }
495 while (result == S_FALSE);
496 }
497 else
498 {
499 mDeviceContext->Flush();
500 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000501}
502
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000503SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
504{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000505 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000506}
507
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000508void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
509{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000510 if (type == gl::SAMPLER_PIXEL)
511 {
512 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
513 {
514 ERR("Pixel shader sampler index %i is not valid.", index);
515 return;
516 }
517
518 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
519 {
520 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
521
522 if (!dxSamplerState)
523 {
524 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
525 "sampler state for pixel shaders at slot %i.", index);
526 }
527
528 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
529
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000530 mCurPixelSamplerStates[index] = samplerState;
531 }
532
533 mForceSetPixelSamplerStates[index] = false;
534 }
535 else if (type == gl::SAMPLER_VERTEX)
536 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000537 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000538 {
539 ERR("Vertex shader sampler index %i is not valid.", index);
540 return;
541 }
542
543 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
544 {
545 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
546
547 if (!dxSamplerState)
548 {
549 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
550 "sampler state for vertex shaders at slot %i.", index);
551 }
552
553 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
554
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000555 mCurVertexSamplerStates[index] = samplerState;
556 }
557
558 mForceSetVertexSamplerStates[index] = false;
559 }
560 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000561}
562
563void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
564{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000565 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000566 unsigned int serial = 0;
567 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000568
569 if (texture)
570 {
571 TextureStorageInterface *texStorage = texture->getNativeTexture();
572 if (texStorage)
573 {
574 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
575 textureSRV = storage11->getSRV();
576 }
577
578 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
579 // missing the shader resource view
580 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000581
582 serial = texture->getTextureSerial();
583 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000584 }
585
586 if (type == gl::SAMPLER_PIXEL)
587 {
588 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
589 {
590 ERR("Pixel shader sampler index %i is not valid.", index);
591 return;
592 }
593
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000594 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
595 {
596 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
597 }
598
599 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000600 }
601 else if (type == gl::SAMPLER_VERTEX)
602 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000603 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000604 {
605 ERR("Vertex shader sampler index %i is not valid.", index);
606 return;
607 }
608
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000609 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
610 {
611 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
612 }
613
614 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000615 }
616 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000617}
618
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000619void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000620{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000621 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000622 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000623 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
624 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000625 if (!dxRasterState)
626 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000627 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000628 "rasterizer state.");
629 }
630
631 mDeviceContext->RSSetState(dxRasterState);
632
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000633 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000634 }
635
636 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000637}
638
639void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor,
640 unsigned int sampleMask)
641{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000642 if (mForceSetBlendState ||
643 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
644 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0 ||
645 sampleMask != mCurSampleMask)
646 {
647 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
648 if (!dxBlendState)
649 {
650 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
651 "blend state.");
652 }
653
654 const float blendColors[] = { blendColor.red, blendColor.green, blendColor.blue, blendColor.alpha };
655 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
656
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000657 mCurBlendState = blendState;
658 mCurBlendColor = blendColor;
659 mCurSampleMask = sampleMask;
660 }
661
662 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000663}
664
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000665void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000666 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000667{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000668 if (mForceSetDepthStencilState ||
669 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
670 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
671 {
672 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
673 stencilRef != stencilBackRef ||
674 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
675 {
676 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
677 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000678 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000679 }
680
681 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
682 if (!dxDepthStencilState)
683 {
684 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
685 "setting the default depth stencil state.");
686 }
687
688 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
689
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000690 mCurDepthStencilState = depthStencilState;
691 mCurStencilRef = stencilRef;
692 mCurStencilBackRef = stencilBackRef;
693 }
694
695 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000696}
697
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000698void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000699{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000700 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
701 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000702 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000703 if (enabled)
704 {
705 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000706 rect.left = std::max(0, scissor.x);
707 rect.top = std::max(0, scissor.y);
708 rect.right = scissor.x + std::max(0, scissor.width);
709 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000710
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000711 mDeviceContext->RSSetScissorRects(1, &rect);
712 }
713
714 if (enabled != mScissorEnabled)
715 {
716 mForceSetRasterState = true;
717 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000718
719 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000720 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000721 }
722
723 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000724}
725
daniel@transgaming.com12985182012-12-20 20:56:31 +0000726bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000727 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000728{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000729 gl::Rectangle actualViewport = viewport;
730 float actualZNear = gl::clamp01(zNear);
731 float actualZFar = gl::clamp01(zFar);
732 if (ignoreViewport)
733 {
734 actualViewport.x = 0;
735 actualViewport.y = 0;
736 actualViewport.width = mRenderTargetDesc.width;
737 actualViewport.height = mRenderTargetDesc.height;
738 actualZNear = 0.0f;
739 actualZFar = 1.0f;
740 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000741
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000742 // Get D3D viewport bounds, which depends on the feature level
743 const Range& viewportBounds = getViewportBounds();
744
745 // 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 +0000746 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000747 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
748 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
749 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
750 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
751 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
752 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000753 dxViewport.MinDepth = actualZNear;
754 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000755
756 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
757 {
758 return false; // Nothing to render
759 }
760
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000761 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
762 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000763
daniel@transgaming.com53670042012-11-28 20:55:51 +0000764 if (viewportChanged)
765 {
766 mDeviceContext->RSSetViewports(1, &dxViewport);
767
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000768 mCurViewport = actualViewport;
769 mCurNear = actualZNear;
770 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000771
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000772 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
773 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
774 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
775 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000776
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000777 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
778 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000779
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000780 mVertexConstants.depthRange[0] = actualZNear;
781 mVertexConstants.depthRange[1] = actualZFar;
782 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000783
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000784 mPixelConstants.depthRange[0] = actualZNear;
785 mPixelConstants.depthRange[1] = actualZFar;
786 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000787 }
788
789 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000790 return true;
791}
792
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000793bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
794{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000795 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000796
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000797 switch (mode)
798 {
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000799 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break;
800 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break;
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000801 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000802 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
803 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
804 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000805 // emulate fans via rewriting index buffer
806 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000807 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000808 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000809 }
810
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000811 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000812
813 return count > 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000814}
815
816bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000817{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000818 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000819 // Also extract the render target dimensions and view
820 unsigned int renderTargetWidth = 0;
821 unsigned int renderTargetHeight = 0;
822 GLenum renderTargetFormat = 0;
823 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
824 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
825 bool missingColorRenderTarget = true;
826
827 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000828 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000829 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
830
831 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000832 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000833 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
834 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
835
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000836 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000837
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000838 if (!colorbuffer)
839 {
840 ERR("render target pointer unexpectedly null.");
841 return false;
842 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000843
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000844 // check for zero-sized default framebuffer, which is a special case.
845 // in this case we do not wish to modify any state and just silently return false.
846 // this will not report any gl error but will cause the calling method to return.
847 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
848 {
849 return false;
850 }
851
852 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
853
854 // Extract the render target dimensions and view
855 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
856 if (!renderTarget)
857 {
858 ERR("render target pointer unexpectedly null.");
859 return false;
860 }
861
862 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
863 if (!framebufferRTVs[colorAttachment])
864 {
865 ERR("render target view pointer unexpectedly null.");
866 return false;
867 }
868
869 if (missingColorRenderTarget)
870 {
871 renderTargetWidth = colorbuffer->getWidth();
872 renderTargetHeight = colorbuffer->getHeight();
873 renderTargetFormat = colorbuffer->getActualFormat();
874 missingColorRenderTarget = false;
875 }
876 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000877 }
878
879 // Get the depth stencil render buffer and serials
880 gl::Renderbuffer *depthStencil = NULL;
881 unsigned int depthbufferSerial = 0;
882 unsigned int stencilbufferSerial = 0;
883 if (framebuffer->getDepthbufferType() != GL_NONE)
884 {
885 depthStencil = framebuffer->getDepthbuffer();
886 if (!depthStencil)
887 {
888 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000889 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000890 return false;
891 }
892
893 depthbufferSerial = depthStencil->getSerial();
894 }
895 else if (framebuffer->getStencilbufferType() != GL_NONE)
896 {
897 depthStencil = framebuffer->getStencilbuffer();
898 if (!depthStencil)
899 {
900 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000901 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000902 return false;
903 }
904
905 stencilbufferSerial = depthStencil->getSerial();
906 }
907
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000908 // Extract the depth stencil sizes and view
909 unsigned int depthSize = 0;
910 unsigned int stencilSize = 0;
911 ID3D11DepthStencilView* framebufferDSV = NULL;
912 if (depthStencil)
913 {
914 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
915 if (!depthStencilRenderTarget)
916 {
917 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000918 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000919 return false;
920 }
921
922 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
923 if (!framebufferDSV)
924 {
925 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000926 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000927 return false;
928 }
929
930 // If there is no render buffer, the width, height and format values come from
931 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000932 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000933 {
934 renderTargetWidth = depthStencil->getWidth();
935 renderTargetHeight = depthStencil->getHeight();
936 renderTargetFormat = depthStencil->getActualFormat();
937 }
938
939 depthSize = depthStencil->getDepthSize();
940 stencilSize = depthStencil->getStencilSize();
941 }
942
943 // Apply the render target and depth stencil
944 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000945 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000946 depthbufferSerial != mAppliedDepthbufferSerial ||
947 stencilbufferSerial != mAppliedStencilbufferSerial)
948 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000949 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000950
951 mRenderTargetDesc.width = renderTargetWidth;
952 mRenderTargetDesc.height = renderTargetHeight;
953 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000954 mForceSetViewport = true;
955 mForceSetScissor = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000956
957 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
958 {
959 mCurDepthSize = depthSize;
960 mForceSetRasterState = true;
961 }
962
963 mCurStencilSize = stencilSize;
964
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000965 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
966 {
967 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
968 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000969 mAppliedDepthbufferSerial = depthbufferSerial;
970 mAppliedStencilbufferSerial = stencilbufferSerial;
971 mRenderTargetDescInitialized = true;
972 mDepthStencilInitialized = true;
973 }
974
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000975 SafeRelease(framebufferRTVs);
976 SafeRelease(framebufferDSV);
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000977
978 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000979}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000980
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000981GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000982{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000983 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
984 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
985 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000986 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000987 return err;
988 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000989
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000990 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000991}
992
daniel@transgaming.com31240482012-11-28 21:06:41 +0000993GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000994{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000995 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000996
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000997 if (err == GL_NO_ERROR)
998 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +0000999 if (indexInfo->storage)
1000 {
1001 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1002 {
1003 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1004 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1005
1006 mDeviceContext->IASetIndexBuffer(storage->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
1007
1008 mAppliedIBSerial = 0;
1009 mAppliedStorageIBSerial = storage->getSerial();
1010 mAppliedIBOffset = indexInfo->startOffset;
1011 }
1012 }
1013 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001014 {
1015 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1016
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001017 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001018
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001019 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001020 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001021 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001022 }
1023 }
1024
1025 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001026}
1027
1028void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1029{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001030 if (mode == GL_LINE_LOOP)
1031 {
1032 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1033 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001034 else if (mode == GL_TRIANGLE_FAN)
1035 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001036 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001037 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001038 else if (instances > 0)
1039 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001040 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001041 }
1042 else
1043 {
1044 mDeviceContext->Draw(count, 0);
1045 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001046}
1047
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001048void 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 +00001049{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001050 if (mode == GL_LINE_LOOP)
1051 {
1052 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1053 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001054 else if (mode == GL_TRIANGLE_FAN)
1055 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001056 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1057 }
1058 else if (instances > 0)
1059 {
1060 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001061 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001062 else
1063 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001064 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001065 }
1066}
1067
1068void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1069{
1070 // Get the raw indices for an indexed draw
1071 if (type != GL_NONE && elementArrayBuffer)
1072 {
1073 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001074 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001075 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001076 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001077 }
1078
1079 if (!mLineLoopIB)
1080 {
1081 mLineLoopIB = new StreamingIndexBufferInterface(this);
1082 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1083 {
1084 delete mLineLoopIB;
1085 mLineLoopIB = NULL;
1086
1087 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001088 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001089 }
1090 }
1091
1092 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
1093 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1094 {
1095 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001096 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001097 }
1098
1099 void* mappedMemory = NULL;
1100 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1101 if (offset == -1 || mappedMemory == NULL)
1102 {
1103 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001104 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001105 }
1106
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001107 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001108 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001109
1110 switch (type)
1111 {
1112 case GL_NONE: // Non-indexed draw
1113 for (int i = 0; i < count; i++)
1114 {
1115 data[i] = i;
1116 }
1117 data[count] = 0;
1118 break;
1119 case GL_UNSIGNED_BYTE:
1120 for (int i = 0; i < count; i++)
1121 {
1122 data[i] = static_cast<const GLubyte*>(indices)[i];
1123 }
1124 data[count] = static_cast<const GLubyte*>(indices)[0];
1125 break;
1126 case GL_UNSIGNED_SHORT:
1127 for (int i = 0; i < count; i++)
1128 {
1129 data[i] = static_cast<const GLushort*>(indices)[i];
1130 }
1131 data[count] = static_cast<const GLushort*>(indices)[0];
1132 break;
1133 case GL_UNSIGNED_INT:
1134 for (int i = 0; i < count; i++)
1135 {
1136 data[i] = static_cast<const GLuint*>(indices)[i];
1137 }
1138 data[count] = static_cast<const GLuint*>(indices)[0];
1139 break;
1140 default: UNREACHABLE();
1141 }
1142
1143 if (!mLineLoopIB->unmapBuffer())
1144 {
1145 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001146 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001147 }
1148
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001149 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001150 {
1151 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1152
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001153 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001154 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001155 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001156 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001157 }
1158
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001159 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001160}
1161
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001162void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001163{
1164 // Get the raw indices for an indexed draw
1165 if (type != GL_NONE && elementArrayBuffer)
1166 {
1167 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001168 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001169 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001170 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001171 }
1172
1173 if (!mTriangleFanIB)
1174 {
1175 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1176 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1177 {
1178 delete mTriangleFanIB;
1179 mTriangleFanIB = NULL;
1180
1181 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001182 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001183 }
1184 }
1185
1186 const int numTris = count - 2;
1187 const int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
1188 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1189 {
1190 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001191 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001192 }
1193
1194 void* mappedMemory = NULL;
1195 int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory);
1196 if (offset == -1 || mappedMemory == NULL)
1197 {
1198 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001199 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001200 }
1201
1202 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1203 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
1204
1205 switch (type)
1206 {
1207 case GL_NONE: // Non-indexed draw
1208 for (int i = 0; i < numTris; i++)
1209 {
1210 data[i*3 + 0] = 0;
1211 data[i*3 + 1] = i + 1;
1212 data[i*3 + 2] = i + 2;
1213 }
1214 break;
1215 case GL_UNSIGNED_BYTE:
1216 for (int i = 0; i < numTris; i++)
1217 {
1218 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1219 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1220 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1221 }
1222 break;
1223 case GL_UNSIGNED_SHORT:
1224 for (int i = 0; i < numTris; i++)
1225 {
1226 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1227 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1228 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1229 }
1230 break;
1231 case GL_UNSIGNED_INT:
1232 for (int i = 0; i < numTris; i++)
1233 {
1234 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1235 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1236 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1237 }
1238 break;
1239 default: UNREACHABLE();
1240 }
1241
1242 if (!mTriangleFanIB->unmapBuffer())
1243 {
1244 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001245 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001246 }
1247
1248 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1249 {
1250 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1251
1252 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1253 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001254 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001255 mAppliedIBOffset = indexBufferOffset;
1256 }
1257
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001258 if (instances > 0)
1259 {
1260 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1261 }
1262 else
1263 {
1264 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1265 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001266}
1267
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001268void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1269{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001270 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001271 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1272
1273 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001274 {
1275 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1276 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001277
daniel@transgaming.come4991412012-12-20 20:55:34 +00001278 ID3D11VertexShader *vertexShader = NULL;
1279 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001280
daniel@transgaming.come4991412012-12-20 20:55:34 +00001281 ID3D11PixelShader *pixelShader = NULL;
1282 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001283
daniel@transgaming.come4991412012-12-20 20:55:34 +00001284 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1285 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001286
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001287 programBinary->dirtyAllUniforms();
1288
1289 mAppliedProgramBinarySerial = programBinarySerial;
1290 }
1291
1292 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001293 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001294
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001295 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001296 {
1297 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001298 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001299 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1300 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001301 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1302 }
1303 else
1304 {
1305 mDeviceContext->GSSetShader(NULL, NULL, 0);
1306 }
1307
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001308 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001309 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001310}
1311
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001312void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001313{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001314 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1315 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001316
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001317 unsigned int totalRegisterCountVS = 0;
1318 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001319
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001320 bool vertexUniformsDirty = false;
1321 bool pixelUniformsDirty = false;
1322
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001323 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1324 {
1325 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001326
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001327 if (uniform->vsRegisterIndex >= 0)
1328 {
1329 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001330 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001331 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001332
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001333 if (uniform->psRegisterIndex >= 0)
1334 {
1335 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001336 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001337 }
1338 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001339
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001340 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1341 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1342
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001343 float (*mapVS)[4] = NULL;
1344 float (*mapPS)[4] = NULL;
1345
1346 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1347 {
1348 D3D11_MAPPED_SUBRESOURCE map = {0};
1349 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1350 ASSERT(SUCCEEDED(result));
1351 mapVS = (float(*)[4])map.pData;
1352 }
1353
1354 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1355 {
1356 D3D11_MAPPED_SUBRESOURCE map = {0};
1357 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1358 ASSERT(SUCCEEDED(result));
1359 mapPS = (float(*)[4])map.pData;
1360 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001361
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001362 for (gl::UniformArray::iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001363 {
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001364 gl::Uniform *uniform = *uniform_iterator;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001365
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001366 if (uniform->type != GL_SAMPLER_2D && uniform->type != GL_SAMPLER_CUBE)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001367 {
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001368 if (uniform->vsRegisterIndex >= 0 && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001369 {
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001370 memcpy(mapVS + uniform->vsRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4]));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001371 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001372
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001373 if (uniform->psRegisterIndex >= 0 && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001374 {
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001375 memcpy(mapPS + uniform->psRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4]));
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001376 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001377 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001378
1379 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001380 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001381
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001382 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001383 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001384 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001385 }
1386
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001387 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001388 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001389 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001390 }
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001391
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001392 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1393 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001394
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001395 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001396 if (!mDriverConstantBufferVS)
1397 {
1398 D3D11_BUFFER_DESC constantBufferDescription = {0};
1399 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1400 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1401 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1402 constantBufferDescription.CPUAccessFlags = 0;
1403 constantBufferDescription.MiscFlags = 0;
1404 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001405
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001406 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001407 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001408
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001409 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1410 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001411
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001412 if (!mDriverConstantBufferPS)
1413 {
1414 D3D11_BUFFER_DESC constantBufferDescription = {0};
1415 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1416 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1417 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1418 constantBufferDescription.CPUAccessFlags = 0;
1419 constantBufferDescription.MiscFlags = 0;
1420 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001421
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001422 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001423 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001424
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001425 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1426 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001427
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001428 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1429 {
1430 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1431 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1432 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001433
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001434 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1435 {
1436 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1437 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1438 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001439
1440 // needed for the point sprite geometry shader
1441 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001442}
1443
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001444void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001445{
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001446 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1447 bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1448 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1449 clearParams.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001450
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001451 unsigned int stencilUnmasked = 0x0;
1452 if (frameBuffer->hasStencil())
1453 {
1454 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1455 stencilUnmasked = (0x1 << stencilSize) - 1;
1456 }
1457 bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1458 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001459
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001460 bool needScissoredClear = mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1461 mCurScissor.x + mCurScissor.width < mRenderTargetDesc.width ||
1462 mCurScissor.y + mCurScissor.height < mRenderTargetDesc.height);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001463
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001464 if (needMaskedColorClear || needMaskedStencilClear || needScissoredClear)
1465 {
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001466 maskedClear(clearParams);
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001467 }
1468 else
1469 {
1470 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1471 {
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001472 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001473 {
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001474 if (frameBuffer->isEnabledColorAttachment(colorAttachment))
1475 {
1476 gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer(colorAttachment);
1477 if (renderbufferObject)
1478 {
1479 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
1480 if (!renderTarget)
1481 {
1482 ERR("render target pointer unexpectedly null.");
1483 return;
1484 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001485
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001486 ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView();
1487 if (!framebufferRTV)
1488 {
1489 ERR("render target view pointer unexpectedly null.");
1490 return;
1491 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001492
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001493 const float clearValues[4] = { clearParams.colorClearValue.red,
1494 clearParams.colorClearValue.green,
1495 clearParams.colorClearValue.blue,
1496 clearParams.colorClearValue.alpha };
1497 mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001498
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001499 framebufferRTV->Release();
1500 }
1501 }
1502 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001503 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001504 if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001505 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001506 gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer();
1507 if (renderbufferObject)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001508 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001509 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil());
1510 if (!renderTarget)
1511 {
1512 ERR("render target pointer unexpectedly null.");
1513 return;
1514 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001515
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001516 ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView();
1517 if (!framebufferDSV)
1518 {
1519 ERR("depth stencil view pointer unexpectedly null.");
1520 return;
1521 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001522
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001523 UINT clearFlags = 0;
1524 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1525 {
1526 clearFlags |= D3D11_CLEAR_DEPTH;
1527 }
1528 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1529 {
1530 clearFlags |= D3D11_CLEAR_STENCIL;
1531 }
1532
shannon.woods@transgaming.combe211b32013-02-28 23:17:16 +00001533 float depthClear = gl::clamp01(clearParams.depthClearValue);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001534 UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF;
1535
1536 mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001537
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001538 framebufferDSV->Release();
1539 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001540 }
1541 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001542}
1543
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001544void Renderer11::maskedClear(const gl::ClearParameters &clearParams)
1545{
1546 HRESULT result;
1547
1548 if (!mClearResourcesInitialized)
1549 {
1550 ASSERT(!mClearVB && !mClearVS && !mClearPS && !mClearScissorRS && !mClearNoScissorRS);
1551
1552 D3D11_BUFFER_DESC vbDesc;
1553 vbDesc.ByteWidth = sizeof(d3d11::PositionDepthColorVertex) * 4;
1554 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
1555 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
1556 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1557 vbDesc.MiscFlags = 0;
1558 vbDesc.StructureByteStride = 0;
1559
1560 result = mDevice->CreateBuffer(&vbDesc, NULL, &mClearVB);
1561 ASSERT(SUCCEEDED(result));
1562 d3d11::SetDebugName(mClearVB, "Renderer11 masked clear vertex buffer");
1563
1564 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
1565 {
1566 { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1567 { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1568 };
1569
1570 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Clear, sizeof(g_VS_Clear), &mClearIL);
1571 ASSERT(SUCCEEDED(result));
1572 d3d11::SetDebugName(mClearIL, "Renderer11 masked clear input layout");
1573
1574 result = mDevice->CreateVertexShader(g_VS_Clear, sizeof(g_VS_Clear), NULL, &mClearVS);
1575 ASSERT(SUCCEEDED(result));
1576 d3d11::SetDebugName(mClearVS, "Renderer11 masked clear vertex shader");
1577
1578 result = mDevice->CreatePixelShader(g_PS_Clear, sizeof(g_PS_Clear), NULL, &mClearPS);
1579 ASSERT(SUCCEEDED(result));
1580 d3d11::SetDebugName(mClearPS, "Renderer11 masked clear pixel shader");
1581
1582 D3D11_RASTERIZER_DESC rsScissorDesc;
1583 rsScissorDesc.FillMode = D3D11_FILL_SOLID;
1584 rsScissorDesc.CullMode = D3D11_CULL_NONE;
1585 rsScissorDesc.FrontCounterClockwise = FALSE;
1586 rsScissorDesc.DepthBias = 0;
1587 rsScissorDesc.DepthBiasClamp = 0.0f;
1588 rsScissorDesc.SlopeScaledDepthBias = 0.0f;
1589 rsScissorDesc.DepthClipEnable = FALSE;
1590 rsScissorDesc.ScissorEnable = TRUE;
1591 rsScissorDesc.MultisampleEnable = FALSE;
1592 rsScissorDesc.AntialiasedLineEnable = FALSE;
1593
1594 result = mDevice->CreateRasterizerState(&rsScissorDesc, &mClearScissorRS);
1595 ASSERT(SUCCEEDED(result));
1596 d3d11::SetDebugName(mClearScissorRS, "Renderer11 masked clear scissor rasterizer state");
1597
1598 D3D11_RASTERIZER_DESC rsNoScissorDesc;
1599 rsNoScissorDesc.FillMode = D3D11_FILL_SOLID;
1600 rsNoScissorDesc.CullMode = D3D11_CULL_NONE;
1601 rsNoScissorDesc.FrontCounterClockwise = FALSE;
1602 rsNoScissorDesc.DepthBias = 0;
1603 rsNoScissorDesc.DepthBiasClamp = 0.0f;
1604 rsNoScissorDesc.SlopeScaledDepthBias = 0.0f;
1605 rsNoScissorDesc.DepthClipEnable = FALSE;
1606 rsNoScissorDesc.ScissorEnable = FALSE;
1607 rsNoScissorDesc.MultisampleEnable = FALSE;
1608 rsNoScissorDesc.AntialiasedLineEnable = FALSE;
1609
1610 result = mDevice->CreateRasterizerState(&rsNoScissorDesc, &mClearNoScissorRS);
1611 ASSERT(SUCCEEDED(result));
1612 d3d11::SetDebugName(mClearNoScissorRS, "Renderer11 masked clear no scissor rasterizer state");
1613
1614 mClearResourcesInitialized = true;
1615 }
1616
1617 // Prepare the depth stencil state to write depth values if the depth should be cleared
1618 // and stencil values if the stencil should be cleared
1619 gl::DepthStencilState glDSState;
1620 glDSState.depthTest = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1621 glDSState.depthFunc = GL_ALWAYS;
1622 glDSState.depthMask = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1623 glDSState.stencilTest = (clearParams.mask & GL_STENCIL_BUFFER_BIT) != 0;
1624 glDSState.stencilFunc = GL_ALWAYS;
1625 glDSState.stencilMask = 0;
1626 glDSState.stencilFail = GL_REPLACE;
1627 glDSState.stencilPassDepthFail = GL_REPLACE;
1628 glDSState.stencilPassDepthPass = GL_REPLACE;
1629 glDSState.stencilWritemask = clearParams.stencilWriteMask;
1630 glDSState.stencilBackFunc = GL_ALWAYS;
1631 glDSState.stencilBackMask = 0;
1632 glDSState.stencilBackFail = GL_REPLACE;
1633 glDSState.stencilBackPassDepthFail = GL_REPLACE;
1634 glDSState.stencilBackPassDepthPass = GL_REPLACE;
1635 glDSState.stencilBackWritemask = clearParams.stencilWriteMask;
1636
1637 int stencilClear = clearParams.stencilClearValue & 0x000000FF;
1638
1639 ID3D11DepthStencilState *dsState = mStateCache.getDepthStencilState(glDSState);
1640
1641 // Prepare the blend state to use a write mask if the color buffer should be cleared
1642 gl::BlendState glBlendState;
1643 glBlendState.blend = false;
1644 glBlendState.sourceBlendRGB = GL_ONE;
1645 glBlendState.destBlendRGB = GL_ZERO;
1646 glBlendState.sourceBlendAlpha = GL_ONE;
1647 glBlendState.destBlendAlpha = GL_ZERO;
1648 glBlendState.blendEquationRGB = GL_FUNC_ADD;
1649 glBlendState.blendEquationAlpha = GL_FUNC_ADD;
1650 glBlendState.colorMaskRed = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskRed : false;
1651 glBlendState.colorMaskGreen = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskGreen : false;
1652 glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false;
shannon.woods@transgaming.com50ea9932013-02-28 23:13:40 +00001653 glBlendState.colorMaskAlpha = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskAlpha : false;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001654 glBlendState.sampleAlphaToCoverage = false;
1655 glBlendState.dither = false;
1656
1657 static const float blendFactors[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
1658 static const UINT sampleMask = 0xFFFFFFFF;
1659
1660 ID3D11BlendState *blendState = mStateCache.getBlendState(glBlendState);
1661
1662 // Set the vertices
1663 D3D11_MAPPED_SUBRESOURCE mappedResource;
1664 result = mDeviceContext->Map(mClearVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
1665 if (FAILED(result))
1666 {
1667 ERR("Failed to map masked clear vertex buffer, HRESULT: 0x%X.", result);
1668 return;
1669 }
1670
1671 d3d11::PositionDepthColorVertex *vertices = reinterpret_cast<d3d11::PositionDepthColorVertex*>(mappedResource.pData);
1672
1673 float depthClear = gl::clamp01(clearParams.depthClearValue);
1674 d3d11::SetPositionDepthColorVertex(&vertices[0], -1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1675 d3d11::SetPositionDepthColorVertex(&vertices[1], -1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1676 d3d11::SetPositionDepthColorVertex(&vertices[2], 1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1677 d3d11::SetPositionDepthColorVertex(&vertices[3], 1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1678
1679 mDeviceContext->Unmap(mClearVB, 0);
1680
1681 // Apply state
1682 mDeviceContext->OMSetBlendState(blendState, blendFactors, sampleMask);
1683 mDeviceContext->OMSetDepthStencilState(dsState, stencilClear);
1684 mDeviceContext->RSSetState(mScissorEnabled ? mClearScissorRS : mClearNoScissorRS);
1685
1686 // Apply shaders
1687 mDeviceContext->IASetInputLayout(mClearIL);
1688 mDeviceContext->VSSetShader(mClearVS, NULL, 0);
1689 mDeviceContext->PSSetShader(mClearPS, NULL, 0);
shannon.woods@transgaming.com2c53e472013-02-28 23:13:56 +00001690 mDeviceContext->GSSetShader(NULL, NULL, 0);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001691
1692 // Apply vertex buffer
1693 static UINT stride = sizeof(d3d11::PositionDepthColorVertex);
1694 static UINT startIdx = 0;
1695 mDeviceContext->IASetVertexBuffers(0, 1, &mClearVB, &stride, &startIdx);
1696 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
1697
1698 // Draw the clear quad
1699 mDeviceContext->Draw(4, 0);
1700
1701 // Clean up
1702 markAllStateDirty();
1703}
1704
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001705void Renderer11::markAllStateDirty()
1706{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001707 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1708 {
1709 mAppliedRenderTargetSerials[rtIndex] = 0;
1710 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001711 mAppliedDepthbufferSerial = 0;
1712 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001713 mDepthStencilInitialized = false;
1714 mRenderTargetDescInitialized = false;
1715
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001716 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001717 {
1718 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001719 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001720 }
1721 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1722 {
1723 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001724 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001725 }
1726
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001727 mForceSetBlendState = true;
1728 mForceSetRasterState = true;
1729 mForceSetDepthStencilState = true;
1730 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001731 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001732
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001733 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001734 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001735 mAppliedIBOffset = 0;
1736
daniel@transgaming.come4991412012-12-20 20:55:34 +00001737 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001738 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1739 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001740}
1741
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001742void Renderer11::releaseDeviceResources()
1743{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001744 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001745 mInputLayoutCache.clear();
1746
1747 delete mVertexDataManager;
1748 mVertexDataManager = NULL;
1749
1750 delete mIndexDataManager;
1751 mIndexDataManager = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001752
1753 delete mLineLoopIB;
1754 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001755
1756 delete mTriangleFanIB;
1757 mTriangleFanIB = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001758
1759 if (mCopyVB)
1760 {
1761 mCopyVB->Release();
1762 mCopyVB = NULL;
1763 }
1764
1765 if (mCopySampler)
1766 {
1767 mCopySampler->Release();
1768 mCopySampler = NULL;
1769 }
1770
1771 if (mCopyIL)
1772 {
1773 mCopyIL->Release();
1774 mCopyIL = NULL;
1775 }
1776
1777 if (mCopyVS)
1778 {
1779 mCopyVS->Release();
1780 mCopyVS = NULL;
1781 }
1782
1783 if (mCopyRGBAPS)
1784 {
1785 mCopyRGBAPS->Release();
1786 mCopyRGBAPS = NULL;
1787 }
1788
1789 if (mCopyRGBPS)
1790 {
1791 mCopyRGBPS->Release();
1792 mCopyRGBPS = NULL;
1793 }
1794
1795 if (mCopyLumPS)
1796 {
1797 mCopyLumPS->Release();
1798 mCopyLumPS = NULL;
1799 }
1800
1801 if (mCopyLumAlphaPS)
1802 {
1803 mCopyLumAlphaPS->Release();
1804 mCopyLumAlphaPS = NULL;
1805 }
1806
1807 mCopyResourcesInitialized = false;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001808
1809 if (mClearVB)
1810 {
1811 mClearVB->Release();
1812 mClearVB = NULL;
1813 }
1814
1815 if (mClearIL)
1816 {
1817 mClearIL->Release();
1818 mClearIL = NULL;
1819 }
1820
1821 if (mClearVS)
1822 {
1823 mClearVS->Release();
1824 mClearVS = NULL;
1825 }
1826
1827 if (mClearPS)
1828 {
1829 mClearPS->Release();
1830 mClearPS = NULL;
1831 }
1832
1833 if (mClearScissorRS)
1834 {
1835 mClearScissorRS->Release();
1836 mClearScissorRS = NULL;
1837 }
1838
1839 if (mClearNoScissorRS)
1840 {
1841 mClearNoScissorRS->Release();
1842 mClearNoScissorRS = NULL;
1843 }
1844
1845 mClearResourcesInitialized = false;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001846
1847 if (mDriverConstantBufferVS)
1848 {
1849 mDriverConstantBufferVS->Release();
1850 mDriverConstantBufferVS = NULL;
1851 }
1852
1853 if (mDriverConstantBufferPS)
1854 {
1855 mDriverConstantBufferPS->Release();
1856 mDriverConstantBufferPS = NULL;
1857 }
daniel@transgaming.combdf787f2013-02-01 03:20:36 +00001858
1859 if (mSyncQuery)
1860 {
1861 mSyncQuery->Release();
1862 mSyncQuery = NULL;
1863 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001864}
1865
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001866void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001867{
1868 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001869 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001870}
1871
1872bool Renderer11::isDeviceLost()
1873{
1874 return mDeviceLost;
1875}
1876
1877// set notify to true to broadcast a message to all contexts of the device loss
1878bool Renderer11::testDeviceLost(bool notify)
1879{
1880 bool isLost = false;
1881
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001882 // GetRemovedReason is used to test if the device is removed
1883 HRESULT result = mDevice->GetDeviceRemovedReason();
1884 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001885
1886 if (isLost)
1887 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001888 // Log error if this is a new device lost event
1889 if (mDeviceLost == false)
1890 {
1891 ERR("The D3D11 device was removed: 0x%08X", result);
1892 }
1893
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001894 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001895 // we'll probably get this done again by notifyDeviceLost
1896 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001897 // Note that we don't want to clear the device loss status here
1898 // -- this needs to be done by resetDevice
1899 mDeviceLost = true;
1900 if (notify)
1901 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001902 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001903 }
1904 }
1905
1906 return isLost;
1907}
1908
1909bool Renderer11::testDeviceResettable()
1910{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001911 // determine if the device is resettable by creating a dummy device
1912 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001913
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001914 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001915 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001916 return false;
1917 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001918
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001919 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001920 {
1921 D3D_FEATURE_LEVEL_11_0,
1922 D3D_FEATURE_LEVEL_10_1,
1923 D3D_FEATURE_LEVEL_10_0,
1924 };
1925
1926 ID3D11Device* dummyDevice;
1927 D3D_FEATURE_LEVEL dummyFeatureLevel;
1928 ID3D11DeviceContext* dummyContext;
1929
1930 HRESULT result = D3D11CreateDevice(NULL,
1931 D3D_DRIVER_TYPE_HARDWARE,
1932 NULL,
1933 #if defined(_DEBUG)
1934 D3D11_CREATE_DEVICE_DEBUG,
1935 #else
1936 0,
1937 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001938 featureLevels,
1939 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001940 D3D11_SDK_VERSION,
1941 &dummyDevice,
1942 &dummyFeatureLevel,
1943 &dummyContext);
1944
1945 if (!mDevice || FAILED(result))
1946 {
1947 return false;
1948 }
1949
1950 dummyContext->Release();
1951 dummyDevice->Release();
1952
1953 return true;
1954}
1955
1956void Renderer11::release()
1957{
1958 releaseDeviceResources();
1959
1960 if (mDxgiFactory)
1961 {
1962 mDxgiFactory->Release();
1963 mDxgiFactory = NULL;
1964 }
1965
1966 if (mDxgiAdapter)
1967 {
1968 mDxgiAdapter->Release();
1969 mDxgiAdapter = NULL;
1970 }
1971
1972 if (mDeviceContext)
1973 {
1974 mDeviceContext->ClearState();
1975 mDeviceContext->Flush();
1976 mDeviceContext->Release();
1977 mDeviceContext = NULL;
1978 }
1979
1980 if (mDevice)
1981 {
1982 mDevice->Release();
1983 mDevice = NULL;
1984 }
1985
1986 if (mD3d11Module)
1987 {
1988 FreeLibrary(mD3d11Module);
1989 mD3d11Module = NULL;
1990 }
1991
1992 if (mDxgiModule)
1993 {
1994 FreeLibrary(mDxgiModule);
1995 mDxgiModule = NULL;
1996 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001997}
1998
1999bool Renderer11::resetDevice()
2000{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00002001 // recreate everything
2002 release();
2003 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002004
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00002005 if (result != EGL_SUCCESS)
2006 {
2007 ERR("Could not reinitialize D3D11 device: %08X", result);
2008 return false;
2009 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002010
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002011 mDeviceLost = false;
2012
2013 return true;
2014}
2015
2016DWORD Renderer11::getAdapterVendor() const
2017{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00002018 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002019}
2020
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002021std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002022{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002023 std::ostringstream rendererString;
2024
2025 rendererString << mDescription;
2026 rendererString << " Direct3D11";
2027
2028 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
2029 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
2030
2031 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002032}
2033
2034GUID Renderer11::getAdapterIdentifier() const
2035{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00002036 // Use the adapter LUID as our adapter ID
2037 // This number is local to a machine is only guaranteed to be unique between restarts
2038 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
2039 GUID adapterId = {0};
2040 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
2041 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002042}
2043
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002044bool Renderer11::getBGRATextureSupport() const
2045{
2046 return mBGRATextureSupport;
2047}
2048
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002049bool Renderer11::getDXT1TextureSupport()
2050{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002051 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002052}
2053
2054bool Renderer11::getDXT3TextureSupport()
2055{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002056 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002057}
2058
2059bool Renderer11::getDXT5TextureSupport()
2060{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002061 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002062}
2063
2064bool Renderer11::getDepthTextureSupport() const
2065{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00002066 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002067}
2068
2069bool Renderer11::getFloat32TextureSupport(bool *filtering, bool *renderable)
2070{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00002071 *renderable = mFloat32RenderSupport;
2072 *filtering = mFloat32FilterSupport;
2073 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002074}
2075
2076bool Renderer11::getFloat16TextureSupport(bool *filtering, bool *renderable)
2077{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00002078 *renderable = mFloat16RenderSupport;
2079 *filtering = mFloat16FilterSupport;
2080 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002081}
2082
2083bool Renderer11::getLuminanceTextureSupport()
2084{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002085 return false;
2086}
2087
2088bool Renderer11::getLuminanceAlphaTextureSupport()
2089{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002090 return false;
2091}
2092
2093bool Renderer11::getTextureFilterAnisotropySupport() const
2094{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002095 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002096}
2097
2098float Renderer11::getTextureMaxAnisotropy() const
2099{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002100 switch (mFeatureLevel)
2101 {
2102 case D3D_FEATURE_LEVEL_11_0:
2103 return D3D11_MAX_MAXANISOTROPY;
2104 case D3D_FEATURE_LEVEL_10_1:
2105 case D3D_FEATURE_LEVEL_10_0:
2106 return D3D10_MAX_MAXANISOTROPY;
2107 default: UNREACHABLE();
2108 return 0;
2109 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002110}
2111
2112bool Renderer11::getEventQuerySupport()
2113{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002114 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002115}
2116
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002117Range Renderer11::getViewportBounds() const
2118{
2119 switch (mFeatureLevel)
2120 {
2121 case D3D_FEATURE_LEVEL_11_0:
2122 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
2123 case D3D_FEATURE_LEVEL_10_1:
2124 case D3D_FEATURE_LEVEL_10_0:
2125 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
2126 default: UNREACHABLE();
2127 return Range(0, 0);
2128 }
2129}
2130
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002131unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002132{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002133 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2134 switch (mFeatureLevel)
2135 {
2136 case D3D_FEATURE_LEVEL_11_0:
2137 case D3D_FEATURE_LEVEL_10_1:
2138 case D3D_FEATURE_LEVEL_10_0:
2139 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
2140 default: UNREACHABLE();
2141 return 0;
2142 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002143}
2144
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002145unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
2146{
2147 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2148}
2149
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002150unsigned int Renderer11::getReservedVertexUniformVectors() const
2151{
2152 return 0; // Driver uniforms are stored in a separate constant buffer
2153}
2154
2155unsigned int Renderer11::getReservedFragmentUniformVectors() const
2156{
2157 return 0; // Driver uniforms are stored in a separate constant buffer
2158}
2159
2160unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002161{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002162 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2163 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2164 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002165}
2166
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002167unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002168{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002169 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2170 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2171 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002172}
2173
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002174unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002175{
2176 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
2177 switch (mFeatureLevel)
2178 {
2179 case D3D_FEATURE_LEVEL_11_0:
2180 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2181 case D3D_FEATURE_LEVEL_10_1:
2182 case D3D_FEATURE_LEVEL_10_0:
2183 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2184 default: UNREACHABLE();
2185 return 0;
2186 }
2187}
2188
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002189bool Renderer11::getNonPower2TextureSupport() const
2190{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002191 switch (mFeatureLevel)
2192 {
2193 case D3D_FEATURE_LEVEL_11_0:
2194 case D3D_FEATURE_LEVEL_10_1:
2195 case D3D_FEATURE_LEVEL_10_0:
2196 return true;
2197 default: UNREACHABLE();
2198 return false;
2199 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002200}
2201
2202bool Renderer11::getOcclusionQuerySupport() const
2203{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002204 switch (mFeatureLevel)
2205 {
2206 case D3D_FEATURE_LEVEL_11_0:
2207 case D3D_FEATURE_LEVEL_10_1:
2208 case D3D_FEATURE_LEVEL_10_0:
2209 return true;
2210 default: UNREACHABLE();
2211 return false;
2212 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002213}
2214
2215bool Renderer11::getInstancingSupport() const
2216{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002217 switch (mFeatureLevel)
2218 {
2219 case D3D_FEATURE_LEVEL_11_0:
2220 case D3D_FEATURE_LEVEL_10_1:
2221 case D3D_FEATURE_LEVEL_10_0:
2222 return true;
2223 default: UNREACHABLE();
2224 return false;
2225 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002226}
2227
2228bool Renderer11::getShareHandleSupport() const
2229{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002230 // We only currently support share handles with BGRA surfaces, because
2231 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002232 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002233 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002234}
2235
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002236bool Renderer11::getDerivativeInstructionSupport() const
2237{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002238 switch (mFeatureLevel)
2239 {
2240 case D3D_FEATURE_LEVEL_11_0:
2241 case D3D_FEATURE_LEVEL_10_1:
2242 case D3D_FEATURE_LEVEL_10_0:
2243 return true;
2244 default: UNREACHABLE();
2245 return false;
2246 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002247}
2248
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002249bool Renderer11::getPostSubBufferSupport() const
2250{
2251 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2252 return false;
2253}
2254
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002255int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002256{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002257 switch (mFeatureLevel)
2258 {
2259 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002260 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002261 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2262 default: UNREACHABLE(); return 0;
2263 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002264}
2265
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002266int Renderer11::getMinorShaderModel() const
2267{
2268 switch (mFeatureLevel)
2269 {
2270 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2271 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2272 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2273 default: UNREACHABLE(); return 0;
2274 }
2275}
2276
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002277float Renderer11::getMaxPointSize() const
2278{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002279 // choose a reasonable maximum. we enforce this in the shader.
2280 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2281 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002282}
2283
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002284int Renderer11::getMaxViewportDimension() const
2285{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002286 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2287 // In our case return the maximum texture size, which is the maximum render buffer size.
2288 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2289 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2290
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002291 switch (mFeatureLevel)
2292 {
2293 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002294 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002295 case D3D_FEATURE_LEVEL_10_1:
2296 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002297 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002298 default: UNREACHABLE();
2299 return 0;
2300 }
2301}
2302
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002303int Renderer11::getMaxTextureWidth() const
2304{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002305 switch (mFeatureLevel)
2306 {
2307 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2308 case D3D_FEATURE_LEVEL_10_1:
2309 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2310 default: UNREACHABLE(); return 0;
2311 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002312}
2313
2314int Renderer11::getMaxTextureHeight() const
2315{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002316 switch (mFeatureLevel)
2317 {
2318 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2319 case D3D_FEATURE_LEVEL_10_1:
2320 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2321 default: UNREACHABLE(); return 0;
2322 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002323}
2324
2325bool Renderer11::get32BitIndexSupport() const
2326{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002327 switch (mFeatureLevel)
2328 {
2329 case D3D_FEATURE_LEVEL_11_0:
2330 case D3D_FEATURE_LEVEL_10_1:
2331 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2332 default: UNREACHABLE(); return false;
2333 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002334}
2335
2336int Renderer11::getMinSwapInterval() const
2337{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002338 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002339}
2340
2341int Renderer11::getMaxSwapInterval() const
2342{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002343 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002344}
2345
2346int Renderer11::getMaxSupportedSamples() const
2347{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002348 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002349}
2350
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002351int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2352{
2353 if (requested == 0)
2354 {
2355 return 0;
2356 }
2357
2358 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2359 if (iter != mMultisampleSupportMap.end())
2360 {
2361 const MultisampleSupportInfo& info = iter->second;
2362 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2363 {
2364 if (info.qualityLevels[i] > 0)
2365 {
2366 return i + 1;
2367 }
2368 }
2369 }
2370
2371 return -1;
2372}
2373
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002374unsigned int Renderer11::getMaxRenderTargets() const
2375{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002376 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2377 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2378
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002379 switch (mFeatureLevel)
2380 {
2381 case D3D_FEATURE_LEVEL_11_0:
2382 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2383 case D3D_FEATURE_LEVEL_10_1:
2384 case D3D_FEATURE_LEVEL_10_0:
2385 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2386 default:
2387 UNREACHABLE();
2388 return 1;
2389 }
2390}
2391
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002392bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002393{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002394 if (source && dest)
2395 {
2396 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2397 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2398
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002399 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002400 return true;
2401 }
2402
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002403 return false;
2404}
2405
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002406bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002407{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002408 if (source && dest)
2409 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002410 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2411 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002412
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002413 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002414 return true;
2415 }
2416
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002417 return false;
2418}
2419
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002420bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002421 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002422{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002423 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002424 if (!colorbuffer)
2425 {
2426 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002427 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002428 }
2429
2430 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2431 if (!sourceRenderTarget)
2432 {
2433 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002434 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002435 }
2436
2437 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2438 if (!source)
2439 {
2440 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002441 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002442 }
2443
2444 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2445 if (!storage11)
2446 {
2447 source->Release();
2448 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002449 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002450 }
2451
2452 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2453 if (!destRenderTarget)
2454 {
2455 source->Release();
2456 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002457 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002458 }
2459
2460 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2461 if (!dest)
2462 {
2463 source->Release();
2464 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002465 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002466 }
2467
2468 gl::Rectangle destRect;
2469 destRect.x = xoffset;
2470 destRect.y = yoffset;
2471 destRect.width = sourceRect.width;
2472 destRect.height = sourceRect.height;
2473
2474 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
2475 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
2476
2477 source->Release();
2478 dest->Release();
2479
2480 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002481}
2482
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002483bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002484 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002485{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002486 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002487 if (!colorbuffer)
2488 {
2489 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002490 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002491 }
2492
2493 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2494 if (!sourceRenderTarget)
2495 {
2496 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002497 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002498 }
2499
2500 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2501 if (!source)
2502 {
2503 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002504 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002505 }
2506
2507 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2508 if (!storage11)
2509 {
2510 source->Release();
2511 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002512 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002513 }
2514
2515 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(target, level));
2516 if (!destRenderTarget)
2517 {
2518 source->Release();
2519 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002520 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002521 }
2522
2523 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2524 if (!dest)
2525 {
2526 source->Release();
2527 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002528 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002529 }
2530
2531 gl::Rectangle destRect;
2532 destRect.x = xoffset;
2533 destRect.y = yoffset;
2534 destRect.width = sourceRect.width;
2535 destRect.height = sourceRect.height;
2536
2537 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
2538 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
2539
2540 source->Release();
2541 dest->Release();
2542
2543 return ret;
2544}
2545
2546bool Renderer11::copyTexture(ID3D11ShaderResourceView *source, const gl::Rectangle &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight,
2547 ID3D11RenderTargetView *dest, const gl::Rectangle &destArea, unsigned int destWidth, unsigned int destHeight, GLenum destFormat)
2548{
2549 HRESULT result;
2550
2551 if (!mCopyResourcesInitialized)
2552 {
2553 ASSERT(!mCopyVB && !mCopySampler && !mCopyIL && !mCopyVS && !mCopyRGBAPS && !mCopyRGBPS && !mCopyLumPS && !mCopyLumAlphaPS);
2554
2555 D3D11_BUFFER_DESC vbDesc;
2556 vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4;
2557 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
2558 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
2559 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
2560 vbDesc.MiscFlags = 0;
2561 vbDesc.StructureByteStride = 0;
2562
2563 result = mDevice->CreateBuffer(&vbDesc, NULL, &mCopyVB);
2564 ASSERT(SUCCEEDED(result));
2565 d3d11::SetDebugName(mCopyVB, "Renderer11 copy texture vertex buffer");
2566
2567 D3D11_SAMPLER_DESC samplerDesc;
2568 samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
2569 samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
2570 samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
2571 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
2572 samplerDesc.MipLODBias = 0.0f;
2573 samplerDesc.MaxAnisotropy = 0;
2574 samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
2575 samplerDesc.BorderColor[0] = 0.0f;
2576 samplerDesc.BorderColor[1] = 0.0f;
2577 samplerDesc.BorderColor[2] = 0.0f;
2578 samplerDesc.BorderColor[3] = 0.0f;
2579 samplerDesc.MinLOD = 0.0f;
2580 samplerDesc.MaxLOD = 0.0f;
2581
2582 result = mDevice->CreateSamplerState(&samplerDesc, &mCopySampler);
2583 ASSERT(SUCCEEDED(result));
2584 d3d11::SetDebugName(mCopySampler, "Renderer11 copy sampler");
2585
2586 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
2587 {
2588 { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2589 { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2590 };
2591
2592 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mCopyIL);
2593 ASSERT(SUCCEEDED(result));
2594 d3d11::SetDebugName(mCopyIL, "Renderer11 copy texture input layout");
2595
2596 result = mDevice->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mCopyVS);
2597 ASSERT(SUCCEEDED(result));
2598 d3d11::SetDebugName(mCopyVS, "Renderer11 copy texture vertex shader");
2599
2600 result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mCopyRGBAPS);
2601 ASSERT(SUCCEEDED(result));
2602 d3d11::SetDebugName(mCopyRGBAPS, "Renderer11 copy texture RGBA pixel shader");
2603
2604 result = mDevice->CreatePixelShader(g_PS_PassthroughRGB, sizeof(g_PS_PassthroughRGB), NULL, &mCopyRGBPS);
2605 ASSERT(SUCCEEDED(result));
2606 d3d11::SetDebugName(mCopyRGBPS, "Renderer11 copy texture RGB pixel shader");
2607
2608 result = mDevice->CreatePixelShader(g_PS_PassthroughLum, sizeof(g_PS_PassthroughLum), NULL, &mCopyLumPS);
2609 ASSERT(SUCCEEDED(result));
2610 d3d11::SetDebugName(mCopyLumPS, "Renderer11 copy texture luminance pixel shader");
2611
2612 result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha, sizeof(g_PS_PassthroughLumAlpha), NULL, &mCopyLumAlphaPS);
2613 ASSERT(SUCCEEDED(result));
2614 d3d11::SetDebugName(mCopyLumAlphaPS, "Renderer11 copy texture luminance alpha pixel shader");
2615
2616 mCopyResourcesInitialized = true;
2617 }
2618
2619 // Verify the source and destination area sizes
2620 if (sourceArea.x < 0 || sourceArea.x + sourceArea.width > static_cast<int>(sourceWidth) ||
2621 sourceArea.y < 0 || sourceArea.y + sourceArea.height > static_cast<int>(sourceHeight) ||
2622 destArea.x < 0 || destArea.x + destArea.width > static_cast<int>(destWidth) ||
2623 destArea.y < 0 || destArea.y + destArea.height > static_cast<int>(destHeight))
2624 {
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002625 return gl::error(GL_INVALID_VALUE, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002626 }
2627
2628 // Set vertices
2629 D3D11_MAPPED_SUBRESOURCE mappedResource;
2630 result = mDeviceContext->Map(mCopyVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
2631 if (FAILED(result))
2632 {
2633 ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002634 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002635 }
2636
2637 d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData);
2638
2639 // Create a quad in homogeneous coordinates
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00002640 float x1 = (destArea.x / float(destWidth)) * 2.0f - 1.0f;
shannon.woods@transgaming.comcfdfd462013-02-28 23:13:01 +00002641 float y1 = ((destHeight - destArea.y - destArea.height) / float(destHeight)) * 2.0f - 1.0f;
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00002642 float x2 = ((destArea.x + destArea.width) / float(destWidth)) * 2.0f - 1.0f;
shannon.woods@transgaming.comcfdfd462013-02-28 23:13:01 +00002643 float y2 = ((destHeight - destArea.y) / float(destHeight)) * 2.0f - 1.0f;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002644
2645 float u1 = sourceArea.x / float(sourceWidth);
2646 float v1 = sourceArea.y / float(sourceHeight);
2647 float u2 = (sourceArea.x + sourceArea.width) / float(sourceWidth);
2648 float v2 = (sourceArea.y + sourceArea.height) / float(sourceHeight);
2649
2650 d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2);
2651 d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1);
2652 d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2);
2653 d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1);
2654
2655 mDeviceContext->Unmap(mCopyVB, 0);
2656
2657 static UINT stride = sizeof(d3d11::PositionTexCoordVertex);
2658 static UINT startIdx = 0;
2659 mDeviceContext->IASetVertexBuffers(0, 1, &mCopyVB, &stride, &startIdx);
2660
2661 // Apply state
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00002662 mDeviceContext->OMSetBlendState(NULL, NULL, 0xFFFFFFF);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002663 mDeviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF);
2664 mDeviceContext->RSSetState(NULL);
2665
2666 // Apply shaders
2667 mDeviceContext->IASetInputLayout(mCopyIL);
2668 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
2669 mDeviceContext->VSSetShader(mCopyVS, NULL, 0);
2670
2671 ID3D11PixelShader *ps = NULL;
2672 switch(destFormat)
2673 {
2674 case GL_RGBA: ps = mCopyRGBAPS; break;
2675 case GL_RGB: ps = mCopyRGBPS; break;
2676 case GL_ALPHA: ps = mCopyRGBAPS; break;
2677 case GL_BGRA_EXT: ps = mCopyRGBAPS; break;
2678 case GL_LUMINANCE: ps = mCopyLumPS; break;
2679 case GL_LUMINANCE_ALPHA: ps = mCopyLumAlphaPS; break;
2680 default: UNREACHABLE(); ps = NULL; break;
2681 }
2682
2683 mDeviceContext->PSSetShader(ps, NULL, 0);
shannon.woods@transgaming.com28e7ba02013-02-28 23:09:28 +00002684 mDeviceContext->GSSetShader(NULL, NULL, 0);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002685
2686 // Unset the currently bound shader resource to avoid conflicts
2687 static ID3D11ShaderResourceView *const nullSRV = NULL;
2688 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
2689
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002690 // Apply render target
2691 setOneTimeRenderTarget(dest);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002692
2693 // Set the viewport
2694 D3D11_VIEWPORT viewport;
2695 viewport.TopLeftX = 0;
2696 viewport.TopLeftY = 0;
2697 viewport.Width = destWidth;
2698 viewport.Height = destHeight;
2699 viewport.MinDepth = 0.0f;
2700 viewport.MaxDepth = 1.0f;
2701 mDeviceContext->RSSetViewports(1, &viewport);
2702
2703 // Apply textures
2704 mDeviceContext->PSSetShaderResources(0, 1, &source);
2705 mDeviceContext->PSSetSamplers(0, 1, &mCopySampler);
2706
2707 // Draw the quad
2708 mDeviceContext->Draw(4, 0);
2709
2710 // Unbind textures and render targets and vertex buffer
2711 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
2712
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002713 unapplyRenderTargets();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002714
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002715 UINT zero = 0;
2716 ID3D11Buffer *const nullBuffer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002717 mDeviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
2718
2719 markAllStateDirty();
2720
2721 return true;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002722}
2723
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002724void Renderer11::unapplyRenderTargets()
2725{
2726 setOneTimeRenderTarget(NULL);
2727}
2728
2729void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2730{
2731 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2732
2733 rtvArray[0] = renderTargetView;
2734
2735 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2736
2737 // Do not preserve the serial for this one-time-use render target
2738 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2739 {
2740 mAppliedRenderTargetSerials[rtIndex] = 0;
2741 }
2742}
2743
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002744RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2745{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002746 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002747 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002748
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002749 if (depth)
2750 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002751 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002752 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
2753 swapChain11->getDepthStencilTexture(), NULL,
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002754 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002755 }
2756 else
2757 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002758 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002759 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002760 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002761 swapChain11->getRenderTargetShaderResource(),
2762 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002763 }
2764 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002765}
2766
2767RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2768{
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002769 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples, depth);
2770 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002771}
2772
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002773ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002774{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002775 ShaderExecutable11 *executable = NULL;
2776
2777 switch (type)
2778 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002779 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002780 {
2781 ID3D11VertexShader *vshader = NULL;
2782 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2783 ASSERT(SUCCEEDED(result));
2784
2785 if (vshader)
2786 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002787 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002788 }
2789 }
2790 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002791 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002792 {
2793 ID3D11PixelShader *pshader = NULL;
2794 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2795 ASSERT(SUCCEEDED(result));
2796
2797 if (pshader)
2798 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002799 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002800 }
2801 }
2802 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002803 case rx::SHADER_GEOMETRY:
2804 {
2805 ID3D11GeometryShader *gshader = NULL;
2806 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2807 ASSERT(SUCCEEDED(result));
2808
2809 if (gshader)
2810 {
2811 executable = new ShaderExecutable11(function, length, gshader);
2812 }
2813 }
2814 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002815 default:
2816 UNREACHABLE();
2817 break;
2818 }
2819
2820 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002821}
2822
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002823ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002824{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002825 const char *profile = NULL;
2826
2827 switch (type)
2828 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002829 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002830 profile = "vs_4_0";
2831 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002832 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002833 profile = "ps_4_0";
2834 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002835 case rx::SHADER_GEOMETRY:
2836 profile = "gs_4_0";
2837 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002838 default:
2839 UNREACHABLE();
2840 return NULL;
2841 }
2842
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00002843 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002844 if (!binary)
2845 return NULL;
2846
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002847 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002848 binary->Release();
2849
2850 return executable;
2851}
2852
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002853VertexBuffer *Renderer11::createVertexBuffer()
2854{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002855 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002856}
2857
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002858IndexBuffer *Renderer11::createIndexBuffer()
2859{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002860 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002861}
2862
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002863BufferStorage *Renderer11::createBufferStorage()
2864{
2865 return new BufferStorage11(this);
2866}
2867
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002868QueryImpl *Renderer11::createQuery(GLenum type)
2869{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002870 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002871}
2872
2873FenceImpl *Renderer11::createFence()
2874{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002875 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002876}
2877
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002878bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002879{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002880 ASSERT(colorbuffer != NULL);
2881
2882 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2883 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002884 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002885 *subresourceIndex = renderTarget->getSubresourceIndex();
2886
2887 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2888 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002889 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002890 ID3D11Resource *textureResource = NULL;
2891 colorBufferRTV->GetResource(&textureResource);
2892 colorBufferRTV->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002893
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002894 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002895 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002896 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
2897 textureResource->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002898
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002899 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002900 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002901 return true;
2902 }
2903 else
2904 {
2905 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2906 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002907 }
2908 }
2909 }
2910 }
2911
2912 return false;
2913}
2914
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002915bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002916 bool blitRenderTarget, bool blitDepthStencil)
2917{
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002918 if (blitRenderTarget && !blitRect(readTarget, readRect, drawTarget, drawRect, BLIT_RENDERTARGET))
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002919 {
2920 return false;
2921 }
2922
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002923 if (blitDepthStencil && !blitRect(readTarget, readRect, drawTarget, drawRect, BLIT_DEPTHSTENCIL))
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002924 {
2925 return false;
2926 }
2927
2928 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002929}
2930
2931void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2932 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2933{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002934 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002935 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002936
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002937 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2938
2939 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002940 {
2941 gl::Rectangle area;
2942 area.x = x;
2943 area.y = y;
2944 area.width = width;
2945 area.height = height;
2946
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002947 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
2948 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002949
2950 colorBufferTexture->Release();
2951 colorBufferTexture = NULL;
2952 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002953}
2954
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002955Image *Renderer11::createImage()
2956{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002957 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00002958}
2959
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002960void Renderer11::generateMipmap(Image *dest, Image *src)
2961{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00002962 Image11 *dest11 = Image11::makeImage11(dest);
2963 Image11 *src11 = Image11::makeImage11(src);
2964 Image11::generateMipmap(dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00002965}
2966
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002967TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
2968{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002969 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
2970 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002971}
2972
2973TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
2974{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002975 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002976}
2977
2978TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
2979{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00002980 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00002981}
2982
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002983static inline unsigned int getFastPixelCopySize(DXGI_FORMAT sourceFormat, GLenum destFormat, GLenum destType)
2984{
2985 if (sourceFormat == DXGI_FORMAT_A8_UNORM &&
2986 destFormat == GL_ALPHA &&
2987 destType == GL_UNSIGNED_BYTE)
2988 {
2989 return 1;
2990 }
2991 else if (sourceFormat == DXGI_FORMAT_R8G8B8A8_UNORM &&
2992 destFormat == GL_RGBA &&
2993 destType == GL_UNSIGNED_BYTE)
2994 {
2995 return 4;
2996 }
2997 else if (sourceFormat == DXGI_FORMAT_B8G8R8A8_UNORM &&
2998 destFormat == GL_BGRA_EXT &&
2999 destType == GL_UNSIGNED_BYTE)
3000 {
3001 return 4;
3002 }
3003 else if (sourceFormat == DXGI_FORMAT_R16G16B16A16_FLOAT &&
3004 destFormat == GL_RGBA &&
3005 destType == GL_HALF_FLOAT_OES)
3006 {
3007 return 8;
3008 }
3009 else if (sourceFormat == DXGI_FORMAT_R32G32B32_FLOAT &&
3010 destFormat == GL_RGB &&
3011 destType == GL_FLOAT)
3012 {
3013 return 12;
3014 }
3015 else if (sourceFormat == DXGI_FORMAT_R32G32B32A32_FLOAT &&
3016 destFormat == GL_RGBA &&
3017 destType == GL_FLOAT)
3018 {
3019 return 16;
3020 }
3021 else
3022 {
3023 return 0;
3024 }
3025}
3026
3027static inline void readPixelColor(const unsigned char *data, DXGI_FORMAT format, unsigned int x,
3028 unsigned int y, int inputPitch, gl::Color *outColor)
3029{
3030 switch (format)
3031 {
3032 case DXGI_FORMAT_R8G8B8A8_UNORM:
3033 {
3034 unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
shannon.woods@transgaming.com25950ea2013-02-28 23:04:15 +00003035 outColor->red = (rgba & 0x000000FF) * (1.0f / 0x000000FF);
3036 outColor->green = (rgba & 0x0000FF00) * (1.0f / 0x0000FF00);
3037 outColor->blue = (rgba & 0x00FF0000) * (1.0f / 0x00FF0000);
3038 outColor->alpha = (rgba & 0xFF000000) * (1.0f / 0xFF000000);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003039 }
3040 break;
3041
3042 case DXGI_FORMAT_A8_UNORM:
3043 {
3044 outColor->red = 0.0f;
3045 outColor->green = 0.0f;
3046 outColor->blue = 0.0f;
3047 outColor->alpha = *(data + x + y * inputPitch) / 255.0f;
3048 }
3049 break;
3050
3051 case DXGI_FORMAT_R32G32B32A32_FLOAT:
3052 {
3053 outColor->red = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 0);
3054 outColor->green = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 1);
3055 outColor->blue = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 2);
3056 outColor->alpha = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 3);
3057 }
3058 break;
3059
3060 case DXGI_FORMAT_R32G32B32_FLOAT:
3061 {
3062 outColor->red = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 0);
3063 outColor->green = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 1);
3064 outColor->blue = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 2);
3065 outColor->alpha = 1.0f;
3066 }
3067 break;
3068
3069 case DXGI_FORMAT_R16G16B16A16_FLOAT:
3070 {
3071 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 0));
3072 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 1));
3073 outColor->blue = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 2));
3074 outColor->alpha = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 3));
3075 }
3076 break;
3077
3078 case DXGI_FORMAT_B8G8R8A8_UNORM:
3079 {
3080 unsigned int bgra = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
shannon.woods@transgaming.com25950ea2013-02-28 23:04:15 +00003081 outColor->red = (bgra & 0x00FF0000) * (1.0f / 0x00FF0000);
3082 outColor->blue = (bgra & 0x000000FF) * (1.0f / 0x000000FF);
3083 outColor->green = (bgra & 0x0000FF00) * (1.0f / 0x0000FF00);
3084 outColor->alpha = (bgra & 0xFF000000) * (1.0f / 0xFF000000);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003085 }
3086 break;
3087
3088 case DXGI_FORMAT_R8_UNORM:
3089 {
3090 outColor->red = *(data + x + y * inputPitch) / 255.0f;
3091 outColor->green = 0.0f;
3092 outColor->blue = 0.0f;
3093 outColor->alpha = 1.0f;
3094 }
3095 break;
3096
3097 case DXGI_FORMAT_R8G8_UNORM:
3098 {
3099 unsigned short rg = *reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch);
3100
3101 outColor->red = (rg & 0xFF00) * (1.0f / 0xFF00);
3102 outColor->green = (rg & 0x00FF) * (1.0f / 0x00FF);
3103 outColor->blue = 0.0f;
3104 outColor->alpha = 1.0f;
3105 }
3106 break;
3107
3108 case DXGI_FORMAT_R16_FLOAT:
3109 {
3110 outColor->red = gl::float16ToFloat32(*reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch));
3111 outColor->green = 0.0f;
3112 outColor->blue = 0.0f;
3113 outColor->alpha = 1.0f;
3114 }
3115 break;
3116
3117 case DXGI_FORMAT_R16G16_FLOAT:
3118 {
3119 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 0));
3120 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 1));
3121 outColor->blue = 0.0f;
3122 outColor->alpha = 1.0f;
3123 }
3124 break;
3125
3126 default:
3127 ERR("ReadPixelColor not implemented for DXGI format %u.", format);
3128 UNIMPLEMENTED();
3129 break;
3130 }
3131}
3132
3133static inline void writePixelColor(const gl::Color &color, GLenum format, GLenum type, unsigned int x,
3134 unsigned int y, int outputPitch, void *outData)
3135{
3136 unsigned char* byteData = reinterpret_cast<unsigned char*>(outData);
3137 unsigned short* shortData = reinterpret_cast<unsigned short*>(outData);
3138
3139 switch (format)
3140 {
3141 case GL_RGBA:
3142 switch (type)
3143 {
3144 case GL_UNSIGNED_BYTE:
3145 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
3146 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3147 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3148 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
3149 break;
3150
3151 default:
3152 ERR("WritePixelColor not implemented for format GL_RGBA and type 0x%X.", type);
3153 UNIMPLEMENTED();
3154 break;
3155 }
3156 break;
3157
3158 case GL_BGRA_EXT:
3159 switch (type)
3160 {
3161 case GL_UNSIGNED_BYTE:
3162 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3163 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3164 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.red + 0.5f);
3165 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
3166 break;
3167
3168 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
3169 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
3170 // this type is packed as follows:
3171 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3172 // --------------------------------------------------------------------------------
3173 // | 4th | 3rd | 2nd | 1st component |
3174 // --------------------------------------------------------------------------------
3175 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
3176 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3177 (static_cast<unsigned short>(15 * color.alpha + 0.5f) << 12) |
3178 (static_cast<unsigned short>(15 * color.red + 0.5f) << 8) |
3179 (static_cast<unsigned short>(15 * color.green + 0.5f) << 4) |
3180 (static_cast<unsigned short>(15 * color.blue + 0.5f) << 0);
3181 break;
3182
3183 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
3184 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
3185 // this type is packed as follows:
3186 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3187 // --------------------------------------------------------------------------------
3188 // | 4th | 3rd | 2nd | 1st component |
3189 // --------------------------------------------------------------------------------
3190 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
3191 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3192 (static_cast<unsigned short>( color.alpha + 0.5f) << 15) |
3193 (static_cast<unsigned short>(31 * color.red + 0.5f) << 10) |
3194 (static_cast<unsigned short>(31 * color.green + 0.5f) << 5) |
3195 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0);
3196 break;
3197
3198 default:
3199 ERR("WritePixelColor not implemented for format GL_BGRA_EXT and type 0x%X.", type);
3200 UNIMPLEMENTED();
3201 break;
3202 }
3203 break;
3204
3205 case GL_RGB:
3206 switch (type)
3207 {
3208 case GL_UNSIGNED_SHORT_5_6_5:
3209 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3210 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0) |
3211 (static_cast<unsigned short>(63 * color.green + 0.5f) << 5) |
3212 (static_cast<unsigned short>(31 * color.red + 0.5f) << 11);
3213 break;
3214
3215 case GL_UNSIGNED_BYTE:
3216 byteData[3 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
3217 byteData[3 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3218 byteData[3 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3219 break;
3220
3221 default:
3222 ERR("WritePixelColor not implemented for format GL_RGB and type 0x%X.", type);
3223 UNIMPLEMENTED();
3224 break;
3225 }
3226 break;
3227
3228 default:
3229 ERR("WritePixelColor not implemented for format 0x%X.", format);
3230 UNIMPLEMENTED();
3231 break;
3232 }
3233}
3234
3235void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3236 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3237 GLint packAlignment, void *pixels)
3238{
3239 D3D11_TEXTURE2D_DESC textureDesc;
3240 texture->GetDesc(&textureDesc);
3241
3242 D3D11_TEXTURE2D_DESC stagingDesc;
3243 stagingDesc.Width = area.width;
3244 stagingDesc.Height = area.height;
3245 stagingDesc.MipLevels = 1;
3246 stagingDesc.ArraySize = 1;
3247 stagingDesc.Format = textureDesc.Format;
3248 stagingDesc.SampleDesc.Count = 1;
3249 stagingDesc.SampleDesc.Quality = 0;
3250 stagingDesc.Usage = D3D11_USAGE_STAGING;
3251 stagingDesc.BindFlags = 0;
3252 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3253 stagingDesc.MiscFlags = 0;
3254
3255 ID3D11Texture2D* stagingTex = NULL;
3256 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3257 if (FAILED(result))
3258 {
3259 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3260 return;
3261 }
3262
3263 ID3D11Texture2D* srcTex = NULL;
3264 if (textureDesc.SampleDesc.Count > 1)
3265 {
3266 D3D11_TEXTURE2D_DESC resolveDesc;
3267 resolveDesc.Width = textureDesc.Width;
3268 resolveDesc.Height = textureDesc.Height;
3269 resolveDesc.MipLevels = 1;
3270 resolveDesc.ArraySize = 1;
3271 resolveDesc.Format = textureDesc.Format;
3272 resolveDesc.SampleDesc.Count = 1;
3273 resolveDesc.SampleDesc.Quality = 0;
3274 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3275 resolveDesc.BindFlags = 0;
3276 resolveDesc.CPUAccessFlags = 0;
3277 resolveDesc.MiscFlags = 0;
3278
3279 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3280 if (FAILED(result))
3281 {
3282 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
3283 stagingTex->Release();
3284 return;
3285 }
3286
3287 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3288 subResource = 0;
3289 }
3290 else
3291 {
3292 srcTex = texture;
3293 srcTex->AddRef();
3294 }
3295
3296 D3D11_BOX srcBox;
3297 srcBox.left = area.x;
3298 srcBox.right = area.x + area.width;
3299 srcBox.top = area.y;
3300 srcBox.bottom = area.y + area.height;
3301 srcBox.front = 0;
3302 srcBox.back = 1;
3303
3304 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3305
3306 srcTex->Release();
3307 srcTex = NULL;
3308
3309 D3D11_MAPPED_SUBRESOURCE mapping;
3310 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3311
3312 unsigned char *source;
3313 int inputPitch;
3314 if (packReverseRowOrder)
3315 {
3316 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3317 inputPitch = -static_cast<int>(mapping.RowPitch);
3318 }
3319 else
3320 {
3321 source = static_cast<unsigned char*>(mapping.pData);
3322 inputPitch = static_cast<int>(mapping.RowPitch);
3323 }
3324
3325 unsigned int fastPixelSize = getFastPixelCopySize(textureDesc.Format, format, type);
3326 if (fastPixelSize != 0)
3327 {
3328 unsigned char *dest = static_cast<unsigned char*>(pixels);
3329 for (int j = 0; j < area.height; j++)
3330 {
3331 memcpy(dest + j * outputPitch, source + j * inputPitch, area.width * fastPixelSize);
3332 }
3333 }
3334 else
3335 {
3336 gl::Color pixelColor;
3337 for (int j = 0; j < area.height; j++)
3338 {
3339 for (int i = 0; i < area.width; i++)
3340 {
3341 readPixelColor(source, textureDesc.Format, i, j, inputPitch, &pixelColor);
3342 writePixelColor(pixelColor, format, type, i, j, outputPitch, pixels);
3343 }
3344 }
3345 }
3346
3347 mDeviceContext->Unmap(stagingTex, 0);
3348
3349 stagingTex->Release();
3350 stagingTex = NULL;
3351}
3352
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003353bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget,
3354 const gl::Rectangle &drawRect, BlitTarget target)
3355{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00003356 // TODO: mrt support
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003357 ASSERT(readRect.width == drawRect.width && readRect.height == drawRect.height);
3358
3359 gl::Renderbuffer *readBuffer = NULL;
3360 switch (target)
3361 {
3362 case BLIT_RENDERTARGET:
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00003363 readBuffer = readTarget->getColorbuffer(0);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003364 break;
3365 case BLIT_DEPTHSTENCIL:
3366 readBuffer = readTarget->getDepthOrStencilbuffer();
3367 break;
3368 default: UNREACHABLE();
3369 }
3370 if (!readBuffer)
3371 {
3372 ERR("Failed to retrieve the color buffer from the read target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003373 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003374 }
3375
3376 RenderTarget11 *sourceRenderTarget = NULL;
3377 switch (target)
3378 {
3379 case BLIT_RENDERTARGET:
3380 sourceRenderTarget = RenderTarget11::makeRenderTarget11(readBuffer->getRenderTarget());
3381 break;
3382 case BLIT_DEPTHSTENCIL:
3383 sourceRenderTarget = RenderTarget11::makeRenderTarget11(readBuffer->getDepthStencil());
3384 break;
3385 default: UNREACHABLE();
3386 }
3387 if (!sourceRenderTarget)
3388 {
3389 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003390 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003391 }
3392
3393 ID3D11Texture2D *source = NULL;
3394 unsigned int sourceSubresource = 0;
3395 if (sourceRenderTarget->getSamples() > 0)
3396 {
3397 ID3D11Texture2D *unresolvedTexture = sourceRenderTarget->getTexture();
3398
3399 source = resolveMultisampledTexture(unresolvedTexture, sourceRenderTarget->getSubresourceIndex());
3400 sourceSubresource = 0;
3401
3402 unresolvedTexture->Release();
3403 }
3404 else
3405 {
3406 source = sourceRenderTarget->getTexture();
3407 sourceSubresource = sourceRenderTarget->getSubresourceIndex();
3408 }
3409
3410 if (!source)
3411 {
3412 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003413 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003414 }
3415
3416
3417 gl::Renderbuffer *drawBuffer = NULL;
3418 switch (target)
3419 {
3420 case BLIT_RENDERTARGET:
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00003421 drawBuffer = drawTarget->getColorbuffer(0);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003422 break;
3423 case BLIT_DEPTHSTENCIL:
3424 drawBuffer = drawTarget->getDepthOrStencilbuffer();
3425 break;
3426 default: UNREACHABLE();
3427 }
3428 if (!drawBuffer)
3429 {
3430 source->Release();
3431 ERR("Failed to retrieve the color buffer from the draw buffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003432 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003433 }
3434
3435 RenderTarget11 *drawRenderTarget = NULL;
3436 switch (target)
3437 {
3438 case BLIT_RENDERTARGET:
3439 drawRenderTarget = RenderTarget11::makeRenderTarget11(drawBuffer->getRenderTarget());
3440 break;
3441 case BLIT_DEPTHSTENCIL:
3442 drawRenderTarget = RenderTarget11::makeRenderTarget11(drawBuffer->getDepthStencil());
3443 break;
3444 default: UNREACHABLE();
3445 }
3446 if (!drawRenderTarget)
3447 {
3448 source->Release();
3449 ERR("Failed to retrieve the render target from the render buffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003450 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003451 }
3452
3453 ID3D11Texture2D *dest = drawRenderTarget->getTexture();
3454 unsigned int destSubresource = drawRenderTarget->getSubresourceIndex();
3455
3456 D3D11_BOX srcBox;
3457 srcBox.left = readRect.x;
3458 srcBox.right = readRect.x + readRect.width;
3459 srcBox.top = readRect.y;
3460 srcBox.bottom = readRect.y + readRect.height;
3461 srcBox.front = 0;
3462 srcBox.back = 1;
3463
3464 mDeviceContext->CopySubresourceRegion(dest, destSubresource, drawRect.x, drawRect.y, 0,
3465 source, sourceSubresource, &srcBox);
3466
3467 source->Release();
3468 dest->Release();
3469
3470 return true;
3471}
3472
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003473ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3474{
3475 D3D11_TEXTURE2D_DESC textureDesc;
3476 source->GetDesc(&textureDesc);
3477
3478 if (textureDesc.SampleDesc.Count > 1)
3479 {
3480 D3D11_TEXTURE2D_DESC resolveDesc;
3481 resolveDesc.Width = textureDesc.Width;
3482 resolveDesc.Height = textureDesc.Height;
3483 resolveDesc.MipLevels = 1;
3484 resolveDesc.ArraySize = 1;
3485 resolveDesc.Format = textureDesc.Format;
3486 resolveDesc.SampleDesc.Count = 1;
3487 resolveDesc.SampleDesc.Quality = 0;
3488 resolveDesc.Usage = textureDesc.Usage;
3489 resolveDesc.BindFlags = textureDesc.BindFlags;
3490 resolveDesc.CPUAccessFlags = 0;
3491 resolveDesc.MiscFlags = 0;
3492
3493 ID3D11Texture2D *resolveTexture = NULL;
3494 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3495 if (FAILED(result))
3496 {
3497 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3498 return NULL;
3499 }
3500
3501 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3502 return resolveTexture;
3503 }
3504 else
3505 {
3506 source->AddRef();
3507 return source;
3508 }
3509}
3510
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003511}