blob: e34767733cda218d2b793b1ed71995e2868b7ba3 [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002//
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00003// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// Renderer11.cpp: Implements a back-end specific class for the D3D11 renderer.
9
daniel@transgaming.com5503fd02012-11-28 19:38:57 +000010#include "libGLESv2/main.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000011#include "libGLESv2/utilities.h"
daniel@transgaming.com18adad02012-11-28 21:04:03 +000012#include "libGLESv2/Buffer.h"
daniel@transgaming.com53670042012-11-28 20:55:51 +000013#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com80fc3322012-11-28 21:02:13 +000014#include "libGLESv2/Framebuffer.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000015#include "libGLESv2/RenderBuffer.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000016#include "libGLESv2/renderer/Renderer11.h"
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +000017#include "libGLESv2/renderer/RenderTarget11.h"
daniel@transgaming.com65e65372012-11-28 19:33:50 +000018#include "libGLESv2/renderer/renderer11_utils.h"
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +000019#include "libGLESv2/renderer/ShaderExecutable11.h"
daniel@transgaming.coma60160b2012-11-28 19:41:15 +000020#include "libGLESv2/renderer/SwapChain11.h"
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000021#include "libGLESv2/renderer/Image11.h"
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +000022#include "libGLESv2/renderer/VertexBuffer11.h"
daniel@transgaming.com11c2af52012-12-20 21:10:01 +000023#include "libGLESv2/renderer/IndexBuffer11.h"
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +000024#include "libGLESv2/renderer/BufferStorage11.h"
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000025#include "libGLESv2/renderer/VertexDataManager.h"
26#include "libGLESv2/renderer/IndexDataManager.h"
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +000027#include "libGLESv2/renderer/TextureStorage11.h"
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +000028#include "libGLESv2/renderer/Query11.h"
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +000029#include "libGLESv2/renderer/Fence11.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000030
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000031#include "libGLESv2/renderer/shaders/compiled/passthrough11vs.h"
32#include "libGLESv2/renderer/shaders/compiled/passthroughrgba11ps.h"
33#include "libGLESv2/renderer/shaders/compiled/passthroughrgb11ps.h"
34#include "libGLESv2/renderer/shaders/compiled/passthroughlum11ps.h"
35#include "libGLESv2/renderer/shaders/compiled/passthroughlumalpha11ps.h"
36
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000037#include "libGLESv2/renderer/shaders/compiled/clear11vs.h"
38#include "libGLESv2/renderer/shaders/compiled/clear11ps.h"
39
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000040#include "libEGL/Display.h"
41
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000042#ifdef _DEBUG
43// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
44// and conformance tests. to enable all warnings, remove this define.
45#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
46#endif
47
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000048namespace rx
49{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000050static const DXGI_FORMAT RenderTargetFormats[] =
51 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000052 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000053 DXGI_FORMAT_R8G8B8A8_UNORM
54 };
55
56static const DXGI_FORMAT DepthStencilFormats[] =
57 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000058 DXGI_FORMAT_UNKNOWN,
59 DXGI_FORMAT_D24_UNORM_S8_UINT,
60 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000061 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000062
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000063enum
64{
65 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
66};
67
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000068Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
69{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000070 mVertexDataManager = NULL;
71 mIndexDataManager = NULL;
72
daniel@transgaming.comc5114302012-12-20 21:11:36 +000073 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000074 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000075
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000076 mCopyResourcesInitialized = false;
77 mCopyVB = NULL;
78 mCopySampler = NULL;
79 mCopyIL = NULL;
80 mCopyVS = NULL;
81 mCopyRGBAPS = NULL;
82 mCopyRGBPS = NULL;
83 mCopyLumPS = NULL;
84 mCopyLumAlphaPS = NULL;
85
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000086 mClearResourcesInitialized = false;
87 mClearVB = NULL;
88 mClearIL = NULL;
89 mClearVS = NULL;
90 mClearPS = NULL;
91 mClearScissorRS = NULL;
92 mClearNoScissorRS = NULL;
93
daniel@transgaming.combdf787f2013-02-01 03:20:36 +000094 mSyncQuery = NULL;
95
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000096 mD3d11Module = NULL;
97 mDxgiModule = NULL;
98
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000099 mDeviceLost = false;
100
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000101 mMaxSupportedSamples = 0;
102
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000103 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000104 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000105 mDxgiAdapter = NULL;
106 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000107
108 mDriverConstantBufferVS = NULL;
109 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000110
111 mBGRATextureSupport = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +0000112
113 mIsGeometryShaderActive = false;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000114}
115
116Renderer11::~Renderer11()
117{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000118 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000119}
120
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000121Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
122{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000123 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000124 return static_cast<rx::Renderer11*>(renderer);
125}
126
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000127#ifndef __d3d11_1_h__
128#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
129#endif
130
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000131EGLint Renderer11::initialize()
132{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000133 if (!initializeCompiler())
134 {
135 return EGL_NOT_INITIALIZED;
136 }
137
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000138 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
139 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000140
141 if (mD3d11Module == NULL || mDxgiModule == NULL)
142 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000143 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000144 return EGL_NOT_INITIALIZED;
145 }
146
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000147 // create the D3D11 device
148 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000149 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000150
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000151 if (D3D11CreateDevice == NULL)
152 {
153 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
154 return EGL_NOT_INITIALIZED;
155 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000156
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000157 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000158 {
159 D3D_FEATURE_LEVEL_11_0,
160 D3D_FEATURE_LEVEL_10_1,
161 D3D_FEATURE_LEVEL_10_0,
162 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000163
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000164 HRESULT result = D3D11CreateDevice(NULL,
165 D3D_DRIVER_TYPE_HARDWARE,
166 NULL,
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000167 #if defined(_DEBUG)
168 D3D11_CREATE_DEVICE_DEBUG,
169 #else
170 0,
171 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000172 featureLevels,
173 ArraySize(featureLevels),
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000174 D3D11_SDK_VERSION,
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000175 &mDevice,
176 &mFeatureLevel,
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000177 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000178
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000179 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000180 {
181 ERR("Could not create D3D11 device - aborting!\n");
182 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
183 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000184
185 IDXGIDevice *dxgiDevice = NULL;
186 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
187
188 if (FAILED(result))
189 {
190 ERR("Could not query DXGI device - aborting!\n");
191 return EGL_NOT_INITIALIZED;
192 }
193
194 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
195
196 if (FAILED(result))
197 {
198 ERR("Could not retrieve DXGI adapter - aborting!\n");
199 return EGL_NOT_INITIALIZED;
200 }
201
202 dxgiDevice->Release();
203
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000204 mDxgiAdapter->GetDesc(&mAdapterDescription);
205 memset(mDescription, 0, sizeof(mDescription));
206 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
207
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000208 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
209
210 if (!mDxgiFactory || FAILED(result))
211 {
212 ERR("Could not create DXGI factory - aborting!\n");
213 return EGL_NOT_INITIALIZED;
214 }
215
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000216 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
217#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
218 ID3D11InfoQueue *infoQueue;
219 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
220
221 if (SUCCEEDED(result))
222 {
223 D3D11_MESSAGE_ID hideMessages[] =
224 {
225 D3D11_MESSAGE_ID_DEVICE_OMSETRENDERTARGETS_HAZARD,
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000226 D3D11_MESSAGE_ID_DEVICE_PSSETSHADERRESOURCES_HAZARD,
227 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000228 };
229
230 D3D11_INFO_QUEUE_FILTER filter = {0};
231 filter.DenyList.NumIDs = ArraySize(hideMessages);
232 filter.DenyList.pIDList = hideMessages;
233
234 infoQueue->AddStorageFilterEntries(&filter);
235
236 infoQueue->Release();
237 }
238#endif
239
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000240 unsigned int maxSupportedSamples = 0;
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000241 unsigned int rtFormatCount = ArraySize(RenderTargetFormats);
242 unsigned int dsFormatCount = ArraySize(DepthStencilFormats);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000243 for (unsigned int i = 0; i < rtFormatCount + dsFormatCount; ++i)
244 {
245 DXGI_FORMAT format = (i < rtFormatCount) ? RenderTargetFormats[i] : DepthStencilFormats[i - rtFormatCount];
246 if (format != DXGI_FORMAT_UNKNOWN)
247 {
248 UINT formatSupport;
249 result = mDevice->CheckFormatSupport(format, &formatSupport);
250 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
251 {
252 MultisampleSupportInfo supportInfo;
253
254 for (unsigned int j = 1; j <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; j++)
255 {
256 result = mDevice->CheckMultisampleQualityLevels(format, j, &supportInfo.qualityLevels[j - 1]);
257 if (SUCCEEDED(result) && supportInfo.qualityLevels[j - 1] > 0)
258 {
259 maxSupportedSamples = std::max(j, maxSupportedSamples);
260 }
261 else
262 {
263 supportInfo.qualityLevels[j - 1] = 0;
264 }
265 }
266
267 mMultisampleSupportMap.insert(std::make_pair(format, supportInfo));
268 }
269 }
270 }
271 mMaxSupportedSamples = maxSupportedSamples;
272
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000273 initializeDevice();
274
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000275 // BGRA texture support is optional in feature levels 10 and 10_1
276 UINT formatSupport;
277 result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport);
278 if (FAILED(result))
279 {
280 ERR("Error checking BGRA format support: 0x%08X", result);
281 }
282 else
283 {
284 const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET);
285 mBGRATextureSupport = (formatSupport & flags) == flags;
286 }
287
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000288 // Check floating point texture support
289 static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE;
290 static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET;
291 static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE;
292
293 DXGI_FORMAT float16Formats[] =
294 {
295 DXGI_FORMAT_R16_FLOAT,
296 DXGI_FORMAT_R16G16_FLOAT,
297 DXGI_FORMAT_R16G16B16A16_FLOAT,
298 };
299
300 DXGI_FORMAT float32Formats[] =
301 {
302 DXGI_FORMAT_R32_FLOAT,
303 DXGI_FORMAT_R32G32_FLOAT,
304 DXGI_FORMAT_R32G32B32_FLOAT,
305 DXGI_FORMAT_R32G32B32A32_FLOAT,
306 };
307
308 mFloat16TextureSupport = true;
309 mFloat16FilterSupport = true;
310 mFloat16RenderSupport = true;
311 for (unsigned int i = 0; i < ArraySize(float16Formats); i++)
312 {
313 if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport)))
314 {
315 mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
316 mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
317 mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
318 }
319 else
320 {
321 mFloat16TextureSupport = false;
322 mFloat16RenderSupport = false;
323 mFloat16FilterSupport = false;
324 }
325 }
326
327 mFloat32TextureSupport = true;
328 mFloat32FilterSupport = true;
329 mFloat32RenderSupport = true;
330 for (unsigned int i = 0; i < ArraySize(float32Formats); i++)
331 {
332 if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport)))
333 {
334 mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
335 mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
336 mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
337 }
338 else
339 {
340 mFloat32TextureSupport = false;
341 mFloat32FilterSupport = false;
342 mFloat32RenderSupport = false;
343 }
344 }
345
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +0000346 // Check compressed texture support
347 const unsigned int requiredCompressedTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D;
348
349 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &formatSupport)))
350 {
351 mDXT1TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
352 }
353 else
354 {
355 mDXT1TextureSupport = false;
356 }
357
358 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport)))
359 {
360 mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
361 }
362 else
363 {
364 mDXT3TextureSupport = false;
365 }
366
367 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC5_UNORM, &formatSupport)))
368 {
369 mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
370 }
371 else
372 {
373 mDXT5TextureSupport = false;
374 }
375
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +0000376 // Check depth texture support
377 DXGI_FORMAT depthTextureFormats[] =
378 {
379 DXGI_FORMAT_D16_UNORM,
380 DXGI_FORMAT_D24_UNORM_S8_UINT,
381 };
382
383 static const unsigned int requiredDepthTextureFlags = D3D11_FORMAT_SUPPORT_DEPTH_STENCIL |
384 D3D11_FORMAT_SUPPORT_TEXTURE2D;
385
386 mDepthTextureSupport = true;
387 for (unsigned int i = 0; i < ArraySize(depthTextureFormats); i++)
388 {
389 if (SUCCEEDED(mDevice->CheckFormatSupport(depthTextureFormats[i], &formatSupport)))
390 {
391 mDepthTextureSupport = mDepthTextureSupport && ((formatSupport & requiredDepthTextureFlags) == requiredDepthTextureFlags);
392 }
393 else
394 {
395 mDepthTextureSupport = false;
396 }
397 }
398
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000399 return EGL_SUCCESS;
400}
401
402// do any one-time device initialization
403// NOTE: this is also needed after a device lost/reset
404// to reset the scene status and ensure the default states are reset.
405void Renderer11::initializeDevice()
406{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000407 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000408 mInputLayoutCache.initialize(mDevice, mDeviceContext);
409
410 ASSERT(!mVertexDataManager && !mIndexDataManager);
411 mVertexDataManager = new VertexDataManager(this);
412 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000413
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000414 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000415}
416
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000417int Renderer11::generateConfigs(ConfigDesc **configDescList)
418{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000419 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
420 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000421 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
422 int numConfigs = 0;
423
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000424 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000425 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000426 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000427 {
428 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
429
430 UINT formatSupport = 0;
431 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000432
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000433 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
434 {
435 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
436
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000437 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000438
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000439 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
440 {
441 UINT formatSupport = 0;
442 result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
443 depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
444 }
445
446 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000447 {
448 ConfigDesc newConfig;
449 newConfig.renderTargetFormat = d3d11_gl::ConvertBackBufferFormat(renderTargetFormat);
450 newConfig.depthStencilFormat = d3d11_gl::ConvertDepthStencilFormat(depthStencilFormat);
451 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
452 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000453 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000454
455 (*configDescList)[numConfigs++] = newConfig;
456 }
457 }
458 }
459 }
460
461 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000462}
463
464void Renderer11::deleteConfigs(ConfigDesc *configDescList)
465{
466 delete [] (configDescList);
467}
468
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000469void Renderer11::sync(bool block)
470{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000471 if (block)
472 {
473 HRESULT result;
474
475 if (!mSyncQuery)
476 {
477 D3D11_QUERY_DESC queryDesc;
478 queryDesc.Query = D3D11_QUERY_EVENT;
479 queryDesc.MiscFlags = 0;
480
481 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
482 ASSERT(SUCCEEDED(result));
483 }
484
485 mDeviceContext->End(mSyncQuery);
486 mDeviceContext->Flush();
487
488 do
489 {
490 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
491
492 // Keep polling, but allow other threads to do something useful first
493 Sleep(0);
494
495 if (testDeviceLost(true))
496 {
497 return;
498 }
499 }
500 while (result == S_FALSE);
501 }
502 else
503 {
504 mDeviceContext->Flush();
505 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000506}
507
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000508SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
509{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000510 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000511}
512
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000513void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
514{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000515 if (type == gl::SAMPLER_PIXEL)
516 {
517 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
518 {
519 ERR("Pixel shader sampler index %i is not valid.", index);
520 return;
521 }
522
523 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
524 {
525 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
526
527 if (!dxSamplerState)
528 {
529 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
530 "sampler state for pixel shaders at slot %i.", index);
531 }
532
533 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
534
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000535 mCurPixelSamplerStates[index] = samplerState;
536 }
537
538 mForceSetPixelSamplerStates[index] = false;
539 }
540 else if (type == gl::SAMPLER_VERTEX)
541 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000542 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000543 {
544 ERR("Vertex shader sampler index %i is not valid.", index);
545 return;
546 }
547
548 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
549 {
550 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
551
552 if (!dxSamplerState)
553 {
554 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
555 "sampler state for vertex shaders at slot %i.", index);
556 }
557
558 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
559
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000560 mCurVertexSamplerStates[index] = samplerState;
561 }
562
563 mForceSetVertexSamplerStates[index] = false;
564 }
565 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000566}
567
568void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
569{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000570 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000571 unsigned int serial = 0;
572 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000573
574 if (texture)
575 {
576 TextureStorageInterface *texStorage = texture->getNativeTexture();
577 if (texStorage)
578 {
579 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
580 textureSRV = storage11->getSRV();
581 }
582
583 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
584 // missing the shader resource view
585 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000586
587 serial = texture->getTextureSerial();
588 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000589 }
590
591 if (type == gl::SAMPLER_PIXEL)
592 {
593 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
594 {
595 ERR("Pixel shader sampler index %i is not valid.", index);
596 return;
597 }
598
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000599 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
600 {
601 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
602 }
603
604 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000605 }
606 else if (type == gl::SAMPLER_VERTEX)
607 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000608 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000609 {
610 ERR("Vertex shader sampler index %i is not valid.", index);
611 return;
612 }
613
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000614 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
615 {
616 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
617 }
618
619 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000620 }
621 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000622}
623
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000624void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000625{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000626 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000627 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000628 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
629 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000630 if (!dxRasterState)
631 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000632 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000633 "rasterizer state.");
634 }
635
636 mDeviceContext->RSSetState(dxRasterState);
637
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000638 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000639 }
640
641 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000642}
643
644void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor,
645 unsigned int sampleMask)
646{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000647 if (mForceSetBlendState ||
648 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
649 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0 ||
650 sampleMask != mCurSampleMask)
651 {
652 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
653 if (!dxBlendState)
654 {
655 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
656 "blend state.");
657 }
658
659 const float blendColors[] = { blendColor.red, blendColor.green, blendColor.blue, blendColor.alpha };
660 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
661
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000662 mCurBlendState = blendState;
663 mCurBlendColor = blendColor;
664 mCurSampleMask = sampleMask;
665 }
666
667 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000668}
669
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000670void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000671 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000672{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000673 if (mForceSetDepthStencilState ||
674 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
675 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
676 {
677 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
678 stencilRef != stencilBackRef ||
679 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
680 {
681 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
682 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000683 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000684 }
685
686 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
687 if (!dxDepthStencilState)
688 {
689 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
690 "setting the default depth stencil state.");
691 }
692
693 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
694
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000695 mCurDepthStencilState = depthStencilState;
696 mCurStencilRef = stencilRef;
697 mCurStencilBackRef = stencilBackRef;
698 }
699
700 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000701}
702
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000703void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000704{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000705 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
706 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000707 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000708 if (enabled)
709 {
710 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000711 rect.left = std::max(0, scissor.x);
712 rect.top = std::max(0, scissor.y);
713 rect.right = scissor.x + std::max(0, scissor.width);
714 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000715
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000716 mDeviceContext->RSSetScissorRects(1, &rect);
717 }
718
719 if (enabled != mScissorEnabled)
720 {
721 mForceSetRasterState = true;
722 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000723
724 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000725 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000726 }
727
728 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000729}
730
daniel@transgaming.com12985182012-12-20 20:56:31 +0000731bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000732 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000733{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000734 gl::Rectangle actualViewport = viewport;
735 float actualZNear = gl::clamp01(zNear);
736 float actualZFar = gl::clamp01(zFar);
737 if (ignoreViewport)
738 {
739 actualViewport.x = 0;
740 actualViewport.y = 0;
741 actualViewport.width = mRenderTargetDesc.width;
742 actualViewport.height = mRenderTargetDesc.height;
743 actualZNear = 0.0f;
744 actualZFar = 1.0f;
745 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000746
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000747 // Get D3D viewport bounds, which depends on the feature level
748 const Range& viewportBounds = getViewportBounds();
749
750 // Clamp width and height first to the gl maximum, then clamp further if we extend past the D3D maximum bounds
daniel@transgaming.com53670042012-11-28 20:55:51 +0000751 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000752 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
753 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
754 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
755 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
756 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
757 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000758 dxViewport.MinDepth = actualZNear;
759 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000760
761 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
762 {
763 return false; // Nothing to render
764 }
765
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000766 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
767 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000768
daniel@transgaming.com53670042012-11-28 20:55:51 +0000769 if (viewportChanged)
770 {
771 mDeviceContext->RSSetViewports(1, &dxViewport);
772
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000773 mCurViewport = actualViewport;
774 mCurNear = actualZNear;
775 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000776
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000777 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
778 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
779 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
780 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000781
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000782 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
783 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000784
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000785 mVertexConstants.depthRange[0] = actualZNear;
786 mVertexConstants.depthRange[1] = actualZFar;
787 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000788
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000789 mPixelConstants.depthRange[0] = actualZNear;
790 mPixelConstants.depthRange[1] = actualZFar;
791 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000792 }
793
794 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000795 return true;
796}
797
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000798bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
799{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000800 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000801
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000802 switch (mode)
803 {
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000804 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break;
805 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break;
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000806 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000807 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
808 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
809 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000810 // emulate fans via rewriting index buffer
811 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000812 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000813 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000814 }
815
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000816 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000817
818 return count > 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000819}
820
821bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000822{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000823 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000824 // Also extract the render target dimensions and view
825 unsigned int renderTargetWidth = 0;
826 unsigned int renderTargetHeight = 0;
827 GLenum renderTargetFormat = 0;
828 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
829 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
830 bool missingColorRenderTarget = true;
831
832 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000833 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000834 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
835
836 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000837 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000838 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
839 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
840
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000841 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000842
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000843 if (!colorbuffer)
844 {
845 ERR("render target pointer unexpectedly null.");
846 return false;
847 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000848
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000849 // check for zero-sized default framebuffer, which is a special case.
850 // in this case we do not wish to modify any state and just silently return false.
851 // this will not report any gl error but will cause the calling method to return.
852 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
853 {
854 return false;
855 }
856
857 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
858
859 // Extract the render target dimensions and view
860 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
861 if (!renderTarget)
862 {
863 ERR("render target pointer unexpectedly null.");
864 return false;
865 }
866
867 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
868 if (!framebufferRTVs[colorAttachment])
869 {
870 ERR("render target view pointer unexpectedly null.");
871 return false;
872 }
873
874 if (missingColorRenderTarget)
875 {
876 renderTargetWidth = colorbuffer->getWidth();
877 renderTargetHeight = colorbuffer->getHeight();
878 renderTargetFormat = colorbuffer->getActualFormat();
879 missingColorRenderTarget = false;
880 }
881 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000882 }
883
884 // Get the depth stencil render buffer and serials
885 gl::Renderbuffer *depthStencil = NULL;
886 unsigned int depthbufferSerial = 0;
887 unsigned int stencilbufferSerial = 0;
888 if (framebuffer->getDepthbufferType() != GL_NONE)
889 {
890 depthStencil = framebuffer->getDepthbuffer();
891 if (!depthStencil)
892 {
893 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000894 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000895 return false;
896 }
897
898 depthbufferSerial = depthStencil->getSerial();
899 }
900 else if (framebuffer->getStencilbufferType() != GL_NONE)
901 {
902 depthStencil = framebuffer->getStencilbuffer();
903 if (!depthStencil)
904 {
905 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000906 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000907 return false;
908 }
909
910 stencilbufferSerial = depthStencil->getSerial();
911 }
912
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000913 // Extract the depth stencil sizes and view
914 unsigned int depthSize = 0;
915 unsigned int stencilSize = 0;
916 ID3D11DepthStencilView* framebufferDSV = NULL;
917 if (depthStencil)
918 {
919 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
920 if (!depthStencilRenderTarget)
921 {
922 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000923 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000924 return false;
925 }
926
927 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
928 if (!framebufferDSV)
929 {
930 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000931 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000932 return false;
933 }
934
935 // If there is no render buffer, the width, height and format values come from
936 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000937 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000938 {
939 renderTargetWidth = depthStencil->getWidth();
940 renderTargetHeight = depthStencil->getHeight();
941 renderTargetFormat = depthStencil->getActualFormat();
942 }
943
944 depthSize = depthStencil->getDepthSize();
945 stencilSize = depthStencil->getStencilSize();
946 }
947
948 // Apply the render target and depth stencil
949 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000950 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000951 depthbufferSerial != mAppliedDepthbufferSerial ||
952 stencilbufferSerial != mAppliedStencilbufferSerial)
953 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000954 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000955
956 mRenderTargetDesc.width = renderTargetWidth;
957 mRenderTargetDesc.height = renderTargetHeight;
958 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000959 mForceSetViewport = true;
960 mForceSetScissor = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000961
962 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
963 {
964 mCurDepthSize = depthSize;
965 mForceSetRasterState = true;
966 }
967
968 mCurStencilSize = stencilSize;
969
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000970 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
971 {
972 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
973 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000974 mAppliedDepthbufferSerial = depthbufferSerial;
975 mAppliedStencilbufferSerial = stencilbufferSerial;
976 mRenderTargetDescInitialized = true;
977 mDepthStencilInitialized = true;
978 }
979
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000980 SafeRelease(framebufferRTVs);
981 SafeRelease(framebufferDSV);
daniel@transgaming.comae39ee22012-11-28 19:42:02 +0000982
983 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000984}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000985
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000986GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000987{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000988 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
989 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
990 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000991 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000992 return err;
993 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +0000994
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000995 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +0000996}
997
daniel@transgaming.com31240482012-11-28 21:06:41 +0000998GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000999{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001000 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001001
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001002 if (err == GL_NO_ERROR)
1003 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001004 if (indexInfo->storage)
1005 {
1006 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1007 {
1008 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1009 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1010
1011 mDeviceContext->IASetIndexBuffer(storage->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
1012
1013 mAppliedIBSerial = 0;
1014 mAppliedStorageIBSerial = storage->getSerial();
1015 mAppliedIBOffset = indexInfo->startOffset;
1016 }
1017 }
1018 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001019 {
1020 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1021
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001022 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001023
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001024 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001025 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001026 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001027 }
1028 }
1029
1030 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001031}
1032
1033void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1034{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001035 if (mode == GL_LINE_LOOP)
1036 {
1037 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1038 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001039 else if (mode == GL_TRIANGLE_FAN)
1040 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001041 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001042 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001043 else if (instances > 0)
1044 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001045 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001046 }
1047 else
1048 {
1049 mDeviceContext->Draw(count, 0);
1050 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001051}
1052
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001053void Renderer11::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001054{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001055 if (mode == GL_LINE_LOOP)
1056 {
1057 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1058 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001059 else if (mode == GL_TRIANGLE_FAN)
1060 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001061 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1062 }
1063 else if (instances > 0)
1064 {
1065 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001066 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001067 else
1068 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001069 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001070 }
1071}
1072
1073void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1074{
1075 // Get the raw indices for an indexed draw
1076 if (type != GL_NONE && elementArrayBuffer)
1077 {
1078 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001079 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001080 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001081 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001082 }
1083
1084 if (!mLineLoopIB)
1085 {
1086 mLineLoopIB = new StreamingIndexBufferInterface(this);
1087 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1088 {
1089 delete mLineLoopIB;
1090 mLineLoopIB = NULL;
1091
1092 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001093 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001094 }
1095 }
1096
1097 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
1098 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1099 {
1100 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001101 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001102 }
1103
1104 void* mappedMemory = NULL;
1105 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1106 if (offset == -1 || mappedMemory == NULL)
1107 {
1108 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001109 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001110 }
1111
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001112 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001113 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001114
1115 switch (type)
1116 {
1117 case GL_NONE: // Non-indexed draw
1118 for (int i = 0; i < count; i++)
1119 {
1120 data[i] = i;
1121 }
1122 data[count] = 0;
1123 break;
1124 case GL_UNSIGNED_BYTE:
1125 for (int i = 0; i < count; i++)
1126 {
1127 data[i] = static_cast<const GLubyte*>(indices)[i];
1128 }
1129 data[count] = static_cast<const GLubyte*>(indices)[0];
1130 break;
1131 case GL_UNSIGNED_SHORT:
1132 for (int i = 0; i < count; i++)
1133 {
1134 data[i] = static_cast<const GLushort*>(indices)[i];
1135 }
1136 data[count] = static_cast<const GLushort*>(indices)[0];
1137 break;
1138 case GL_UNSIGNED_INT:
1139 for (int i = 0; i < count; i++)
1140 {
1141 data[i] = static_cast<const GLuint*>(indices)[i];
1142 }
1143 data[count] = static_cast<const GLuint*>(indices)[0];
1144 break;
1145 default: UNREACHABLE();
1146 }
1147
1148 if (!mLineLoopIB->unmapBuffer())
1149 {
1150 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001151 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001152 }
1153
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001154 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001155 {
1156 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1157
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001158 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001159 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001160 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001161 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001162 }
1163
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001164 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001165}
1166
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001167void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001168{
1169 // Get the raw indices for an indexed draw
1170 if (type != GL_NONE && elementArrayBuffer)
1171 {
1172 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001173 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001174 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001175 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001176 }
1177
1178 if (!mTriangleFanIB)
1179 {
1180 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1181 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1182 {
1183 delete mTriangleFanIB;
1184 mTriangleFanIB = NULL;
1185
1186 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001187 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001188 }
1189 }
1190
1191 const int numTris = count - 2;
1192 const int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
1193 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1194 {
1195 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001196 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001197 }
1198
1199 void* mappedMemory = NULL;
1200 int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory);
1201 if (offset == -1 || mappedMemory == NULL)
1202 {
1203 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001204 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001205 }
1206
1207 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1208 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
1209
1210 switch (type)
1211 {
1212 case GL_NONE: // Non-indexed draw
1213 for (int i = 0; i < numTris; i++)
1214 {
1215 data[i*3 + 0] = 0;
1216 data[i*3 + 1] = i + 1;
1217 data[i*3 + 2] = i + 2;
1218 }
1219 break;
1220 case GL_UNSIGNED_BYTE:
1221 for (int i = 0; i < numTris; i++)
1222 {
1223 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1224 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1225 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1226 }
1227 break;
1228 case GL_UNSIGNED_SHORT:
1229 for (int i = 0; i < numTris; i++)
1230 {
1231 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1232 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1233 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1234 }
1235 break;
1236 case GL_UNSIGNED_INT:
1237 for (int i = 0; i < numTris; i++)
1238 {
1239 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1240 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1241 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1242 }
1243 break;
1244 default: UNREACHABLE();
1245 }
1246
1247 if (!mTriangleFanIB->unmapBuffer())
1248 {
1249 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001250 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001251 }
1252
1253 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1254 {
1255 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1256
1257 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1258 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001259 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001260 mAppliedIBOffset = indexBufferOffset;
1261 }
1262
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001263 if (instances > 0)
1264 {
1265 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1266 }
1267 else
1268 {
1269 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1270 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001271}
1272
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001273void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1274{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001275 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001276 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1277
1278 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001279 {
1280 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1281 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001282
daniel@transgaming.come4991412012-12-20 20:55:34 +00001283 ID3D11VertexShader *vertexShader = NULL;
1284 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001285
daniel@transgaming.come4991412012-12-20 20:55:34 +00001286 ID3D11PixelShader *pixelShader = NULL;
1287 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001288
daniel@transgaming.come4991412012-12-20 20:55:34 +00001289 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1290 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001291
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001292 programBinary->dirtyAllUniforms();
1293
1294 mAppliedProgramBinarySerial = programBinarySerial;
1295 }
1296
1297 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001298 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001299
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001300 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001301 {
1302 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001303 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001304 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1305 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001306 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1307 }
1308 else
1309 {
1310 mDeviceContext->GSSetShader(NULL, NULL, 0);
1311 }
1312
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001313 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001314 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001315}
1316
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001317void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001318{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001319 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1320 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001321
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001322 unsigned int totalRegisterCountVS = 0;
1323 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001324
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001325 bool vertexUniformsDirty = false;
1326 bool pixelUniformsDirty = false;
1327
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001328 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1329 {
1330 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001331
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001332 if (uniform->vsRegisterIndex >= 0)
1333 {
1334 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001335 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001336 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001337
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001338 if (uniform->psRegisterIndex >= 0)
1339 {
1340 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001341 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001342 }
1343 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001344
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001345 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1346 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1347
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001348 float (*mapVS)[4] = NULL;
1349 float (*mapPS)[4] = NULL;
1350
1351 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1352 {
1353 D3D11_MAPPED_SUBRESOURCE map = {0};
1354 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1355 ASSERT(SUCCEEDED(result));
1356 mapVS = (float(*)[4])map.pData;
1357 }
1358
1359 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1360 {
1361 D3D11_MAPPED_SUBRESOURCE map = {0};
1362 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1363 ASSERT(SUCCEEDED(result));
1364 mapPS = (float(*)[4])map.pData;
1365 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001366
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001367 for (gl::UniformArray::iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001368 {
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001369 gl::Uniform *uniform = *uniform_iterator;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001370
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001371 if (uniform->type != GL_SAMPLER_2D && uniform->type != GL_SAMPLER_CUBE)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001372 {
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001373 if (uniform->vsRegisterIndex >= 0 && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001374 {
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001375 memcpy(mapVS + uniform->vsRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4]));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001376 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001377
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001378 if (uniform->psRegisterIndex >= 0 && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001379 {
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001380 memcpy(mapPS + uniform->psRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4]));
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001381 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001382 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001383
1384 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001385 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001386
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001387 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001388 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001389 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001390 }
1391
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001392 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001393 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001394 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001395 }
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001396
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001397 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1398 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001399
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001400 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001401 if (!mDriverConstantBufferVS)
1402 {
1403 D3D11_BUFFER_DESC constantBufferDescription = {0};
1404 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1405 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1406 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1407 constantBufferDescription.CPUAccessFlags = 0;
1408 constantBufferDescription.MiscFlags = 0;
1409 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001410
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001411 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001412 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001413
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001414 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1415 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001416
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001417 if (!mDriverConstantBufferPS)
1418 {
1419 D3D11_BUFFER_DESC constantBufferDescription = {0};
1420 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1421 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1422 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1423 constantBufferDescription.CPUAccessFlags = 0;
1424 constantBufferDescription.MiscFlags = 0;
1425 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001426
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001427 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001428 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001429
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001430 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1431 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001432
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001433 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1434 {
1435 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1436 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1437 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001438
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001439 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1440 {
1441 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1442 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1443 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001444
1445 // needed for the point sprite geometry shader
1446 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001447}
1448
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001449void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001450{
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001451 bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha;
1452 bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1453 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1454 clearParams.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001455
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001456 unsigned int stencilUnmasked = 0x0;
1457 if (frameBuffer->hasStencil())
1458 {
1459 unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat());
1460 stencilUnmasked = (0x1 << stencilSize) - 1;
1461 }
1462 bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1463 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001464
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001465 bool needScissoredClear = mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1466 mCurScissor.x + mCurScissor.width < mRenderTargetDesc.width ||
1467 mCurScissor.y + mCurScissor.height < mRenderTargetDesc.height);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001468
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001469 if (needMaskedColorClear || needMaskedStencilClear || needScissoredClear)
1470 {
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001471 maskedClear(clearParams);
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001472 }
1473 else
1474 {
1475 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1476 {
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001477 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001478 {
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001479 if (frameBuffer->isEnabledColorAttachment(colorAttachment))
1480 {
1481 gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer(colorAttachment);
1482 if (renderbufferObject)
1483 {
1484 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
1485 if (!renderTarget)
1486 {
1487 ERR("render target pointer unexpectedly null.");
1488 return;
1489 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001490
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001491 ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView();
1492 if (!framebufferRTV)
1493 {
1494 ERR("render target view pointer unexpectedly null.");
1495 return;
1496 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001497
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001498 const float clearValues[4] = { clearParams.colorClearValue.red,
1499 clearParams.colorClearValue.green,
1500 clearParams.colorClearValue.blue,
1501 clearParams.colorClearValue.alpha };
1502 mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001503
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001504 framebufferRTV->Release();
1505 }
1506 }
1507 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001508 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001509 if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001510 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001511 gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer();
1512 if (renderbufferObject)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001513 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001514 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil());
1515 if (!renderTarget)
1516 {
1517 ERR("render target pointer unexpectedly null.");
1518 return;
1519 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001520
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001521 ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView();
1522 if (!framebufferDSV)
1523 {
1524 ERR("depth stencil view pointer unexpectedly null.");
1525 return;
1526 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001527
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001528 UINT clearFlags = 0;
1529 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1530 {
1531 clearFlags |= D3D11_CLEAR_DEPTH;
1532 }
1533 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1534 {
1535 clearFlags |= D3D11_CLEAR_STENCIL;
1536 }
1537
shannon.woods@transgaming.combe211b32013-02-28 23:17:16 +00001538 float depthClear = gl::clamp01(clearParams.depthClearValue);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001539 UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF;
1540
1541 mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001542
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001543 framebufferDSV->Release();
1544 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001545 }
1546 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001547}
1548
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001549void Renderer11::maskedClear(const gl::ClearParameters &clearParams)
1550{
1551 HRESULT result;
1552
1553 if (!mClearResourcesInitialized)
1554 {
1555 ASSERT(!mClearVB && !mClearVS && !mClearPS && !mClearScissorRS && !mClearNoScissorRS);
1556
1557 D3D11_BUFFER_DESC vbDesc;
1558 vbDesc.ByteWidth = sizeof(d3d11::PositionDepthColorVertex) * 4;
1559 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
1560 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
1561 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1562 vbDesc.MiscFlags = 0;
1563 vbDesc.StructureByteStride = 0;
1564
1565 result = mDevice->CreateBuffer(&vbDesc, NULL, &mClearVB);
1566 ASSERT(SUCCEEDED(result));
1567 d3d11::SetDebugName(mClearVB, "Renderer11 masked clear vertex buffer");
1568
1569 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
1570 {
1571 { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1572 { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1573 };
1574
1575 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Clear, sizeof(g_VS_Clear), &mClearIL);
1576 ASSERT(SUCCEEDED(result));
1577 d3d11::SetDebugName(mClearIL, "Renderer11 masked clear input layout");
1578
1579 result = mDevice->CreateVertexShader(g_VS_Clear, sizeof(g_VS_Clear), NULL, &mClearVS);
1580 ASSERT(SUCCEEDED(result));
1581 d3d11::SetDebugName(mClearVS, "Renderer11 masked clear vertex shader");
1582
1583 result = mDevice->CreatePixelShader(g_PS_Clear, sizeof(g_PS_Clear), NULL, &mClearPS);
1584 ASSERT(SUCCEEDED(result));
1585 d3d11::SetDebugName(mClearPS, "Renderer11 masked clear pixel shader");
1586
1587 D3D11_RASTERIZER_DESC rsScissorDesc;
1588 rsScissorDesc.FillMode = D3D11_FILL_SOLID;
1589 rsScissorDesc.CullMode = D3D11_CULL_NONE;
1590 rsScissorDesc.FrontCounterClockwise = FALSE;
1591 rsScissorDesc.DepthBias = 0;
1592 rsScissorDesc.DepthBiasClamp = 0.0f;
1593 rsScissorDesc.SlopeScaledDepthBias = 0.0f;
1594 rsScissorDesc.DepthClipEnable = FALSE;
1595 rsScissorDesc.ScissorEnable = TRUE;
1596 rsScissorDesc.MultisampleEnable = FALSE;
1597 rsScissorDesc.AntialiasedLineEnable = FALSE;
1598
1599 result = mDevice->CreateRasterizerState(&rsScissorDesc, &mClearScissorRS);
1600 ASSERT(SUCCEEDED(result));
1601 d3d11::SetDebugName(mClearScissorRS, "Renderer11 masked clear scissor rasterizer state");
1602
1603 D3D11_RASTERIZER_DESC rsNoScissorDesc;
1604 rsNoScissorDesc.FillMode = D3D11_FILL_SOLID;
1605 rsNoScissorDesc.CullMode = D3D11_CULL_NONE;
1606 rsNoScissorDesc.FrontCounterClockwise = FALSE;
1607 rsNoScissorDesc.DepthBias = 0;
1608 rsNoScissorDesc.DepthBiasClamp = 0.0f;
1609 rsNoScissorDesc.SlopeScaledDepthBias = 0.0f;
1610 rsNoScissorDesc.DepthClipEnable = FALSE;
1611 rsNoScissorDesc.ScissorEnable = FALSE;
1612 rsNoScissorDesc.MultisampleEnable = FALSE;
1613 rsNoScissorDesc.AntialiasedLineEnable = FALSE;
1614
1615 result = mDevice->CreateRasterizerState(&rsNoScissorDesc, &mClearNoScissorRS);
1616 ASSERT(SUCCEEDED(result));
1617 d3d11::SetDebugName(mClearNoScissorRS, "Renderer11 masked clear no scissor rasterizer state");
1618
1619 mClearResourcesInitialized = true;
1620 }
1621
1622 // Prepare the depth stencil state to write depth values if the depth should be cleared
1623 // and stencil values if the stencil should be cleared
1624 gl::DepthStencilState glDSState;
1625 glDSState.depthTest = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1626 glDSState.depthFunc = GL_ALWAYS;
1627 glDSState.depthMask = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1628 glDSState.stencilTest = (clearParams.mask & GL_STENCIL_BUFFER_BIT) != 0;
1629 glDSState.stencilFunc = GL_ALWAYS;
1630 glDSState.stencilMask = 0;
1631 glDSState.stencilFail = GL_REPLACE;
1632 glDSState.stencilPassDepthFail = GL_REPLACE;
1633 glDSState.stencilPassDepthPass = GL_REPLACE;
1634 glDSState.stencilWritemask = clearParams.stencilWriteMask;
1635 glDSState.stencilBackFunc = GL_ALWAYS;
1636 glDSState.stencilBackMask = 0;
1637 glDSState.stencilBackFail = GL_REPLACE;
1638 glDSState.stencilBackPassDepthFail = GL_REPLACE;
1639 glDSState.stencilBackPassDepthPass = GL_REPLACE;
1640 glDSState.stencilBackWritemask = clearParams.stencilWriteMask;
1641
1642 int stencilClear = clearParams.stencilClearValue & 0x000000FF;
1643
1644 ID3D11DepthStencilState *dsState = mStateCache.getDepthStencilState(glDSState);
1645
1646 // Prepare the blend state to use a write mask if the color buffer should be cleared
1647 gl::BlendState glBlendState;
1648 glBlendState.blend = false;
1649 glBlendState.sourceBlendRGB = GL_ONE;
1650 glBlendState.destBlendRGB = GL_ZERO;
1651 glBlendState.sourceBlendAlpha = GL_ONE;
1652 glBlendState.destBlendAlpha = GL_ZERO;
1653 glBlendState.blendEquationRGB = GL_FUNC_ADD;
1654 glBlendState.blendEquationAlpha = GL_FUNC_ADD;
1655 glBlendState.colorMaskRed = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskRed : false;
1656 glBlendState.colorMaskGreen = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskGreen : false;
1657 glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false;
shannon.woods@transgaming.com50ea9932013-02-28 23:13:40 +00001658 glBlendState.colorMaskAlpha = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskAlpha : false;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001659 glBlendState.sampleAlphaToCoverage = false;
1660 glBlendState.dither = false;
1661
1662 static const float blendFactors[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
1663 static const UINT sampleMask = 0xFFFFFFFF;
1664
1665 ID3D11BlendState *blendState = mStateCache.getBlendState(glBlendState);
1666
1667 // Set the vertices
1668 D3D11_MAPPED_SUBRESOURCE mappedResource;
1669 result = mDeviceContext->Map(mClearVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
1670 if (FAILED(result))
1671 {
1672 ERR("Failed to map masked clear vertex buffer, HRESULT: 0x%X.", result);
1673 return;
1674 }
1675
1676 d3d11::PositionDepthColorVertex *vertices = reinterpret_cast<d3d11::PositionDepthColorVertex*>(mappedResource.pData);
1677
1678 float depthClear = gl::clamp01(clearParams.depthClearValue);
1679 d3d11::SetPositionDepthColorVertex(&vertices[0], -1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1680 d3d11::SetPositionDepthColorVertex(&vertices[1], -1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1681 d3d11::SetPositionDepthColorVertex(&vertices[2], 1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1682 d3d11::SetPositionDepthColorVertex(&vertices[3], 1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1683
1684 mDeviceContext->Unmap(mClearVB, 0);
1685
1686 // Apply state
1687 mDeviceContext->OMSetBlendState(blendState, blendFactors, sampleMask);
1688 mDeviceContext->OMSetDepthStencilState(dsState, stencilClear);
1689 mDeviceContext->RSSetState(mScissorEnabled ? mClearScissorRS : mClearNoScissorRS);
1690
1691 // Apply shaders
1692 mDeviceContext->IASetInputLayout(mClearIL);
1693 mDeviceContext->VSSetShader(mClearVS, NULL, 0);
1694 mDeviceContext->PSSetShader(mClearPS, NULL, 0);
shannon.woods@transgaming.com2c53e472013-02-28 23:13:56 +00001695 mDeviceContext->GSSetShader(NULL, NULL, 0);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001696
1697 // Apply vertex buffer
1698 static UINT stride = sizeof(d3d11::PositionDepthColorVertex);
1699 static UINT startIdx = 0;
1700 mDeviceContext->IASetVertexBuffers(0, 1, &mClearVB, &stride, &startIdx);
1701 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
1702
1703 // Draw the clear quad
1704 mDeviceContext->Draw(4, 0);
1705
1706 // Clean up
1707 markAllStateDirty();
1708}
1709
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001710void Renderer11::markAllStateDirty()
1711{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001712 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1713 {
1714 mAppliedRenderTargetSerials[rtIndex] = 0;
1715 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001716 mAppliedDepthbufferSerial = 0;
1717 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001718 mDepthStencilInitialized = false;
1719 mRenderTargetDescInitialized = false;
1720
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001721 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001722 {
1723 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001724 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001725 }
1726 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1727 {
1728 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001729 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001730 }
1731
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001732 mForceSetBlendState = true;
1733 mForceSetRasterState = true;
1734 mForceSetDepthStencilState = true;
1735 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001736 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001737
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001738 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001739 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001740 mAppliedIBOffset = 0;
1741
daniel@transgaming.come4991412012-12-20 20:55:34 +00001742 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001743 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1744 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001745}
1746
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001747void Renderer11::releaseDeviceResources()
1748{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001749 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001750 mInputLayoutCache.clear();
1751
1752 delete mVertexDataManager;
1753 mVertexDataManager = NULL;
1754
1755 delete mIndexDataManager;
1756 mIndexDataManager = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001757
1758 delete mLineLoopIB;
1759 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001760
1761 delete mTriangleFanIB;
1762 mTriangleFanIB = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001763
1764 if (mCopyVB)
1765 {
1766 mCopyVB->Release();
1767 mCopyVB = NULL;
1768 }
1769
1770 if (mCopySampler)
1771 {
1772 mCopySampler->Release();
1773 mCopySampler = NULL;
1774 }
1775
1776 if (mCopyIL)
1777 {
1778 mCopyIL->Release();
1779 mCopyIL = NULL;
1780 }
1781
1782 if (mCopyVS)
1783 {
1784 mCopyVS->Release();
1785 mCopyVS = NULL;
1786 }
1787
1788 if (mCopyRGBAPS)
1789 {
1790 mCopyRGBAPS->Release();
1791 mCopyRGBAPS = NULL;
1792 }
1793
1794 if (mCopyRGBPS)
1795 {
1796 mCopyRGBPS->Release();
1797 mCopyRGBPS = NULL;
1798 }
1799
1800 if (mCopyLumPS)
1801 {
1802 mCopyLumPS->Release();
1803 mCopyLumPS = NULL;
1804 }
1805
1806 if (mCopyLumAlphaPS)
1807 {
1808 mCopyLumAlphaPS->Release();
1809 mCopyLumAlphaPS = NULL;
1810 }
1811
1812 mCopyResourcesInitialized = false;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001813
1814 if (mClearVB)
1815 {
1816 mClearVB->Release();
1817 mClearVB = NULL;
1818 }
1819
1820 if (mClearIL)
1821 {
1822 mClearIL->Release();
1823 mClearIL = NULL;
1824 }
1825
1826 if (mClearVS)
1827 {
1828 mClearVS->Release();
1829 mClearVS = NULL;
1830 }
1831
1832 if (mClearPS)
1833 {
1834 mClearPS->Release();
1835 mClearPS = NULL;
1836 }
1837
1838 if (mClearScissorRS)
1839 {
1840 mClearScissorRS->Release();
1841 mClearScissorRS = NULL;
1842 }
1843
1844 if (mClearNoScissorRS)
1845 {
1846 mClearNoScissorRS->Release();
1847 mClearNoScissorRS = NULL;
1848 }
1849
1850 mClearResourcesInitialized = false;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001851
1852 if (mDriverConstantBufferVS)
1853 {
1854 mDriverConstantBufferVS->Release();
1855 mDriverConstantBufferVS = NULL;
1856 }
1857
1858 if (mDriverConstantBufferPS)
1859 {
1860 mDriverConstantBufferPS->Release();
1861 mDriverConstantBufferPS = NULL;
1862 }
daniel@transgaming.combdf787f2013-02-01 03:20:36 +00001863
1864 if (mSyncQuery)
1865 {
1866 mSyncQuery->Release();
1867 mSyncQuery = NULL;
1868 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001869}
1870
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001871void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001872{
1873 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001874 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001875}
1876
1877bool Renderer11::isDeviceLost()
1878{
1879 return mDeviceLost;
1880}
1881
1882// set notify to true to broadcast a message to all contexts of the device loss
1883bool Renderer11::testDeviceLost(bool notify)
1884{
1885 bool isLost = false;
1886
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001887 // GetRemovedReason is used to test if the device is removed
1888 HRESULT result = mDevice->GetDeviceRemovedReason();
1889 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001890
1891 if (isLost)
1892 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001893 // Log error if this is a new device lost event
1894 if (mDeviceLost == false)
1895 {
1896 ERR("The D3D11 device was removed: 0x%08X", result);
1897 }
1898
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001899 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001900 // we'll probably get this done again by notifyDeviceLost
1901 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001902 // Note that we don't want to clear the device loss status here
1903 // -- this needs to be done by resetDevice
1904 mDeviceLost = true;
1905 if (notify)
1906 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001907 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001908 }
1909 }
1910
1911 return isLost;
1912}
1913
1914bool Renderer11::testDeviceResettable()
1915{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001916 // determine if the device is resettable by creating a dummy device
1917 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001918
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001919 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001920 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001921 return false;
1922 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001923
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001924 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001925 {
1926 D3D_FEATURE_LEVEL_11_0,
1927 D3D_FEATURE_LEVEL_10_1,
1928 D3D_FEATURE_LEVEL_10_0,
1929 };
1930
1931 ID3D11Device* dummyDevice;
1932 D3D_FEATURE_LEVEL dummyFeatureLevel;
1933 ID3D11DeviceContext* dummyContext;
1934
1935 HRESULT result = D3D11CreateDevice(NULL,
1936 D3D_DRIVER_TYPE_HARDWARE,
1937 NULL,
1938 #if defined(_DEBUG)
1939 D3D11_CREATE_DEVICE_DEBUG,
1940 #else
1941 0,
1942 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001943 featureLevels,
1944 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001945 D3D11_SDK_VERSION,
1946 &dummyDevice,
1947 &dummyFeatureLevel,
1948 &dummyContext);
1949
1950 if (!mDevice || FAILED(result))
1951 {
1952 return false;
1953 }
1954
1955 dummyContext->Release();
1956 dummyDevice->Release();
1957
1958 return true;
1959}
1960
1961void Renderer11::release()
1962{
1963 releaseDeviceResources();
1964
1965 if (mDxgiFactory)
1966 {
1967 mDxgiFactory->Release();
1968 mDxgiFactory = NULL;
1969 }
1970
1971 if (mDxgiAdapter)
1972 {
1973 mDxgiAdapter->Release();
1974 mDxgiAdapter = NULL;
1975 }
1976
1977 if (mDeviceContext)
1978 {
1979 mDeviceContext->ClearState();
1980 mDeviceContext->Flush();
1981 mDeviceContext->Release();
1982 mDeviceContext = NULL;
1983 }
1984
1985 if (mDevice)
1986 {
1987 mDevice->Release();
1988 mDevice = NULL;
1989 }
1990
1991 if (mD3d11Module)
1992 {
1993 FreeLibrary(mD3d11Module);
1994 mD3d11Module = NULL;
1995 }
1996
1997 if (mDxgiModule)
1998 {
1999 FreeLibrary(mDxgiModule);
2000 mDxgiModule = NULL;
2001 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002002}
2003
2004bool Renderer11::resetDevice()
2005{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00002006 // recreate everything
2007 release();
2008 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002009
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00002010 if (result != EGL_SUCCESS)
2011 {
2012 ERR("Could not reinitialize D3D11 device: %08X", result);
2013 return false;
2014 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002015
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002016 mDeviceLost = false;
2017
2018 return true;
2019}
2020
2021DWORD Renderer11::getAdapterVendor() const
2022{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00002023 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002024}
2025
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002026std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002027{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002028 std::ostringstream rendererString;
2029
2030 rendererString << mDescription;
2031 rendererString << " Direct3D11";
2032
2033 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
2034 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
2035
2036 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002037}
2038
2039GUID Renderer11::getAdapterIdentifier() const
2040{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00002041 // Use the adapter LUID as our adapter ID
2042 // This number is local to a machine is only guaranteed to be unique between restarts
2043 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
2044 GUID adapterId = {0};
2045 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
2046 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002047}
2048
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002049bool Renderer11::getBGRATextureSupport() const
2050{
2051 return mBGRATextureSupport;
2052}
2053
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002054bool Renderer11::getDXT1TextureSupport()
2055{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002056 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002057}
2058
2059bool Renderer11::getDXT3TextureSupport()
2060{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002061 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002062}
2063
2064bool Renderer11::getDXT5TextureSupport()
2065{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002066 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002067}
2068
2069bool Renderer11::getDepthTextureSupport() const
2070{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00002071 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002072}
2073
2074bool Renderer11::getFloat32TextureSupport(bool *filtering, bool *renderable)
2075{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00002076 *renderable = mFloat32RenderSupport;
2077 *filtering = mFloat32FilterSupport;
2078 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002079}
2080
2081bool Renderer11::getFloat16TextureSupport(bool *filtering, bool *renderable)
2082{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00002083 *renderable = mFloat16RenderSupport;
2084 *filtering = mFloat16FilterSupport;
2085 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002086}
2087
2088bool Renderer11::getLuminanceTextureSupport()
2089{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002090 return false;
2091}
2092
2093bool Renderer11::getLuminanceAlphaTextureSupport()
2094{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002095 return false;
2096}
2097
2098bool Renderer11::getTextureFilterAnisotropySupport() const
2099{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002100 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002101}
2102
2103float Renderer11::getTextureMaxAnisotropy() const
2104{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002105 switch (mFeatureLevel)
2106 {
2107 case D3D_FEATURE_LEVEL_11_0:
2108 return D3D11_MAX_MAXANISOTROPY;
2109 case D3D_FEATURE_LEVEL_10_1:
2110 case D3D_FEATURE_LEVEL_10_0:
2111 return D3D10_MAX_MAXANISOTROPY;
2112 default: UNREACHABLE();
2113 return 0;
2114 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002115}
2116
2117bool Renderer11::getEventQuerySupport()
2118{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002119 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002120}
2121
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002122Range Renderer11::getViewportBounds() const
2123{
2124 switch (mFeatureLevel)
2125 {
2126 case D3D_FEATURE_LEVEL_11_0:
2127 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
2128 case D3D_FEATURE_LEVEL_10_1:
2129 case D3D_FEATURE_LEVEL_10_0:
2130 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
2131 default: UNREACHABLE();
2132 return Range(0, 0);
2133 }
2134}
2135
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002136unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002137{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002138 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2139 switch (mFeatureLevel)
2140 {
2141 case D3D_FEATURE_LEVEL_11_0:
2142 case D3D_FEATURE_LEVEL_10_1:
2143 case D3D_FEATURE_LEVEL_10_0:
2144 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
2145 default: UNREACHABLE();
2146 return 0;
2147 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002148}
2149
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002150unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
2151{
2152 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2153}
2154
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002155unsigned int Renderer11::getReservedVertexUniformVectors() const
2156{
2157 return 0; // Driver uniforms are stored in a separate constant buffer
2158}
2159
2160unsigned int Renderer11::getReservedFragmentUniformVectors() const
2161{
2162 return 0; // Driver uniforms are stored in a separate constant buffer
2163}
2164
2165unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002166{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002167 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2168 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2169 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002170}
2171
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002172unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002173{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002174 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2175 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2176 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002177}
2178
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002179unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002180{
2181 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
2182 switch (mFeatureLevel)
2183 {
2184 case D3D_FEATURE_LEVEL_11_0:
2185 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2186 case D3D_FEATURE_LEVEL_10_1:
2187 case D3D_FEATURE_LEVEL_10_0:
2188 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2189 default: UNREACHABLE();
2190 return 0;
2191 }
2192}
2193
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002194bool Renderer11::getNonPower2TextureSupport() const
2195{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002196 switch (mFeatureLevel)
2197 {
2198 case D3D_FEATURE_LEVEL_11_0:
2199 case D3D_FEATURE_LEVEL_10_1:
2200 case D3D_FEATURE_LEVEL_10_0:
2201 return true;
2202 default: UNREACHABLE();
2203 return false;
2204 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002205}
2206
2207bool Renderer11::getOcclusionQuerySupport() const
2208{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002209 switch (mFeatureLevel)
2210 {
2211 case D3D_FEATURE_LEVEL_11_0:
2212 case D3D_FEATURE_LEVEL_10_1:
2213 case D3D_FEATURE_LEVEL_10_0:
2214 return true;
2215 default: UNREACHABLE();
2216 return false;
2217 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002218}
2219
2220bool Renderer11::getInstancingSupport() const
2221{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002222 switch (mFeatureLevel)
2223 {
2224 case D3D_FEATURE_LEVEL_11_0:
2225 case D3D_FEATURE_LEVEL_10_1:
2226 case D3D_FEATURE_LEVEL_10_0:
2227 return true;
2228 default: UNREACHABLE();
2229 return false;
2230 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002231}
2232
2233bool Renderer11::getShareHandleSupport() const
2234{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002235 // We only currently support share handles with BGRA surfaces, because
2236 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002237 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002238 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002239}
2240
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002241bool Renderer11::getDerivativeInstructionSupport() const
2242{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002243 switch (mFeatureLevel)
2244 {
2245 case D3D_FEATURE_LEVEL_11_0:
2246 case D3D_FEATURE_LEVEL_10_1:
2247 case D3D_FEATURE_LEVEL_10_0:
2248 return true;
2249 default: UNREACHABLE();
2250 return false;
2251 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002252}
2253
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002254bool Renderer11::getPostSubBufferSupport() const
2255{
2256 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2257 return false;
2258}
2259
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002260int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002261{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002262 switch (mFeatureLevel)
2263 {
2264 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002265 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002266 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2267 default: UNREACHABLE(); return 0;
2268 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002269}
2270
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002271int Renderer11::getMinorShaderModel() const
2272{
2273 switch (mFeatureLevel)
2274 {
2275 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2276 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2277 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2278 default: UNREACHABLE(); return 0;
2279 }
2280}
2281
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002282float Renderer11::getMaxPointSize() const
2283{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002284 // choose a reasonable maximum. we enforce this in the shader.
2285 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2286 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002287}
2288
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002289int Renderer11::getMaxViewportDimension() const
2290{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002291 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2292 // In our case return the maximum texture size, which is the maximum render buffer size.
2293 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2294 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2295
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002296 switch (mFeatureLevel)
2297 {
2298 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002299 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002300 case D3D_FEATURE_LEVEL_10_1:
2301 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002302 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002303 default: UNREACHABLE();
2304 return 0;
2305 }
2306}
2307
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002308int Renderer11::getMaxTextureWidth() const
2309{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002310 switch (mFeatureLevel)
2311 {
2312 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2313 case D3D_FEATURE_LEVEL_10_1:
2314 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2315 default: UNREACHABLE(); return 0;
2316 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002317}
2318
2319int Renderer11::getMaxTextureHeight() const
2320{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002321 switch (mFeatureLevel)
2322 {
2323 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2324 case D3D_FEATURE_LEVEL_10_1:
2325 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2326 default: UNREACHABLE(); return 0;
2327 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002328}
2329
2330bool Renderer11::get32BitIndexSupport() const
2331{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002332 switch (mFeatureLevel)
2333 {
2334 case D3D_FEATURE_LEVEL_11_0:
2335 case D3D_FEATURE_LEVEL_10_1:
2336 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2337 default: UNREACHABLE(); return false;
2338 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002339}
2340
2341int Renderer11::getMinSwapInterval() const
2342{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002343 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002344}
2345
2346int Renderer11::getMaxSwapInterval() const
2347{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002348 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002349}
2350
2351int Renderer11::getMaxSupportedSamples() const
2352{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002353 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002354}
2355
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002356int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2357{
2358 if (requested == 0)
2359 {
2360 return 0;
2361 }
2362
2363 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2364 if (iter != mMultisampleSupportMap.end())
2365 {
2366 const MultisampleSupportInfo& info = iter->second;
2367 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2368 {
2369 if (info.qualityLevels[i] > 0)
2370 {
2371 return i + 1;
2372 }
2373 }
2374 }
2375
2376 return -1;
2377}
2378
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002379unsigned int Renderer11::getMaxRenderTargets() const
2380{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002381 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2382 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2383
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002384 switch (mFeatureLevel)
2385 {
2386 case D3D_FEATURE_LEVEL_11_0:
2387 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2388 case D3D_FEATURE_LEVEL_10_1:
2389 case D3D_FEATURE_LEVEL_10_0:
2390 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2391 default:
2392 UNREACHABLE();
2393 return 1;
2394 }
2395}
2396
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002397bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002398{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002399 if (source && dest)
2400 {
2401 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2402 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2403
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002404 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002405 return true;
2406 }
2407
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002408 return false;
2409}
2410
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002411bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002412{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002413 if (source && dest)
2414 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002415 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2416 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002417
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002418 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002419 return true;
2420 }
2421
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002422 return false;
2423}
2424
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002425bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002426 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002427{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002428 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002429 if (!colorbuffer)
2430 {
2431 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002432 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002433 }
2434
2435 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2436 if (!sourceRenderTarget)
2437 {
2438 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002439 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002440 }
2441
2442 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2443 if (!source)
2444 {
2445 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002446 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002447 }
2448
2449 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2450 if (!storage11)
2451 {
2452 source->Release();
2453 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002454 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002455 }
2456
2457 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2458 if (!destRenderTarget)
2459 {
2460 source->Release();
2461 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002462 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002463 }
2464
2465 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2466 if (!dest)
2467 {
2468 source->Release();
2469 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002470 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002471 }
2472
2473 gl::Rectangle destRect;
2474 destRect.x = xoffset;
2475 destRect.y = yoffset;
2476 destRect.width = sourceRect.width;
2477 destRect.height = sourceRect.height;
2478
2479 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
2480 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
2481
2482 source->Release();
2483 dest->Release();
2484
2485 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002486}
2487
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002488bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002489 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002490{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002491 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002492 if (!colorbuffer)
2493 {
2494 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002495 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002496 }
2497
2498 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2499 if (!sourceRenderTarget)
2500 {
2501 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002502 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002503 }
2504
2505 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2506 if (!source)
2507 {
2508 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002509 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002510 }
2511
2512 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2513 if (!storage11)
2514 {
2515 source->Release();
2516 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002517 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002518 }
2519
2520 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(target, level));
2521 if (!destRenderTarget)
2522 {
2523 source->Release();
2524 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002525 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002526 }
2527
2528 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2529 if (!dest)
2530 {
2531 source->Release();
2532 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002533 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002534 }
2535
2536 gl::Rectangle destRect;
2537 destRect.x = xoffset;
2538 destRect.y = yoffset;
2539 destRect.width = sourceRect.width;
2540 destRect.height = sourceRect.height;
2541
2542 bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(),
2543 dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat);
2544
2545 source->Release();
2546 dest->Release();
2547
2548 return ret;
2549}
2550
2551bool Renderer11::copyTexture(ID3D11ShaderResourceView *source, const gl::Rectangle &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight,
2552 ID3D11RenderTargetView *dest, const gl::Rectangle &destArea, unsigned int destWidth, unsigned int destHeight, GLenum destFormat)
2553{
2554 HRESULT result;
2555
2556 if (!mCopyResourcesInitialized)
2557 {
2558 ASSERT(!mCopyVB && !mCopySampler && !mCopyIL && !mCopyVS && !mCopyRGBAPS && !mCopyRGBPS && !mCopyLumPS && !mCopyLumAlphaPS);
2559
2560 D3D11_BUFFER_DESC vbDesc;
2561 vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4;
2562 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
2563 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
2564 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
2565 vbDesc.MiscFlags = 0;
2566 vbDesc.StructureByteStride = 0;
2567
2568 result = mDevice->CreateBuffer(&vbDesc, NULL, &mCopyVB);
2569 ASSERT(SUCCEEDED(result));
2570 d3d11::SetDebugName(mCopyVB, "Renderer11 copy texture vertex buffer");
2571
2572 D3D11_SAMPLER_DESC samplerDesc;
2573 samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
2574 samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
2575 samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
2576 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
2577 samplerDesc.MipLODBias = 0.0f;
2578 samplerDesc.MaxAnisotropy = 0;
2579 samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
2580 samplerDesc.BorderColor[0] = 0.0f;
2581 samplerDesc.BorderColor[1] = 0.0f;
2582 samplerDesc.BorderColor[2] = 0.0f;
2583 samplerDesc.BorderColor[3] = 0.0f;
2584 samplerDesc.MinLOD = 0.0f;
2585 samplerDesc.MaxLOD = 0.0f;
2586
2587 result = mDevice->CreateSamplerState(&samplerDesc, &mCopySampler);
2588 ASSERT(SUCCEEDED(result));
2589 d3d11::SetDebugName(mCopySampler, "Renderer11 copy sampler");
2590
2591 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
2592 {
2593 { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2594 { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2595 };
2596
2597 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mCopyIL);
2598 ASSERT(SUCCEEDED(result));
2599 d3d11::SetDebugName(mCopyIL, "Renderer11 copy texture input layout");
2600
2601 result = mDevice->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mCopyVS);
2602 ASSERT(SUCCEEDED(result));
2603 d3d11::SetDebugName(mCopyVS, "Renderer11 copy texture vertex shader");
2604
2605 result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mCopyRGBAPS);
2606 ASSERT(SUCCEEDED(result));
2607 d3d11::SetDebugName(mCopyRGBAPS, "Renderer11 copy texture RGBA pixel shader");
2608
2609 result = mDevice->CreatePixelShader(g_PS_PassthroughRGB, sizeof(g_PS_PassthroughRGB), NULL, &mCopyRGBPS);
2610 ASSERT(SUCCEEDED(result));
2611 d3d11::SetDebugName(mCopyRGBPS, "Renderer11 copy texture RGB pixel shader");
2612
2613 result = mDevice->CreatePixelShader(g_PS_PassthroughLum, sizeof(g_PS_PassthroughLum), NULL, &mCopyLumPS);
2614 ASSERT(SUCCEEDED(result));
2615 d3d11::SetDebugName(mCopyLumPS, "Renderer11 copy texture luminance pixel shader");
2616
2617 result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha, sizeof(g_PS_PassthroughLumAlpha), NULL, &mCopyLumAlphaPS);
2618 ASSERT(SUCCEEDED(result));
2619 d3d11::SetDebugName(mCopyLumAlphaPS, "Renderer11 copy texture luminance alpha pixel shader");
2620
2621 mCopyResourcesInitialized = true;
2622 }
2623
2624 // Verify the source and destination area sizes
2625 if (sourceArea.x < 0 || sourceArea.x + sourceArea.width > static_cast<int>(sourceWidth) ||
2626 sourceArea.y < 0 || sourceArea.y + sourceArea.height > static_cast<int>(sourceHeight) ||
2627 destArea.x < 0 || destArea.x + destArea.width > static_cast<int>(destWidth) ||
2628 destArea.y < 0 || destArea.y + destArea.height > static_cast<int>(destHeight))
2629 {
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002630 return gl::error(GL_INVALID_VALUE, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002631 }
2632
2633 // Set vertices
2634 D3D11_MAPPED_SUBRESOURCE mappedResource;
2635 result = mDeviceContext->Map(mCopyVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
2636 if (FAILED(result))
2637 {
2638 ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002639 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002640 }
2641
2642 d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData);
2643
2644 // Create a quad in homogeneous coordinates
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00002645 float x1 = (destArea.x / float(destWidth)) * 2.0f - 1.0f;
shannon.woods@transgaming.comcfdfd462013-02-28 23:13:01 +00002646 float y1 = ((destHeight - destArea.y - destArea.height) / float(destHeight)) * 2.0f - 1.0f;
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00002647 float x2 = ((destArea.x + destArea.width) / float(destWidth)) * 2.0f - 1.0f;
shannon.woods@transgaming.comcfdfd462013-02-28 23:13:01 +00002648 float y2 = ((destHeight - destArea.y) / float(destHeight)) * 2.0f - 1.0f;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002649
2650 float u1 = sourceArea.x / float(sourceWidth);
2651 float v1 = sourceArea.y / float(sourceHeight);
2652 float u2 = (sourceArea.x + sourceArea.width) / float(sourceWidth);
2653 float v2 = (sourceArea.y + sourceArea.height) / float(sourceHeight);
2654
2655 d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2);
2656 d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1);
2657 d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2);
2658 d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1);
2659
2660 mDeviceContext->Unmap(mCopyVB, 0);
2661
2662 static UINT stride = sizeof(d3d11::PositionTexCoordVertex);
2663 static UINT startIdx = 0;
2664 mDeviceContext->IASetVertexBuffers(0, 1, &mCopyVB, &stride, &startIdx);
2665
2666 // Apply state
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00002667 mDeviceContext->OMSetBlendState(NULL, NULL, 0xFFFFFFF);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002668 mDeviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF);
2669 mDeviceContext->RSSetState(NULL);
2670
2671 // Apply shaders
2672 mDeviceContext->IASetInputLayout(mCopyIL);
2673 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
2674 mDeviceContext->VSSetShader(mCopyVS, NULL, 0);
2675
2676 ID3D11PixelShader *ps = NULL;
2677 switch(destFormat)
2678 {
2679 case GL_RGBA: ps = mCopyRGBAPS; break;
2680 case GL_RGB: ps = mCopyRGBPS; break;
2681 case GL_ALPHA: ps = mCopyRGBAPS; break;
2682 case GL_BGRA_EXT: ps = mCopyRGBAPS; break;
2683 case GL_LUMINANCE: ps = mCopyLumPS; break;
2684 case GL_LUMINANCE_ALPHA: ps = mCopyLumAlphaPS; break;
2685 default: UNREACHABLE(); ps = NULL; break;
2686 }
2687
2688 mDeviceContext->PSSetShader(ps, NULL, 0);
shannon.woods@transgaming.com28e7ba02013-02-28 23:09:28 +00002689 mDeviceContext->GSSetShader(NULL, NULL, 0);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002690
2691 // Unset the currently bound shader resource to avoid conflicts
2692 static ID3D11ShaderResourceView *const nullSRV = NULL;
2693 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
2694
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002695 // Apply render target
2696 setOneTimeRenderTarget(dest);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002697
2698 // Set the viewport
2699 D3D11_VIEWPORT viewport;
2700 viewport.TopLeftX = 0;
2701 viewport.TopLeftY = 0;
2702 viewport.Width = destWidth;
2703 viewport.Height = destHeight;
2704 viewport.MinDepth = 0.0f;
2705 viewport.MaxDepth = 1.0f;
2706 mDeviceContext->RSSetViewports(1, &viewport);
2707
2708 // Apply textures
2709 mDeviceContext->PSSetShaderResources(0, 1, &source);
2710 mDeviceContext->PSSetSamplers(0, 1, &mCopySampler);
2711
2712 // Draw the quad
2713 mDeviceContext->Draw(4, 0);
2714
2715 // Unbind textures and render targets and vertex buffer
2716 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
2717
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002718 unapplyRenderTargets();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002719
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002720 UINT zero = 0;
2721 ID3D11Buffer *const nullBuffer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002722 mDeviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
2723
2724 markAllStateDirty();
2725
2726 return true;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002727}
2728
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00002729void Renderer11::unapplyRenderTargets()
2730{
2731 setOneTimeRenderTarget(NULL);
2732}
2733
2734void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
2735{
2736 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
2737
2738 rtvArray[0] = renderTargetView;
2739
2740 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
2741
2742 // Do not preserve the serial for this one-time-use render target
2743 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
2744 {
2745 mAppliedRenderTargetSerials[rtIndex] = 0;
2746 }
2747}
2748
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002749RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
2750{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002751 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002752 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002753
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002754 if (depth)
2755 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002756 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002757 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
2758 swapChain11->getDepthStencilTexture(), NULL,
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002759 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002760 }
2761 else
2762 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00002763 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002764 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00002765 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00002766 swapChain11->getRenderTargetShaderResource(),
2767 swapChain11->getWidth(), swapChain11->getHeight());
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00002768 }
2769 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002770}
2771
2772RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
2773{
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00002774 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples, depth);
2775 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00002776}
2777
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002778ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00002779{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002780 ShaderExecutable11 *executable = NULL;
2781
2782 switch (type)
2783 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002784 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002785 {
2786 ID3D11VertexShader *vshader = NULL;
2787 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
2788 ASSERT(SUCCEEDED(result));
2789
2790 if (vshader)
2791 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002792 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002793 }
2794 }
2795 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002796 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002797 {
2798 ID3D11PixelShader *pshader = NULL;
2799 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
2800 ASSERT(SUCCEEDED(result));
2801
2802 if (pshader)
2803 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00002804 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002805 }
2806 }
2807 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002808 case rx::SHADER_GEOMETRY:
2809 {
2810 ID3D11GeometryShader *gshader = NULL;
2811 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
2812 ASSERT(SUCCEEDED(result));
2813
2814 if (gshader)
2815 {
2816 executable = new ShaderExecutable11(function, length, gshader);
2817 }
2818 }
2819 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00002820 default:
2821 UNREACHABLE();
2822 break;
2823 }
2824
2825 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00002826}
2827
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002828ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00002829{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002830 const char *profile = NULL;
2831
2832 switch (type)
2833 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002834 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002835 profile = "vs_4_0";
2836 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00002837 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002838 profile = "ps_4_0";
2839 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002840 case rx::SHADER_GEOMETRY:
2841 profile = "gs_4_0";
2842 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002843 default:
2844 UNREACHABLE();
2845 return NULL;
2846 }
2847
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00002848 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002849 if (!binary)
2850 return NULL;
2851
daniel@transgaming.com2275f912012-12-20 21:13:22 +00002852 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00002853 binary->Release();
2854
2855 return executable;
2856}
2857
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002858VertexBuffer *Renderer11::createVertexBuffer()
2859{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00002860 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00002861}
2862
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002863IndexBuffer *Renderer11::createIndexBuffer()
2864{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00002865 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002866}
2867
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00002868BufferStorage *Renderer11::createBufferStorage()
2869{
2870 return new BufferStorage11(this);
2871}
2872
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002873QueryImpl *Renderer11::createQuery(GLenum type)
2874{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002875 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00002876}
2877
2878FenceImpl *Renderer11::createFence()
2879{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002880 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00002881}
2882
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002883bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002884{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002885 ASSERT(colorbuffer != NULL);
2886
2887 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2888 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002889 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002890 *subresourceIndex = renderTarget->getSubresourceIndex();
2891
2892 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
2893 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002894 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002895 ID3D11Resource *textureResource = NULL;
2896 colorBufferRTV->GetResource(&textureResource);
2897 colorBufferRTV->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002898
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002899 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002900 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002901 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
2902 textureResource->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002903
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002904 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002905 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002906 return true;
2907 }
2908 else
2909 {
2910 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
2911 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00002912 }
2913 }
2914 }
2915 }
2916
2917 return false;
2918}
2919
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00002920bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002921 bool blitRenderTarget, bool blitDepthStencil)
2922{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002923 // TODO: mrt blit support
2924 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002925 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002926 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
2927 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(0);
2928
2929 if (!readBuffer)
2930 {
2931 ERR("Failed to retrieve the read buffer from the read framebuffer.");
2932 return gl::error(GL_OUT_OF_MEMORY, false);
2933 }
2934
2935 if (!drawBuffer)
2936 {
2937 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
2938 return gl::error(GL_OUT_OF_MEMORY, false);
2939 }
2940
2941 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
2942 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
2943
2944 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget))
2945 {
2946 return false;
2947 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002948 }
2949
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002950 if (blitDepthStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002951 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00002952 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
2953 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
2954
2955 if (!readBuffer)
2956 {
2957 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
2958 return gl::error(GL_OUT_OF_MEMORY, false);
2959 }
2960
2961 if (!drawBuffer)
2962 {
2963 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
2964 return gl::error(GL_OUT_OF_MEMORY, false);
2965 }
2966
2967 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
2968 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
2969
2970 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget))
2971 {
2972 return false;
2973 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00002974 }
2975
2976 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00002977}
2978
2979void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
2980 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
2981{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002982 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002983 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002984
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00002985 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2986
2987 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002988 {
2989 gl::Rectangle area;
2990 area.x = x;
2991 area.y = y;
2992 area.width = width;
2993 area.height = height;
2994
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00002995 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
2996 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00002997
2998 colorBufferTexture->Release();
2999 colorBufferTexture = NULL;
3000 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003001}
3002
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003003Image *Renderer11::createImage()
3004{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003005 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003006}
3007
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003008void Renderer11::generateMipmap(Image *dest, Image *src)
3009{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003010 Image11 *dest11 = Image11::makeImage11(dest);
3011 Image11 *src11 = Image11::makeImage11(src);
3012 Image11::generateMipmap(dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003013}
3014
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003015TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3016{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003017 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3018 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003019}
3020
3021TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3022{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003023 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003024}
3025
3026TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3027{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003028 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003029}
3030
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003031static inline unsigned int getFastPixelCopySize(DXGI_FORMAT sourceFormat, GLenum destFormat, GLenum destType)
3032{
3033 if (sourceFormat == DXGI_FORMAT_A8_UNORM &&
3034 destFormat == GL_ALPHA &&
3035 destType == GL_UNSIGNED_BYTE)
3036 {
3037 return 1;
3038 }
3039 else if (sourceFormat == DXGI_FORMAT_R8G8B8A8_UNORM &&
3040 destFormat == GL_RGBA &&
3041 destType == GL_UNSIGNED_BYTE)
3042 {
3043 return 4;
3044 }
3045 else if (sourceFormat == DXGI_FORMAT_B8G8R8A8_UNORM &&
3046 destFormat == GL_BGRA_EXT &&
3047 destType == GL_UNSIGNED_BYTE)
3048 {
3049 return 4;
3050 }
3051 else if (sourceFormat == DXGI_FORMAT_R16G16B16A16_FLOAT &&
3052 destFormat == GL_RGBA &&
3053 destType == GL_HALF_FLOAT_OES)
3054 {
3055 return 8;
3056 }
3057 else if (sourceFormat == DXGI_FORMAT_R32G32B32_FLOAT &&
3058 destFormat == GL_RGB &&
3059 destType == GL_FLOAT)
3060 {
3061 return 12;
3062 }
3063 else if (sourceFormat == DXGI_FORMAT_R32G32B32A32_FLOAT &&
3064 destFormat == GL_RGBA &&
3065 destType == GL_FLOAT)
3066 {
3067 return 16;
3068 }
3069 else
3070 {
3071 return 0;
3072 }
3073}
3074
3075static inline void readPixelColor(const unsigned char *data, DXGI_FORMAT format, unsigned int x,
3076 unsigned int y, int inputPitch, gl::Color *outColor)
3077{
3078 switch (format)
3079 {
3080 case DXGI_FORMAT_R8G8B8A8_UNORM:
3081 {
3082 unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
shannon.woods@transgaming.com25950ea2013-02-28 23:04:15 +00003083 outColor->red = (rgba & 0x000000FF) * (1.0f / 0x000000FF);
3084 outColor->green = (rgba & 0x0000FF00) * (1.0f / 0x0000FF00);
3085 outColor->blue = (rgba & 0x00FF0000) * (1.0f / 0x00FF0000);
3086 outColor->alpha = (rgba & 0xFF000000) * (1.0f / 0xFF000000);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003087 }
3088 break;
3089
3090 case DXGI_FORMAT_A8_UNORM:
3091 {
3092 outColor->red = 0.0f;
3093 outColor->green = 0.0f;
3094 outColor->blue = 0.0f;
3095 outColor->alpha = *(data + x + y * inputPitch) / 255.0f;
3096 }
3097 break;
3098
3099 case DXGI_FORMAT_R32G32B32A32_FLOAT:
3100 {
3101 outColor->red = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 0);
3102 outColor->green = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 1);
3103 outColor->blue = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 2);
3104 outColor->alpha = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 3);
3105 }
3106 break;
3107
3108 case DXGI_FORMAT_R32G32B32_FLOAT:
3109 {
3110 outColor->red = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 0);
3111 outColor->green = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 1);
3112 outColor->blue = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 2);
3113 outColor->alpha = 1.0f;
3114 }
3115 break;
3116
3117 case DXGI_FORMAT_R16G16B16A16_FLOAT:
3118 {
3119 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 0));
3120 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 1));
3121 outColor->blue = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 2));
3122 outColor->alpha = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 3));
3123 }
3124 break;
3125
3126 case DXGI_FORMAT_B8G8R8A8_UNORM:
3127 {
3128 unsigned int bgra = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
shannon.woods@transgaming.com25950ea2013-02-28 23:04:15 +00003129 outColor->red = (bgra & 0x00FF0000) * (1.0f / 0x00FF0000);
3130 outColor->blue = (bgra & 0x000000FF) * (1.0f / 0x000000FF);
3131 outColor->green = (bgra & 0x0000FF00) * (1.0f / 0x0000FF00);
3132 outColor->alpha = (bgra & 0xFF000000) * (1.0f / 0xFF000000);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003133 }
3134 break;
3135
3136 case DXGI_FORMAT_R8_UNORM:
3137 {
3138 outColor->red = *(data + x + y * inputPitch) / 255.0f;
3139 outColor->green = 0.0f;
3140 outColor->blue = 0.0f;
3141 outColor->alpha = 1.0f;
3142 }
3143 break;
3144
3145 case DXGI_FORMAT_R8G8_UNORM:
3146 {
3147 unsigned short rg = *reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch);
3148
3149 outColor->red = (rg & 0xFF00) * (1.0f / 0xFF00);
3150 outColor->green = (rg & 0x00FF) * (1.0f / 0x00FF);
3151 outColor->blue = 0.0f;
3152 outColor->alpha = 1.0f;
3153 }
3154 break;
3155
3156 case DXGI_FORMAT_R16_FLOAT:
3157 {
3158 outColor->red = gl::float16ToFloat32(*reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch));
3159 outColor->green = 0.0f;
3160 outColor->blue = 0.0f;
3161 outColor->alpha = 1.0f;
3162 }
3163 break;
3164
3165 case DXGI_FORMAT_R16G16_FLOAT:
3166 {
3167 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 0));
3168 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 1));
3169 outColor->blue = 0.0f;
3170 outColor->alpha = 1.0f;
3171 }
3172 break;
3173
3174 default:
3175 ERR("ReadPixelColor not implemented for DXGI format %u.", format);
3176 UNIMPLEMENTED();
3177 break;
3178 }
3179}
3180
3181static inline void writePixelColor(const gl::Color &color, GLenum format, GLenum type, unsigned int x,
3182 unsigned int y, int outputPitch, void *outData)
3183{
3184 unsigned char* byteData = reinterpret_cast<unsigned char*>(outData);
3185 unsigned short* shortData = reinterpret_cast<unsigned short*>(outData);
3186
3187 switch (format)
3188 {
3189 case GL_RGBA:
3190 switch (type)
3191 {
3192 case GL_UNSIGNED_BYTE:
3193 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
3194 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3195 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3196 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
3197 break;
3198
3199 default:
3200 ERR("WritePixelColor not implemented for format GL_RGBA and type 0x%X.", type);
3201 UNIMPLEMENTED();
3202 break;
3203 }
3204 break;
3205
3206 case GL_BGRA_EXT:
3207 switch (type)
3208 {
3209 case GL_UNSIGNED_BYTE:
3210 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3211 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3212 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.red + 0.5f);
3213 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
3214 break;
3215
3216 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
3217 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
3218 // this type is packed as follows:
3219 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3220 // --------------------------------------------------------------------------------
3221 // | 4th | 3rd | 2nd | 1st component |
3222 // --------------------------------------------------------------------------------
3223 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
3224 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3225 (static_cast<unsigned short>(15 * color.alpha + 0.5f) << 12) |
3226 (static_cast<unsigned short>(15 * color.red + 0.5f) << 8) |
3227 (static_cast<unsigned short>(15 * color.green + 0.5f) << 4) |
3228 (static_cast<unsigned short>(15 * color.blue + 0.5f) << 0);
3229 break;
3230
3231 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
3232 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
3233 // this type is packed as follows:
3234 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3235 // --------------------------------------------------------------------------------
3236 // | 4th | 3rd | 2nd | 1st component |
3237 // --------------------------------------------------------------------------------
3238 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
3239 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3240 (static_cast<unsigned short>( color.alpha + 0.5f) << 15) |
3241 (static_cast<unsigned short>(31 * color.red + 0.5f) << 10) |
3242 (static_cast<unsigned short>(31 * color.green + 0.5f) << 5) |
3243 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0);
3244 break;
3245
3246 default:
3247 ERR("WritePixelColor not implemented for format GL_BGRA_EXT and type 0x%X.", type);
3248 UNIMPLEMENTED();
3249 break;
3250 }
3251 break;
3252
3253 case GL_RGB:
3254 switch (type)
3255 {
3256 case GL_UNSIGNED_SHORT_5_6_5:
3257 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3258 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0) |
3259 (static_cast<unsigned short>(63 * color.green + 0.5f) << 5) |
3260 (static_cast<unsigned short>(31 * color.red + 0.5f) << 11);
3261 break;
3262
3263 case GL_UNSIGNED_BYTE:
3264 byteData[3 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
3265 byteData[3 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3266 byteData[3 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3267 break;
3268
3269 default:
3270 ERR("WritePixelColor not implemented for format GL_RGB and type 0x%X.", type);
3271 UNIMPLEMENTED();
3272 break;
3273 }
3274 break;
3275
3276 default:
3277 ERR("WritePixelColor not implemented for format 0x%X.", format);
3278 UNIMPLEMENTED();
3279 break;
3280 }
3281}
3282
3283void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3284 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3285 GLint packAlignment, void *pixels)
3286{
3287 D3D11_TEXTURE2D_DESC textureDesc;
3288 texture->GetDesc(&textureDesc);
3289
3290 D3D11_TEXTURE2D_DESC stagingDesc;
3291 stagingDesc.Width = area.width;
3292 stagingDesc.Height = area.height;
3293 stagingDesc.MipLevels = 1;
3294 stagingDesc.ArraySize = 1;
3295 stagingDesc.Format = textureDesc.Format;
3296 stagingDesc.SampleDesc.Count = 1;
3297 stagingDesc.SampleDesc.Quality = 0;
3298 stagingDesc.Usage = D3D11_USAGE_STAGING;
3299 stagingDesc.BindFlags = 0;
3300 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3301 stagingDesc.MiscFlags = 0;
3302
3303 ID3D11Texture2D* stagingTex = NULL;
3304 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3305 if (FAILED(result))
3306 {
3307 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3308 return;
3309 }
3310
3311 ID3D11Texture2D* srcTex = NULL;
3312 if (textureDesc.SampleDesc.Count > 1)
3313 {
3314 D3D11_TEXTURE2D_DESC resolveDesc;
3315 resolveDesc.Width = textureDesc.Width;
3316 resolveDesc.Height = textureDesc.Height;
3317 resolveDesc.MipLevels = 1;
3318 resolveDesc.ArraySize = 1;
3319 resolveDesc.Format = textureDesc.Format;
3320 resolveDesc.SampleDesc.Count = 1;
3321 resolveDesc.SampleDesc.Quality = 0;
3322 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3323 resolveDesc.BindFlags = 0;
3324 resolveDesc.CPUAccessFlags = 0;
3325 resolveDesc.MiscFlags = 0;
3326
3327 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3328 if (FAILED(result))
3329 {
3330 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
3331 stagingTex->Release();
3332 return;
3333 }
3334
3335 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3336 subResource = 0;
3337 }
3338 else
3339 {
3340 srcTex = texture;
3341 srcTex->AddRef();
3342 }
3343
3344 D3D11_BOX srcBox;
3345 srcBox.left = area.x;
3346 srcBox.right = area.x + area.width;
3347 srcBox.top = area.y;
3348 srcBox.bottom = area.y + area.height;
3349 srcBox.front = 0;
3350 srcBox.back = 1;
3351
3352 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3353
3354 srcTex->Release();
3355 srcTex = NULL;
3356
3357 D3D11_MAPPED_SUBRESOURCE mapping;
3358 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3359
3360 unsigned char *source;
3361 int inputPitch;
3362 if (packReverseRowOrder)
3363 {
3364 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3365 inputPitch = -static_cast<int>(mapping.RowPitch);
3366 }
3367 else
3368 {
3369 source = static_cast<unsigned char*>(mapping.pData);
3370 inputPitch = static_cast<int>(mapping.RowPitch);
3371 }
3372
3373 unsigned int fastPixelSize = getFastPixelCopySize(textureDesc.Format, format, type);
3374 if (fastPixelSize != 0)
3375 {
3376 unsigned char *dest = static_cast<unsigned char*>(pixels);
3377 for (int j = 0; j < area.height; j++)
3378 {
3379 memcpy(dest + j * outputPitch, source + j * inputPitch, area.width * fastPixelSize);
3380 }
3381 }
3382 else
3383 {
3384 gl::Color pixelColor;
3385 for (int j = 0; j < area.height; j++)
3386 {
3387 for (int i = 0; i < area.width; i++)
3388 {
3389 readPixelColor(source, textureDesc.Format, i, j, inputPitch, &pixelColor);
3390 writePixelColor(pixelColor, format, type, i, j, outputPitch, pixels);
3391 }
3392 }
3393 }
3394
3395 mDeviceContext->Unmap(stagingTex, 0);
3396
3397 stagingTex->Release();
3398 stagingTex = NULL;
3399}
3400
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003401bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget, RenderTarget *drawRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003402{
3403 ASSERT(readRect.width == drawRect.width && readRect.height == drawRect.height);
3404
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003405 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3406 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003407 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003408 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003409 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003410 }
3411
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003412 ID3D11Texture2D *readTexture = NULL;
3413 unsigned int readSubresource = 0;
3414 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003415 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003416 ID3D11Texture2D *unresolvedTexture = readRenderTarget11->getTexture();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003417
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003418 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3419 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003420
3421 unresolvedTexture->Release();
3422 }
3423 else
3424 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003425 readTexture = readRenderTarget11->getTexture();
3426 readSubresource = readRenderTarget11->getSubresourceIndex();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003427 }
3428
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003429 if (!readTexture)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003430 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003431 ERR("Failed to retrieve the read render target view from the read render target.");
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
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003435 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003436 if (!drawRenderTarget)
3437 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003438 readTexture->Release();
3439 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003440 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003441 }
3442
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003443 ID3D11Texture2D *drawTexture = drawRenderTarget11->getTexture();
3444 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003445
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003446 D3D11_BOX readBox;
3447 readBox.left = readRect.x;
3448 readBox.right = readRect.x + readRect.width;
3449 readBox.top = readRect.y;
3450 readBox.bottom = readRect.y + readRect.height;
3451 readBox.front = 0;
3452 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003453
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003454 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, drawRect.x, drawRect.y, 0,
3455 readTexture, readSubresource, &readBox);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003456
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003457 readTexture->Release();
3458 drawTexture->Release();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003459
3460 return true;
3461}
3462
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003463ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3464{
3465 D3D11_TEXTURE2D_DESC textureDesc;
3466 source->GetDesc(&textureDesc);
3467
3468 if (textureDesc.SampleDesc.Count > 1)
3469 {
3470 D3D11_TEXTURE2D_DESC resolveDesc;
3471 resolveDesc.Width = textureDesc.Width;
3472 resolveDesc.Height = textureDesc.Height;
3473 resolveDesc.MipLevels = 1;
3474 resolveDesc.ArraySize = 1;
3475 resolveDesc.Format = textureDesc.Format;
3476 resolveDesc.SampleDesc.Count = 1;
3477 resolveDesc.SampleDesc.Quality = 0;
3478 resolveDesc.Usage = textureDesc.Usage;
3479 resolveDesc.BindFlags = textureDesc.BindFlags;
3480 resolveDesc.CPUAccessFlags = 0;
3481 resolveDesc.MiscFlags = 0;
3482
3483 ID3D11Texture2D *resolveTexture = NULL;
3484 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3485 if (FAILED(result))
3486 {
3487 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3488 return NULL;
3489 }
3490
3491 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3492 return resolveTexture;
3493 }
3494 else
3495 {
3496 source->AddRef();
3497 return source;
3498 }
3499}
3500
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003501}