blob: 5aad9128333ec9cf68650864bef7d28eebd71065 [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002//
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00003// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// Renderer11.cpp: Implements a back-end specific class for the D3D11 renderer.
9
daniel@transgaming.com5503fd02012-11-28 19:38:57 +000010#include "libGLESv2/main.h"
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +000011#include "common/utilities.h"
daniel@transgaming.com18adad02012-11-28 21:04:03 +000012#include "libGLESv2/Buffer.h"
daniel@transgaming.com53670042012-11-28 20:55:51 +000013#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com80fc3322012-11-28 21:02:13 +000014#include "libGLESv2/Framebuffer.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000015#include "libGLESv2/RenderBuffer.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000016#include "libGLESv2/renderer/Renderer11.h"
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +000017#include "libGLESv2/renderer/RenderTarget11.h"
daniel@transgaming.com65e65372012-11-28 19:33:50 +000018#include "libGLESv2/renderer/renderer11_utils.h"
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +000019#include "libGLESv2/renderer/formatutils11.h"
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +000020#include "libGLESv2/renderer/ShaderExecutable11.h"
daniel@transgaming.coma60160b2012-11-28 19:41:15 +000021#include "libGLESv2/renderer/SwapChain11.h"
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000022#include "libGLESv2/renderer/Image11.h"
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +000023#include "libGLESv2/renderer/VertexBuffer11.h"
daniel@transgaming.com11c2af52012-12-20 21:10:01 +000024#include "libGLESv2/renderer/IndexBuffer11.h"
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +000025#include "libGLESv2/renderer/BufferStorage11.h"
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000026#include "libGLESv2/renderer/VertexDataManager.h"
27#include "libGLESv2/renderer/IndexDataManager.h"
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +000028#include "libGLESv2/renderer/TextureStorage11.h"
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +000029#include "libGLESv2/renderer/Query11.h"
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +000030#include "libGLESv2/renderer/Fence11.h"
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000031
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +000032#include "libGLESv2/renderer/shaders/compiled/passthrough2d11vs.h"
33#include "libGLESv2/renderer/shaders/compiled/passthroughrgba2d11ps.h"
34#include "libGLESv2/renderer/shaders/compiled/passthroughrgb2d11ps.h"
35#include "libGLESv2/renderer/shaders/compiled/passthroughlum2d11ps.h"
36#include "libGLESv2/renderer/shaders/compiled/passthroughlumalpha2d11ps.h"
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000037
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +000038#include "libGLESv2/renderer/shaders/compiled/passthrough3d11vs.h"
39#include "libGLESv2/renderer/shaders/compiled/passthrough3d11gs.h"
40#include "libGLESv2/renderer/shaders/compiled/passthroughrgba3d11ps.h"
41#include "libGLESv2/renderer/shaders/compiled/passthroughrgb3d11ps.h"
42#include "libGLESv2/renderer/shaders/compiled/passthroughlum3d11ps.h"
43#include "libGLESv2/renderer/shaders/compiled/passthroughlumalpha3d11ps.h"
44
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000045#include "libGLESv2/renderer/shaders/compiled/clear11vs.h"
shannonwoods@chromium.org98949842013-05-30 00:01:51 +000046#include "libGLESv2/renderer/shaders/compiled/clearsingle11ps.h"
47#include "libGLESv2/renderer/shaders/compiled/clearmultiple11ps.h"
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +000048
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000049#include "libEGL/Display.h"
50
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +000051#ifdef _DEBUG
52// this flag enables suppressing some spurious warnings that pop up in certain WebGL samples
53// and conformance tests. to enable all warnings, remove this define.
54#define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1
55#endif
56
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000057namespace rx
58{
daniel@transgaming.com65e65372012-11-28 19:33:50 +000059static const DXGI_FORMAT RenderTargetFormats[] =
60 {
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000061 DXGI_FORMAT_B8G8R8A8_UNORM,
daniel@transgaming.com65e65372012-11-28 19:33:50 +000062 DXGI_FORMAT_R8G8B8A8_UNORM
63 };
64
65static const DXGI_FORMAT DepthStencilFormats[] =
66 {
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +000067 DXGI_FORMAT_UNKNOWN,
68 DXGI_FORMAT_D24_UNORM_S8_UINT,
69 DXGI_FORMAT_D16_UNORM
daniel@transgaming.com65e65372012-11-28 19:33:50 +000070 };
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000071
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000072enum
73{
74 MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16
75};
76
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000077Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc)
78{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +000079 mVertexDataManager = NULL;
80 mIndexDataManager = NULL;
81
daniel@transgaming.comc5114302012-12-20 21:11:36 +000082 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +000083 mTriangleFanIB = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +000084
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +000085 mCopyResourcesInitialized = false;
86 mCopyVB = NULL;
87 mCopySampler = NULL;
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +000088 mCopy2DIL = NULL;
89 mCopy2DVS = NULL;
90 mCopyRGBA2DPS = NULL;
91 mCopyRGB2DPS = NULL;
92 mCopyLum2DPS = NULL;
93 mCopyLumAlpha2DPS = NULL;
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +000094 mCopy3DIL = NULL;
95 mCopy3DVS = NULL;
96 mCopy3DGS = NULL;
97 mCopyRGBA3DPS = NULL;
98 mCopyRGB3DPS = NULL;
99 mCopyLum3DPS = NULL;
100 mCopyLumAlpha3DPS = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +0000101
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +0000102 mClearResourcesInitialized = false;
103 mClearVB = NULL;
104 mClearIL = NULL;
105 mClearVS = NULL;
shannonwoods@chromium.org98949842013-05-30 00:01:51 +0000106 mClearSinglePS = NULL;
107 mClearMultiplePS = NULL;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +0000108 mClearScissorRS = NULL;
109 mClearNoScissorRS = NULL;
110
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000111 mSyncQuery = NULL;
112
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000113 mD3d11Module = NULL;
114 mDxgiModule = NULL;
115
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000116 mDeviceLost = false;
117
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000118 mMaxSupportedSamples = 0;
119
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000120 mDevice = NULL;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000121 mDeviceContext = NULL;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000122 mDxgiAdapter = NULL;
123 mDxgiFactory = NULL;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000124
125 mDriverConstantBufferVS = NULL;
126 mDriverConstantBufferPS = NULL;
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000127
128 mBGRATextureSupport = false;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +0000129
130 mIsGeometryShaderActive = false;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000131}
132
133Renderer11::~Renderer11()
134{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000135 release();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000136}
137
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000138Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
139{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +0000140 ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer));
daniel@transgaming.comb64ed282012-11-28 20:54:02 +0000141 return static_cast<rx::Renderer11*>(renderer);
142}
143
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000144#ifndef __d3d11_1_h__
145#define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081)
146#endif
147
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000148EGLint Renderer11::initialize()
149{
daniel@transgaming.com25e16af2012-11-28 21:05:57 +0000150 if (!initializeCompiler())
151 {
152 return EGL_NOT_INITIALIZED;
153 }
154
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000155 mDxgiModule = LoadLibrary(TEXT("dxgi.dll"));
156 mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000157
158 if (mD3d11Module == NULL || mDxgiModule == NULL)
159 {
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000160 ERR("Could not load D3D11 or DXGI library - aborting!\n");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000161 return EGL_NOT_INITIALIZED;
162 }
163
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000164 // create the D3D11 device
165 ASSERT(mDevice == NULL);
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000166 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000167
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000168 if (D3D11CreateDevice == NULL)
169 {
170 ERR("Could not retrieve D3D11CreateDevice address - aborting!\n");
171 return EGL_NOT_INITIALIZED;
172 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000173
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000174 D3D_FEATURE_LEVEL featureLevels[] =
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000175 {
176 D3D_FEATURE_LEVEL_11_0,
177 D3D_FEATURE_LEVEL_10_1,
178 D3D_FEATURE_LEVEL_10_0,
179 };
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000180
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000181 HRESULT result = S_OK;
182
183#ifdef _DEBUG
184 result = D3D11CreateDevice(NULL,
185 D3D_DRIVER_TYPE_HARDWARE,
186 NULL,
187 D3D11_CREATE_DEVICE_DEBUG,
188 featureLevels,
189 ArraySize(featureLevels),
190 D3D11_SDK_VERSION,
191 &mDevice,
192 &mFeatureLevel,
193 &mDeviceContext);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000194
daniel@transgaming.com25072f62012-11-28 19:31:32 +0000195 if (!mDevice || FAILED(result))
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000196 {
shannonwoods@chromium.orgc3419c12013-05-30 00:02:01 +0000197 ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n");
198 }
199
200 if (!mDevice || FAILED(result))
201#endif
202 {
203 result = D3D11CreateDevice(NULL,
204 D3D_DRIVER_TYPE_HARDWARE,
205 NULL,
206 0,
207 featureLevels,
208 ArraySize(featureLevels),
209 D3D11_SDK_VERSION,
210 &mDevice,
211 &mFeatureLevel,
212 &mDeviceContext);
213
214 if (!mDevice || FAILED(result))
215 {
216 ERR("Could not create D3D11 device - aborting!\n");
217 return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer
218 }
daniel@transgaming.comc1e26342012-11-28 19:31:16 +0000219 }
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000220
221 IDXGIDevice *dxgiDevice = NULL;
222 result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
223
224 if (FAILED(result))
225 {
226 ERR("Could not query DXGI device - aborting!\n");
227 return EGL_NOT_INITIALIZED;
228 }
229
230 result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
231
232 if (FAILED(result))
233 {
234 ERR("Could not retrieve DXGI adapter - aborting!\n");
235 return EGL_NOT_INITIALIZED;
236 }
237
238 dxgiDevice->Release();
239
daniel@transgaming.com1f811f52012-11-28 20:57:39 +0000240 mDxgiAdapter->GetDesc(&mAdapterDescription);
241 memset(mDescription, 0, sizeof(mDescription));
242 wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1);
243
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000244 result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
245
246 if (!mDxgiFactory || FAILED(result))
247 {
248 ERR("Could not create DXGI factory - aborting!\n");
249 return EGL_NOT_INITIALIZED;
250 }
251
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000252 // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log
253#if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG)
254 ID3D11InfoQueue *infoQueue;
255 result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue);
256
257 if (SUCCEEDED(result))
258 {
259 D3D11_MESSAGE_ID hideMessages[] =
260 {
261 D3D11_MESSAGE_ID_DEVICE_OMSETRENDERTARGETS_HAZARD,
shannon.woods%transgaming.com@gtempaccount.comf9686c22013-04-13 03:33:45 +0000262 D3D11_MESSAGE_ID_DEVICE_PSSETSHADERRESOURCES_HAZARD,
263 D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET
shannon.woods@transgaming.com236be772013-02-28 23:18:15 +0000264 };
265
266 D3D11_INFO_QUEUE_FILTER filter = {0};
267 filter.DenyList.NumIDs = ArraySize(hideMessages);
268 filter.DenyList.pIDList = hideMessages;
269
270 infoQueue->AddStorageFilterEntries(&filter);
271
272 infoQueue->Release();
273 }
274#endif
275
Geoff Lang61e49a52013-05-29 10:22:58 -0400276 mMaxSupportedSamples = 0;
277
278 const d3d11::DXGIFormatSet &dxgiFormats = d3d11::GetAllUsedDXGIFormats();
279 for (d3d11::DXGIFormatSet::const_iterator i = dxgiFormats.begin(); i != dxgiFormats.end(); ++i)
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000280 {
Geoff Lang61e49a52013-05-29 10:22:58 -0400281 MultisampleSupportInfo support = getMultisampleSupportInfo(*i);
282 mMultisampleSupportMap.insert(std::make_pair(*i, support));
283 mMaxSupportedSamples = std::max(mMaxSupportedSamples, support.maxSupportedSamples);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000284 }
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000285
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000286 initializeDevice();
287
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +0000288 // BGRA texture support is optional in feature levels 10 and 10_1
289 UINT formatSupport;
290 result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport);
291 if (FAILED(result))
292 {
293 ERR("Error checking BGRA format support: 0x%08X", result);
294 }
295 else
296 {
297 const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET);
298 mBGRATextureSupport = (formatSupport & flags) == flags;
299 }
300
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +0000301 // Check floating point texture support
302 static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE;
303 static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET;
304 static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE;
305
306 DXGI_FORMAT float16Formats[] =
307 {
308 DXGI_FORMAT_R16_FLOAT,
309 DXGI_FORMAT_R16G16_FLOAT,
310 DXGI_FORMAT_R16G16B16A16_FLOAT,
311 };
312
313 DXGI_FORMAT float32Formats[] =
314 {
315 DXGI_FORMAT_R32_FLOAT,
316 DXGI_FORMAT_R32G32_FLOAT,
317 DXGI_FORMAT_R32G32B32_FLOAT,
318 DXGI_FORMAT_R32G32B32A32_FLOAT,
319 };
320
321 mFloat16TextureSupport = true;
322 mFloat16FilterSupport = true;
323 mFloat16RenderSupport = true;
324 for (unsigned int i = 0; i < ArraySize(float16Formats); i++)
325 {
326 if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport)))
327 {
328 mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
329 mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
330 mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
331 }
332 else
333 {
334 mFloat16TextureSupport = false;
335 mFloat16RenderSupport = false;
336 mFloat16FilterSupport = false;
337 }
338 }
339
340 mFloat32TextureSupport = true;
341 mFloat32FilterSupport = true;
342 mFloat32RenderSupport = true;
343 for (unsigned int i = 0; i < ArraySize(float32Formats); i++)
344 {
345 if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport)))
346 {
347 mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags;
348 mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags;
349 mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags;
350 }
351 else
352 {
353 mFloat32TextureSupport = false;
354 mFloat32FilterSupport = false;
355 mFloat32RenderSupport = false;
356 }
357 }
358
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +0000359 // Check compressed texture support
360 const unsigned int requiredCompressedTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D;
361
362 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &formatSupport)))
363 {
364 mDXT1TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
365 }
366 else
367 {
368 mDXT1TextureSupport = false;
369 }
370
371 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport)))
372 {
373 mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
374 }
375 else
376 {
377 mDXT3TextureSupport = false;
378 }
379
380 if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC5_UNORM, &formatSupport)))
381 {
382 mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags;
383 }
384 else
385 {
386 mDXT5TextureSupport = false;
387 }
388
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +0000389 // Check depth texture support
390 DXGI_FORMAT depthTextureFormats[] =
391 {
392 DXGI_FORMAT_D16_UNORM,
393 DXGI_FORMAT_D24_UNORM_S8_UINT,
394 };
395
396 static const unsigned int requiredDepthTextureFlags = D3D11_FORMAT_SUPPORT_DEPTH_STENCIL |
397 D3D11_FORMAT_SUPPORT_TEXTURE2D;
398
399 mDepthTextureSupport = true;
400 for (unsigned int i = 0; i < ArraySize(depthTextureFormats); i++)
401 {
402 if (SUCCEEDED(mDevice->CheckFormatSupport(depthTextureFormats[i], &formatSupport)))
403 {
404 mDepthTextureSupport = mDepthTextureSupport && ((formatSupport & requiredDepthTextureFlags) == requiredDepthTextureFlags);
405 }
406 else
407 {
408 mDepthTextureSupport = false;
409 }
410 }
411
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000412 return EGL_SUCCESS;
413}
414
415// do any one-time device initialization
416// NOTE: this is also needed after a device lost/reset
417// to reset the scene status and ensure the default states are reset.
418void Renderer11::initializeDevice()
419{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000420 mStateCache.initialize(mDevice);
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +0000421 mInputLayoutCache.initialize(mDevice, mDeviceContext);
422
423 ASSERT(!mVertexDataManager && !mIndexDataManager);
424 mVertexDataManager = new VertexDataManager(this);
425 mIndexDataManager = new IndexDataManager(this);
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000426
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000427 markAllStateDirty();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000428}
429
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000430int Renderer11::generateConfigs(ConfigDesc **configDescList)
431{
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +0000432 unsigned int numRenderFormats = ArraySize(RenderTargetFormats);
433 unsigned int numDepthFormats = ArraySize(DepthStencilFormats);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000434 (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats];
435 int numConfigs = 0;
436
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000437 for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000438 {
daniel@transgaming.come3e826d2012-11-28 19:42:35 +0000439 for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000440 {
441 DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex];
442
443 UINT formatSupport = 0;
444 HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport);
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +0000445
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000446 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET))
447 {
448 DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex];
449
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000450 bool depthStencilFormatOK = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000451
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000452 if (depthStencilFormat != DXGI_FORMAT_UNKNOWN)
453 {
454 UINT formatSupport = 0;
455 result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport);
456 depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL);
457 }
458
459 if (depthStencilFormatOK)
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000460 {
461 ConfigDesc newConfig;
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +0000462 newConfig.renderTargetFormat = d3d11_gl::GetInternalFormat(renderTargetFormat);
463 newConfig.depthStencilFormat = d3d11_gl::GetInternalFormat(depthStencilFormat);
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000464 newConfig.multiSample = 0; // FIXME: enumerate multi-sampling
465 newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000466 newConfig.es3Capable = true;
daniel@transgaming.com65e65372012-11-28 19:33:50 +0000467
468 (*configDescList)[numConfigs++] = newConfig;
469 }
470 }
471 }
472 }
473
474 return numConfigs;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000475}
476
477void Renderer11::deleteConfigs(ConfigDesc *configDescList)
478{
479 delete [] (configDescList);
480}
481
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000482void Renderer11::sync(bool block)
483{
daniel@transgaming.combdf787f2013-02-01 03:20:36 +0000484 if (block)
485 {
486 HRESULT result;
487
488 if (!mSyncQuery)
489 {
490 D3D11_QUERY_DESC queryDesc;
491 queryDesc.Query = D3D11_QUERY_EVENT;
492 queryDesc.MiscFlags = 0;
493
494 result = mDevice->CreateQuery(&queryDesc, &mSyncQuery);
495 ASSERT(SUCCEEDED(result));
496 }
497
498 mDeviceContext->End(mSyncQuery);
499 mDeviceContext->Flush();
500
501 do
502 {
503 result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH);
504
505 // Keep polling, but allow other threads to do something useful first
506 Sleep(0);
507
508 if (testDeviceLost(true))
509 {
510 return;
511 }
512 }
513 while (result == S_FALSE);
514 }
515 else
516 {
517 mDeviceContext->Flush();
518 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000519}
520
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000521SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat)
522{
daniel@transgaming.coma60160b2012-11-28 19:41:15 +0000523 return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +0000524}
525
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000526void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState)
527{
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000528 if (type == gl::SAMPLER_PIXEL)
529 {
530 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
531 {
532 ERR("Pixel shader sampler index %i is not valid.", index);
533 return;
534 }
535
536 if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0)
537 {
538 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
539
540 if (!dxSamplerState)
541 {
542 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
543 "sampler state for pixel shaders at slot %i.", index);
544 }
545
546 mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState);
547
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000548 mCurPixelSamplerStates[index] = samplerState;
549 }
550
551 mForceSetPixelSamplerStates[index] = false;
552 }
553 else if (type == gl::SAMPLER_VERTEX)
554 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000555 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000556 {
557 ERR("Vertex shader sampler index %i is not valid.", index);
558 return;
559 }
560
561 if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0)
562 {
563 ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState);
564
565 if (!dxSamplerState)
566 {
567 ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default"
568 "sampler state for vertex shaders at slot %i.", index);
569 }
570
571 mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState);
572
daniel@transgaming.com54de24f2013-01-11 04:07:59 +0000573 mCurVertexSamplerStates[index] = samplerState;
574 }
575
576 mForceSetVertexSamplerStates[index] = false;
577 }
578 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000579}
580
581void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture)
582{
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000583 ID3D11ShaderResourceView *textureSRV = NULL;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000584 unsigned int serial = 0;
585 bool forceSetTexture = false;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000586
587 if (texture)
588 {
589 TextureStorageInterface *texStorage = texture->getNativeTexture();
590 if (texStorage)
591 {
592 TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance());
593 textureSRV = storage11->getSRV();
594 }
595
596 // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly
597 // missing the shader resource view
598 ASSERT(textureSRV != NULL);
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000599
600 serial = texture->getTextureSerial();
601 forceSetTexture = texture->hasDirtyImages();
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000602 }
603
604 if (type == gl::SAMPLER_PIXEL)
605 {
606 if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS)
607 {
608 ERR("Pixel shader sampler index %i is not valid.", index);
609 return;
610 }
611
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000612 if (forceSetTexture || mCurPixelTextureSerials[index] != serial)
613 {
614 mDeviceContext->PSSetShaderResources(index, 1, &textureSRV);
615 }
616
617 mCurPixelTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000618 }
619 else if (type == gl::SAMPLER_VERTEX)
620 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000621 if (index < 0 || index >= (int)getMaxVertexTextureImageUnits())
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000622 {
623 ERR("Vertex shader sampler index %i is not valid.", index);
624 return;
625 }
626
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +0000627 if (forceSetTexture || mCurVertexTextureSerials[index] != serial)
628 {
629 mDeviceContext->VSSetShaderResources(index, 1, &textureSRV);
630 }
631
632 mCurVertexTextureSerials[index] = serial;
daniel@transgaming.com0785fad2013-01-11 04:08:10 +0000633 }
634 else UNREACHABLE();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000635}
636
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000637bool Renderer11::setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[])
638{
639 // convert buffers to ID3D11Buffer*
640 ID3D11Buffer *vertexConstantBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = { NULL };
641 ID3D11Buffer *pixelConstantBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = { NULL };
642
643 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
644 {
645 const gl::Buffer *uniformBuffer = vertexUniformBuffers[uniformBufferIndex];
646 if (uniformBuffer)
647 {
648 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
649 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(GL_UNIFORM_BUFFER);
650
651 if (!constantBuffer)
652 {
653 return false;
654 }
655
656 vertexConstantBuffers[uniformBufferIndex] = constantBuffer;
657 }
658 }
659
660 for (unsigned int uniformBufferIndex = 0; uniformBufferIndex < gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS; uniformBufferIndex++)
661 {
662 const gl::Buffer *uniformBuffer = fragmentUniformBuffers[uniformBufferIndex];
663 if (uniformBuffer)
664 {
665 BufferStorage11 *bufferStorage = BufferStorage11::makeBufferStorage11(uniformBuffer->getStorage());
666 ID3D11Buffer *constantBuffer = bufferStorage->getBuffer(GL_UNIFORM_BUFFER);
667
668 if (!constantBuffer)
669 {
670 return false;
671 }
672
673 pixelConstantBuffers[uniformBufferIndex] = constantBuffer;
674 }
675 }
676
677 mDeviceContext->VSSetConstantBuffers(getReservedVertexUniformBuffers(), getMaxVertexShaderUniformBuffers(), vertexConstantBuffers);
678 mDeviceContext->PSSetConstantBuffers(getReservedFragmentUniformBuffers(), getMaxFragmentShaderUniformBuffers(), pixelConstantBuffers);
679
680 return true;
681}
682
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000683void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000684{
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000685 if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000686 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000687 ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled,
688 mCurDepthSize);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000689 if (!dxRasterState)
690 {
daniel@transgaming.com0f9b3202013-01-11 04:08:05 +0000691 ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default"
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000692 "rasterizer state.");
693 }
694
695 mDeviceContext->RSSetState(dxRasterState);
696
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000697 mCurRasterState = rasterState;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000698 }
699
700 mForceSetRasterState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000701}
702
703void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor,
704 unsigned int sampleMask)
705{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000706 if (mForceSetBlendState ||
707 memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 ||
708 memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0 ||
709 sampleMask != mCurSampleMask)
710 {
711 ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState);
712 if (!dxBlendState)
713 {
714 ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default "
715 "blend state.");
716 }
717
shannonwoods@chromium.org568c82e2013-05-30 00:13:15 +0000718 float blendColors[4] = {0.0f};
719 if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
720 blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
721 {
722 blendColors[0] = blendColor.red;
723 blendColors[1] = blendColor.green;
724 blendColors[2] = blendColor.blue;
725 blendColors[3] = blendColor.alpha;
726 }
727 else
728 {
729 blendColors[0] = blendColor.alpha;
730 blendColors[1] = blendColor.alpha;
731 blendColors[2] = blendColor.alpha;
732 blendColors[3] = blendColor.alpha;
733 }
734
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000735 mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask);
736
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +0000737 mCurBlendState = blendState;
738 mCurBlendColor = blendColor;
739 mCurSampleMask = sampleMask;
740 }
741
742 mForceSetBlendState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000743}
744
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000745void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000746 int stencilBackRef, bool frontFaceCCW)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000747{
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000748 if (mForceSetDepthStencilState ||
749 memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 ||
750 stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef)
751 {
752 if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
753 stencilRef != stencilBackRef ||
754 depthStencilState.stencilMask != depthStencilState.stencilBackMask)
755 {
756 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are "
757 "invalid under WebGL.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000758 return gl::error(GL_INVALID_OPERATION);
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000759 }
760
761 ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState);
762 if (!dxDepthStencilState)
763 {
764 ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, "
765 "setting the default depth stencil state.");
766 }
767
768 mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef));
769
daniel@transgaming.com5503fd02012-11-28 19:38:57 +0000770 mCurDepthStencilState = depthStencilState;
771 mCurStencilRef = stencilRef;
772 mCurStencilBackRef = stencilBackRef;
773 }
774
775 mForceSetDepthStencilState = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000776}
777
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000778void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000779{
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000780 if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 ||
781 enabled != mScissorEnabled)
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000782 {
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000783 if (enabled)
784 {
785 D3D11_RECT rect;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000786 rect.left = std::max(0, scissor.x);
787 rect.top = std::max(0, scissor.y);
788 rect.right = scissor.x + std::max(0, scissor.width);
789 rect.bottom = scissor.y + std::max(0, scissor.height);
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000790
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000791 mDeviceContext->RSSetScissorRects(1, &rect);
792 }
793
794 if (enabled != mScissorEnabled)
795 {
796 mForceSetRasterState = true;
797 }
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000798
799 mCurScissor = scissor;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000800 mScissorEnabled = enabled;
daniel@transgaming.comdcf1e672012-11-28 19:38:19 +0000801 }
802
803 mForceSetScissor = false;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000804}
805
daniel@transgaming.com12985182012-12-20 20:56:31 +0000806bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
shannon.woods@transgaming.com0b236e22013-01-25 21:57:07 +0000807 bool ignoreViewport)
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000808{
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000809 gl::Rectangle actualViewport = viewport;
810 float actualZNear = gl::clamp01(zNear);
811 float actualZFar = gl::clamp01(zFar);
812 if (ignoreViewport)
813 {
814 actualViewport.x = 0;
815 actualViewport.y = 0;
816 actualViewport.width = mRenderTargetDesc.width;
817 actualViewport.height = mRenderTargetDesc.height;
818 actualZNear = 0.0f;
819 actualZFar = 1.0f;
820 }
daniel@transgaming.com53670042012-11-28 20:55:51 +0000821
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000822 // Get D3D viewport bounds, which depends on the feature level
823 const Range& viewportBounds = getViewportBounds();
824
825 // 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 +0000826 D3D11_VIEWPORT dxViewport;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +0000827 dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end);
828 dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end);
829 dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension());
830 dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension());
831 dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX));
832 dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY));
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000833 dxViewport.MinDepth = actualZNear;
834 dxViewport.MaxDepth = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000835
836 if (dxViewport.Width <= 0 || dxViewport.Height <= 0)
837 {
838 return false; // Nothing to render
839 }
840
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000841 bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 ||
842 actualZNear != mCurNear || actualZFar != mCurFar;
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000843
daniel@transgaming.com53670042012-11-28 20:55:51 +0000844 if (viewportChanged)
845 {
846 mDeviceContext->RSSetViewports(1, &dxViewport);
847
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +0000848 mCurViewport = actualViewport;
849 mCurNear = actualZNear;
850 mCurFar = actualZFar;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000851
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000852 mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f;
853 mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f;
854 mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f);
855 mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f);
daniel@transgaming.com53670042012-11-28 20:55:51 +0000856
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000857 mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f;
858 mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000859
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000860 mVertexConstants.depthRange[0] = actualZNear;
861 mVertexConstants.depthRange[1] = actualZFar;
862 mVertexConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.comed36abd2013-01-11 21:15:58 +0000863
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +0000864 mPixelConstants.depthRange[0] = actualZNear;
865 mPixelConstants.depthRange[1] = actualZFar;
866 mPixelConstants.depthRange[2] = actualZFar - actualZNear;
daniel@transgaming.com53670042012-11-28 20:55:51 +0000867 }
868
869 mForceSetViewport = false;
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000870 return true;
871}
872
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000873bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count)
874{
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000875 D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000876
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000877 switch (mode)
878 {
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000879 case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break;
880 case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break;
daniel@transgaming.com1ef09672013-01-11 04:10:37 +0000881 case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000882 case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
883 case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
884 case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +0000885 // emulate fans via rewriting index buffer
886 case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000887 default:
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000888 return gl::error(GL_INVALID_ENUM, false);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000889 }
890
daniel@transgaming.comc52be632012-11-28 21:04:28 +0000891 mDeviceContext->IASetPrimitiveTopology(primitiveTopology);
daniel@transgaming.com0b03b062012-11-28 21:03:49 +0000892
893 return count > 0;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000894}
895
896bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer)
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000897{
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000898 // Get the color render buffer and serial
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000899 // Also extract the render target dimensions and view
900 unsigned int renderTargetWidth = 0;
901 unsigned int renderTargetHeight = 0;
902 GLenum renderTargetFormat = 0;
903 unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0};
904 ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
905 bool missingColorRenderTarget = true;
906
907 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000908 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000909 const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment);
910
911 if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000912 {
shannon.woods%transgaming.com@gtempaccount.com4059a382013-04-13 03:31:16 +0000913 // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order)
914 ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment));
915
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000916 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000917
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000918 if (!colorbuffer)
919 {
920 ERR("render target pointer unexpectedly null.");
921 return false;
922 }
shannon.woods@transgaming.com3e3da582013-02-28 23:09:03 +0000923
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000924 // check for zero-sized default framebuffer, which is a special case.
925 // in this case we do not wish to modify any state and just silently return false.
926 // this will not report any gl error but will cause the calling method to return.
927 if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0)
928 {
929 return false;
930 }
931
932 renderTargetSerials[colorAttachment] = colorbuffer->getSerial();
933
934 // Extract the render target dimensions and view
935 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
936 if (!renderTarget)
937 {
938 ERR("render target pointer unexpectedly null.");
939 return false;
940 }
941
942 framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView();
943 if (!framebufferRTVs[colorAttachment])
944 {
945 ERR("render target view pointer unexpectedly null.");
946 return false;
947 }
948
949 if (missingColorRenderTarget)
950 {
951 renderTargetWidth = colorbuffer->getWidth();
952 renderTargetHeight = colorbuffer->getHeight();
953 renderTargetFormat = colorbuffer->getActualFormat();
954 missingColorRenderTarget = false;
955 }
956 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000957 }
958
959 // Get the depth stencil render buffer and serials
960 gl::Renderbuffer *depthStencil = NULL;
961 unsigned int depthbufferSerial = 0;
962 unsigned int stencilbufferSerial = 0;
963 if (framebuffer->getDepthbufferType() != GL_NONE)
964 {
965 depthStencil = framebuffer->getDepthbuffer();
966 if (!depthStencil)
967 {
968 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000969 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000970 return false;
971 }
972
973 depthbufferSerial = depthStencil->getSerial();
974 }
975 else if (framebuffer->getStencilbufferType() != GL_NONE)
976 {
977 depthStencil = framebuffer->getStencilbuffer();
978 if (!depthStencil)
979 {
980 ERR("Depth stencil pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000981 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000982 return false;
983 }
984
985 stencilbufferSerial = depthStencil->getSerial();
986 }
987
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000988 // Extract the depth stencil sizes and view
989 unsigned int depthSize = 0;
990 unsigned int stencilSize = 0;
991 ID3D11DepthStencilView* framebufferDSV = NULL;
992 if (depthStencil)
993 {
994 RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil());
995 if (!depthStencilRenderTarget)
996 {
997 ERR("render target pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +0000998 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +0000999 return false;
1000 }
1001
1002 framebufferDSV = depthStencilRenderTarget->getDepthStencilView();
1003 if (!framebufferDSV)
1004 {
1005 ERR("depth stencil view pointer unexpectedly null.");
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001006 SafeRelease(framebufferRTVs);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001007 return false;
1008 }
1009
1010 // If there is no render buffer, the width, height and format values come from
1011 // the depth stencil
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001012 if (missingColorRenderTarget)
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001013 {
1014 renderTargetWidth = depthStencil->getWidth();
1015 renderTargetHeight = depthStencil->getHeight();
1016 renderTargetFormat = depthStencil->getActualFormat();
1017 }
1018
1019 depthSize = depthStencil->getDepthSize();
1020 stencilSize = depthStencil->getStencilSize();
1021 }
1022
1023 // Apply the render target and depth stencil
1024 if (!mRenderTargetDescInitialized || !mDepthStencilInitialized ||
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001025 memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 ||
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001026 depthbufferSerial != mAppliedDepthbufferSerial ||
1027 stencilbufferSerial != mAppliedStencilbufferSerial)
1028 {
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001029 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV);
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001030
1031 mRenderTargetDesc.width = renderTargetWidth;
1032 mRenderTargetDesc.height = renderTargetHeight;
1033 mRenderTargetDesc.format = renderTargetFormat;
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00001034 mForceSetViewport = true;
1035 mForceSetScissor = true;
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001036
1037 if (!mDepthStencilInitialized || depthSize != mCurDepthSize)
1038 {
1039 mCurDepthSize = depthSize;
1040 mForceSetRasterState = true;
1041 }
1042
1043 mCurStencilSize = stencilSize;
1044
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001045 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1046 {
1047 mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex];
1048 }
daniel@transgaming.com80fc3322012-11-28 21:02:13 +00001049 mAppliedDepthbufferSerial = depthbufferSerial;
1050 mAppliedStencilbufferSerial = stencilbufferSerial;
1051 mRenderTargetDescInitialized = true;
1052 mDepthStencilInitialized = true;
1053 }
1054
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001055 SafeRelease(framebufferRTVs);
1056 SafeRelease(framebufferDSV);
daniel@transgaming.comae39ee22012-11-28 19:42:02 +00001057
1058 return true;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +00001059}
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001060
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001061GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001062{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001063 TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS];
1064 GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
1065 if (err != GL_NO_ERROR)
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001066 {
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001067 return err;
1068 }
daniel@transgaming.comda495a12012-11-28 21:03:56 +00001069
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001070 return mInputLayoutCache.applyVertexBuffers(attributes, programBinary);
daniel@transgaming.comdef9f0f2012-11-28 20:53:20 +00001071}
1072
daniel@transgaming.com31240482012-11-28 21:06:41 +00001073GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001074{
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001075 GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001076
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001077 if (err == GL_NO_ERROR)
1078 {
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001079 if (indexInfo->storage)
1080 {
1081 if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset)
1082 {
1083 BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage);
1084 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1085
shannonwoods@chromium.org675526e2013-05-30 00:04:49 +00001086 mDeviceContext->IASetIndexBuffer(storage->getBuffer(GL_ELEMENT_ARRAY_BUFFER), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001087
1088 mAppliedIBSerial = 0;
1089 mAppliedStorageIBSerial = storage->getSerial();
1090 mAppliedIBOffset = indexInfo->startOffset;
1091 }
1092 }
1093 else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset)
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001094 {
1095 IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer);
1096
daniel@transgaming.com22ada2c2013-01-11 04:07:12 +00001097 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset);
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001098
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001099 mAppliedIBSerial = indexInfo->serial;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001100 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001101 mAppliedIBOffset = indexInfo->startOffset;
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001102 }
1103 }
1104
1105 return err;
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001106}
1107
1108void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances)
1109{
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001110 if (mode == GL_LINE_LOOP)
1111 {
1112 drawLineLoop(count, GL_NONE, NULL, 0, NULL);
1113 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001114 else if (mode == GL_TRIANGLE_FAN)
1115 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001116 drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001117 }
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001118 else if (instances > 0)
1119 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001120 mDeviceContext->DrawInstanced(count, instances, 0, 0);
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001121 }
1122 else
1123 {
1124 mDeviceContext->Draw(count, 0);
1125 }
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001126}
1127
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001128void 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 +00001129{
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001130 if (mode == GL_LINE_LOOP)
1131 {
1132 drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer);
1133 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001134 else if (mode == GL_TRIANGLE_FAN)
1135 {
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001136 drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances);
1137 }
1138 else if (instances > 0)
1139 {
1140 mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001141 }
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001142 else
1143 {
daniel@transgaming.com9f7ede62013-01-11 04:07:06 +00001144 mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex));
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001145 }
1146}
1147
1148void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer)
1149{
1150 // Get the raw indices for an indexed draw
1151 if (type != GL_NONE && elementArrayBuffer)
1152 {
1153 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001154 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001155 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001156 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001157 }
1158
1159 if (!mLineLoopIB)
1160 {
1161 mLineLoopIB = new StreamingIndexBufferInterface(this);
1162 if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1163 {
1164 delete mLineLoopIB;
1165 mLineLoopIB = NULL;
1166
1167 ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001168 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001169 }
1170 }
1171
1172 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
1173 if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1174 {
1175 ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001176 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001177 }
1178
1179 void* mappedMemory = NULL;
1180 int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory);
1181 if (offset == -1 || mappedMemory == NULL)
1182 {
1183 ERR("Could not map index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001184 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001185 }
1186
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001187 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001188 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001189
1190 switch (type)
1191 {
1192 case GL_NONE: // Non-indexed draw
1193 for (int i = 0; i < count; i++)
1194 {
1195 data[i] = i;
1196 }
1197 data[count] = 0;
1198 break;
1199 case GL_UNSIGNED_BYTE:
1200 for (int i = 0; i < count; i++)
1201 {
1202 data[i] = static_cast<const GLubyte*>(indices)[i];
1203 }
1204 data[count] = static_cast<const GLubyte*>(indices)[0];
1205 break;
1206 case GL_UNSIGNED_SHORT:
1207 for (int i = 0; i < count; i++)
1208 {
1209 data[i] = static_cast<const GLushort*>(indices)[i];
1210 }
1211 data[count] = static_cast<const GLushort*>(indices)[0];
1212 break;
1213 case GL_UNSIGNED_INT:
1214 for (int i = 0; i < count; i++)
1215 {
1216 data[i] = static_cast<const GLuint*>(indices)[i];
1217 }
1218 data[count] = static_cast<const GLuint*>(indices)[0];
1219 break;
1220 default: UNREACHABLE();
1221 }
1222
1223 if (!mLineLoopIB->unmapBuffer())
1224 {
1225 ERR("Could not unmap index buffer for GL_LINE_LOOP.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001226 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001227 }
1228
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001229 if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001230 {
1231 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer());
1232
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001233 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001234 mAppliedIBSerial = mLineLoopIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001235 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001236 mAppliedIBOffset = indexBufferOffset;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001237 }
1238
daniel@transgaming.com1ef09672013-01-11 04:10:37 +00001239 mDeviceContext->DrawIndexed(count + 1, 0, -minIndex);
daniel@transgaming.com91207b72012-11-28 20:56:43 +00001240}
1241
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001242void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances)
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001243{
1244 // Get the raw indices for an indexed draw
1245 if (type != GL_NONE && elementArrayBuffer)
1246 {
1247 gl::Buffer *indexBuffer = elementArrayBuffer;
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001248 BufferStorage *storage = indexBuffer->getStorage();
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001249 intptr_t offset = reinterpret_cast<intptr_t>(indices);
shannon.woods@transgaming.com76655412013-02-28 23:08:09 +00001250 indices = static_cast<const GLubyte*>(storage->getData()) + offset;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001251 }
1252
1253 if (!mTriangleFanIB)
1254 {
1255 mTriangleFanIB = new StreamingIndexBufferInterface(this);
1256 if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT))
1257 {
1258 delete mTriangleFanIB;
1259 mTriangleFanIB = NULL;
1260
1261 ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001262 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001263 }
1264 }
1265
1266 const int numTris = count - 2;
1267 const int spaceNeeded = (numTris * 3) * sizeof(unsigned int);
1268 if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT))
1269 {
1270 ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001271 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001272 }
1273
1274 void* mappedMemory = NULL;
1275 int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory);
1276 if (offset == -1 || mappedMemory == NULL)
1277 {
1278 ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001279 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001280 }
1281
1282 unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory);
1283 unsigned int indexBufferOffset = static_cast<unsigned int>(offset);
1284
1285 switch (type)
1286 {
1287 case GL_NONE: // Non-indexed draw
1288 for (int i = 0; i < numTris; i++)
1289 {
1290 data[i*3 + 0] = 0;
1291 data[i*3 + 1] = i + 1;
1292 data[i*3 + 2] = i + 2;
1293 }
1294 break;
1295 case GL_UNSIGNED_BYTE:
1296 for (int i = 0; i < numTris; i++)
1297 {
1298 data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0];
1299 data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1];
1300 data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2];
1301 }
1302 break;
1303 case GL_UNSIGNED_SHORT:
1304 for (int i = 0; i < numTris; i++)
1305 {
1306 data[i*3 + 0] = static_cast<const GLushort*>(indices)[0];
1307 data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1];
1308 data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2];
1309 }
1310 break;
1311 case GL_UNSIGNED_INT:
1312 for (int i = 0; i < numTris; i++)
1313 {
1314 data[i*3 + 0] = static_cast<const GLuint*>(indices)[0];
1315 data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1];
1316 data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2];
1317 }
1318 break;
1319 default: UNREACHABLE();
1320 }
1321
1322 if (!mTriangleFanIB->unmapBuffer())
1323 {
1324 ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001325 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001326 }
1327
1328 if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset)
1329 {
1330 IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer());
1331
1332 mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset);
1333 mAppliedIBSerial = mTriangleFanIB->getSerial();
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001334 mAppliedStorageIBSerial = 0;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001335 mAppliedIBOffset = indexBufferOffset;
1336 }
1337
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +00001338 if (instances > 0)
1339 {
1340 mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0);
1341 }
1342 else
1343 {
1344 mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex);
1345 }
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001346}
1347
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001348void Renderer11::applyShaders(gl::ProgramBinary *programBinary)
1349{
daniel@transgaming.come4991412012-12-20 20:55:34 +00001350 unsigned int programBinarySerial = programBinary->getSerial();
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001351 const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial);
1352
1353 if (updateProgramState)
daniel@transgaming.come4991412012-12-20 20:55:34 +00001354 {
1355 ShaderExecutable *vertexExe = programBinary->getVertexExecutable();
1356 ShaderExecutable *pixelExe = programBinary->getPixelExecutable();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001357
daniel@transgaming.come4991412012-12-20 20:55:34 +00001358 ID3D11VertexShader *vertexShader = NULL;
1359 if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001360
daniel@transgaming.come4991412012-12-20 20:55:34 +00001361 ID3D11PixelShader *pixelShader = NULL;
1362 if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader();
daniel@transgaming.comd4b2db22012-11-28 21:05:15 +00001363
daniel@transgaming.come4991412012-12-20 20:55:34 +00001364 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
1365 mDeviceContext->VSSetShader(vertexShader, NULL, 0);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001366
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001367 programBinary->dirtyAllUniforms();
1368
1369 mAppliedProgramBinarySerial = programBinarySerial;
1370 }
1371
1372 // Only use the geometry shader currently for point sprite drawing
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001373 const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode);
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001374
shannon.woods@transgaming.com43ccf3f2013-02-28 23:07:19 +00001375 if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive)
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001376 {
1377 if (usesGeometryShader)
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001378 {
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001379 ShaderExecutable *geometryExe = programBinary->getGeometryExecutable();
1380 ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001381 mDeviceContext->GSSetShader(geometryShader, NULL, 0);
1382 }
1383 else
1384 {
1385 mDeviceContext->GSSetShader(NULL, NULL, 0);
1386 }
1387
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +00001388 mIsGeometryShaderActive = usesGeometryShader;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001389 }
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +00001390}
1391
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001392void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001393{
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001394 ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
1395 ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable());
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001396
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001397 unsigned int totalRegisterCountVS = 0;
1398 unsigned int totalRegisterCountPS = 0;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001399
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001400 bool vertexUniformsDirty = false;
1401 bool pixelUniformsDirty = false;
1402
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001403 for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
1404 {
1405 const gl::Uniform *uniform = *uniform_iterator;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001406
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001407 if (uniform->isReferencedByVertexShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001408 {
1409 totalRegisterCountVS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001410 vertexUniformsDirty = vertexUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001411 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001412
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001413 if (uniform->isReferencedByFragmentShader())
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001414 {
1415 totalRegisterCountPS += uniform->registerCount;
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001416 pixelUniformsDirty = pixelUniformsDirty || uniform->dirty;
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001417 }
1418 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001419
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001420 ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS);
1421 ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS);
1422
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001423 float (*mapVS)[4] = NULL;
1424 float (*mapPS)[4] = NULL;
1425
1426 if (totalRegisterCountVS > 0 && vertexUniformsDirty)
1427 {
1428 D3D11_MAPPED_SUBRESOURCE map = {0};
1429 HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1430 ASSERT(SUCCEEDED(result));
1431 mapVS = (float(*)[4])map.pData;
1432 }
1433
1434 if (totalRegisterCountPS > 0 && pixelUniformsDirty)
1435 {
1436 D3D11_MAPPED_SUBRESOURCE map = {0};
1437 HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
1438 ASSERT(SUCCEEDED(result));
1439 mapPS = (float(*)[4])map.pData;
1440 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001441
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001442 for (gl::UniformArray::iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001443 {
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001444 gl::Uniform *uniform = *uniform_iterator;
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001445
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001446 if (uniform->type != GL_SAMPLER_2D && uniform->type != GL_SAMPLER_CUBE)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001447 {
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001448 if (uniform->isReferencedByVertexShader() && mapVS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001449 {
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001450 memcpy(mapVS + uniform->vsRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4]));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001451 }
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001452
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +00001453 if (uniform->isReferencedByFragmentShader() && mapPS)
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001454 {
shannon.woods@transgaming.com13979a62013-02-28 23:10:18 +00001455 memcpy(mapPS + uniform->psRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4]));
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001456 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001457 }
shannon.woods@transgaming.com21ba6472013-01-25 21:53:32 +00001458
1459 uniform->dirty = false;
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001460 }
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001461
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001462 if (mapVS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001463 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001464 mDeviceContext->Unmap(vertexConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001465 }
1466
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001467 if (mapPS)
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001468 {
shannon.woods@transgaming.com09bf2a72013-02-28 23:12:54 +00001469 mDeviceContext->Unmap(pixelConstantBuffer, 0);
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001470 }
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001471
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001472 mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer);
1473 mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001474
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001475 // Driver uniforms
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001476 if (!mDriverConstantBufferVS)
1477 {
1478 D3D11_BUFFER_DESC constantBufferDescription = {0};
1479 constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants);
1480 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1481 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1482 constantBufferDescription.CPUAccessFlags = 0;
1483 constantBufferDescription.MiscFlags = 0;
1484 constantBufferDescription.StructureByteStride = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00001485
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001486 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001487 ASSERT(SUCCEEDED(result));
daniel@transgaming.com873f28a2012-12-20 21:12:42 +00001488
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001489 mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS);
1490 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001491
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001492 if (!mDriverConstantBufferPS)
1493 {
1494 D3D11_BUFFER_DESC constantBufferDescription = {0};
1495 constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants);
1496 constantBufferDescription.Usage = D3D11_USAGE_DEFAULT;
1497 constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1498 constantBufferDescription.CPUAccessFlags = 0;
1499 constantBufferDescription.MiscFlags = 0;
1500 constantBufferDescription.StructureByteStride = 0;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001501
shannon.woods@transgaming.com5929ef22013-01-25 21:53:24 +00001502 HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS);
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001503 ASSERT(SUCCEEDED(result));
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001504
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001505 mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS);
1506 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001507
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001508 if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0)
1509 {
1510 mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0);
1511 memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants));
1512 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +00001513
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001514 if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0)
1515 {
1516 mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0);
1517 memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants));
1518 }
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001519
1520 // needed for the point sprite geometry shader
1521 mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS);
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001522}
1523
daniel@transgaming.com084a2572012-11-28 20:55:17 +00001524void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer)
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001525{
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001526 bool alphaUnmasked = gl::GetAlphaBits(mRenderTargetDesc.format, getCurrentClientVersion()) == 0 ||
1527 clearParams.colorMaskAlpha;
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001528 bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) &&
1529 !(clearParams.colorMaskRed && clearParams.colorMaskGreen &&
1530 clearParams.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001531
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001532 unsigned int stencilUnmasked = 0x0;
1533 if (frameBuffer->hasStencil())
1534 {
shannonwoods@chromium.org755012f2013-05-30 00:09:32 +00001535 unsigned int stencilSize = gl::GetStencilBits(frameBuffer->getStencilbuffer()->getActualFormat(),
1536 getCurrentClientVersion());
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001537 stencilUnmasked = (0x1 << stencilSize) - 1;
1538 }
1539 bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) &&
1540 (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001541
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001542 bool needScissoredClear = mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 ||
1543 mCurScissor.x + mCurScissor.width < mRenderTargetDesc.width ||
1544 mCurScissor.y + mCurScissor.height < mRenderTargetDesc.height);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001545
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001546 if (needMaskedColorClear || needMaskedStencilClear || needScissoredClear)
1547 {
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001548 maskedClear(clearParams, frameBuffer->usingExtendedDrawBuffers());
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001549 }
1550 else
1551 {
1552 if (clearParams.mask & GL_COLOR_BUFFER_BIT)
1553 {
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001554 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001555 {
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001556 if (frameBuffer->isEnabledColorAttachment(colorAttachment))
1557 {
1558 gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer(colorAttachment);
1559 if (renderbufferObject)
1560 {
1561 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget());
1562 if (!renderTarget)
1563 {
1564 ERR("render target pointer unexpectedly null.");
1565 return;
1566 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001567
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001568 ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView();
1569 if (!framebufferRTV)
1570 {
1571 ERR("render target view pointer unexpectedly null.");
1572 return;
1573 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001574
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001575 const float clearValues[4] = { clearParams.colorClearValue.red,
1576 clearParams.colorClearValue.green,
1577 clearParams.colorClearValue.blue,
1578 clearParams.colorClearValue.alpha };
1579 mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001580
shannon.woods%transgaming.com@gtempaccount.comdae24092013-04-13 03:31:31 +00001581 framebufferRTV->Release();
1582 }
1583 }
1584 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001585 }
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001586 if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001587 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001588 gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer();
1589 if (renderbufferObject)
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001590 {
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001591 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil());
1592 if (!renderTarget)
1593 {
1594 ERR("render target pointer unexpectedly null.");
1595 return;
1596 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001597
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001598 ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView();
1599 if (!framebufferDSV)
1600 {
1601 ERR("depth stencil view pointer unexpectedly null.");
1602 return;
1603 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001604
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001605 UINT clearFlags = 0;
1606 if (clearParams.mask & GL_DEPTH_BUFFER_BIT)
1607 {
1608 clearFlags |= D3D11_CLEAR_DEPTH;
1609 }
1610 if (clearParams.mask & GL_STENCIL_BUFFER_BIT)
1611 {
1612 clearFlags |= D3D11_CLEAR_STENCIL;
1613 }
1614
shannon.woods@transgaming.combe211b32013-02-28 23:17:16 +00001615 float depthClear = gl::clamp01(clearParams.depthClearValue);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001616 UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF;
1617
1618 mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear);
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001619
shannon.woods@transgaming.com81c5ef92013-01-25 21:52:08 +00001620 framebufferDSV->Release();
1621 }
daniel@transgaming.com54e67542012-11-28 21:02:31 +00001622 }
1623 }
daniel@transgaming.comd084c622012-11-28 19:36:05 +00001624}
1625
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001626void Renderer11::maskedClear(const gl::ClearParameters &clearParams, bool usingExtendedDrawBuffers)
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001627{
1628 HRESULT result;
1629
1630 if (!mClearResourcesInitialized)
1631 {
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001632 ASSERT(!mClearVB && !mClearVS && !mClearSinglePS && !mClearMultiplePS && !mClearScissorRS && !mClearNoScissorRS);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001633
1634 D3D11_BUFFER_DESC vbDesc;
1635 vbDesc.ByteWidth = sizeof(d3d11::PositionDepthColorVertex) * 4;
1636 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
1637 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
1638 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1639 vbDesc.MiscFlags = 0;
1640 vbDesc.StructureByteStride = 0;
1641
1642 result = mDevice->CreateBuffer(&vbDesc, NULL, &mClearVB);
1643 ASSERT(SUCCEEDED(result));
1644 d3d11::SetDebugName(mClearVB, "Renderer11 masked clear vertex buffer");
1645
1646 D3D11_INPUT_ELEMENT_DESC quadLayout[] =
1647 {
1648 { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1649 { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
1650 };
1651
1652 result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Clear, sizeof(g_VS_Clear), &mClearIL);
1653 ASSERT(SUCCEEDED(result));
1654 d3d11::SetDebugName(mClearIL, "Renderer11 masked clear input layout");
1655
1656 result = mDevice->CreateVertexShader(g_VS_Clear, sizeof(g_VS_Clear), NULL, &mClearVS);
1657 ASSERT(SUCCEEDED(result));
1658 d3d11::SetDebugName(mClearVS, "Renderer11 masked clear vertex shader");
1659
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001660 result = mDevice->CreatePixelShader(g_PS_ClearSingle, sizeof(g_PS_ClearSingle), NULL, &mClearSinglePS);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001661 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001662 d3d11::SetDebugName(mClearSinglePS, "Renderer11 masked clear pixel shader (1 RT)");
1663
1664 result = mDevice->CreatePixelShader(g_PS_ClearMultiple, sizeof(g_PS_ClearMultiple), NULL, &mClearMultiplePS);
1665 ASSERT(SUCCEEDED(result));
1666 d3d11::SetDebugName(mClearMultiplePS, "Renderer11 masked clear pixel shader (MRT)");
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001667
1668 D3D11_RASTERIZER_DESC rsScissorDesc;
1669 rsScissorDesc.FillMode = D3D11_FILL_SOLID;
1670 rsScissorDesc.CullMode = D3D11_CULL_NONE;
1671 rsScissorDesc.FrontCounterClockwise = FALSE;
1672 rsScissorDesc.DepthBias = 0;
1673 rsScissorDesc.DepthBiasClamp = 0.0f;
1674 rsScissorDesc.SlopeScaledDepthBias = 0.0f;
1675 rsScissorDesc.DepthClipEnable = FALSE;
1676 rsScissorDesc.ScissorEnable = TRUE;
1677 rsScissorDesc.MultisampleEnable = FALSE;
1678 rsScissorDesc.AntialiasedLineEnable = FALSE;
1679
1680 result = mDevice->CreateRasterizerState(&rsScissorDesc, &mClearScissorRS);
1681 ASSERT(SUCCEEDED(result));
1682 d3d11::SetDebugName(mClearScissorRS, "Renderer11 masked clear scissor rasterizer state");
1683
1684 D3D11_RASTERIZER_DESC rsNoScissorDesc;
1685 rsNoScissorDesc.FillMode = D3D11_FILL_SOLID;
1686 rsNoScissorDesc.CullMode = D3D11_CULL_NONE;
1687 rsNoScissorDesc.FrontCounterClockwise = FALSE;
1688 rsNoScissorDesc.DepthBias = 0;
1689 rsNoScissorDesc.DepthBiasClamp = 0.0f;
1690 rsNoScissorDesc.SlopeScaledDepthBias = 0.0f;
1691 rsNoScissorDesc.DepthClipEnable = FALSE;
1692 rsNoScissorDesc.ScissorEnable = FALSE;
1693 rsNoScissorDesc.MultisampleEnable = FALSE;
1694 rsNoScissorDesc.AntialiasedLineEnable = FALSE;
1695
1696 result = mDevice->CreateRasterizerState(&rsNoScissorDesc, &mClearNoScissorRS);
1697 ASSERT(SUCCEEDED(result));
1698 d3d11::SetDebugName(mClearNoScissorRS, "Renderer11 masked clear no scissor rasterizer state");
1699
1700 mClearResourcesInitialized = true;
1701 }
1702
1703 // Prepare the depth stencil state to write depth values if the depth should be cleared
1704 // and stencil values if the stencil should be cleared
1705 gl::DepthStencilState glDSState;
1706 glDSState.depthTest = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1707 glDSState.depthFunc = GL_ALWAYS;
1708 glDSState.depthMask = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0;
1709 glDSState.stencilTest = (clearParams.mask & GL_STENCIL_BUFFER_BIT) != 0;
1710 glDSState.stencilFunc = GL_ALWAYS;
1711 glDSState.stencilMask = 0;
1712 glDSState.stencilFail = GL_REPLACE;
1713 glDSState.stencilPassDepthFail = GL_REPLACE;
1714 glDSState.stencilPassDepthPass = GL_REPLACE;
1715 glDSState.stencilWritemask = clearParams.stencilWriteMask;
1716 glDSState.stencilBackFunc = GL_ALWAYS;
1717 glDSState.stencilBackMask = 0;
1718 glDSState.stencilBackFail = GL_REPLACE;
1719 glDSState.stencilBackPassDepthFail = GL_REPLACE;
1720 glDSState.stencilBackPassDepthPass = GL_REPLACE;
1721 glDSState.stencilBackWritemask = clearParams.stencilWriteMask;
1722
1723 int stencilClear = clearParams.stencilClearValue & 0x000000FF;
1724
1725 ID3D11DepthStencilState *dsState = mStateCache.getDepthStencilState(glDSState);
1726
1727 // Prepare the blend state to use a write mask if the color buffer should be cleared
1728 gl::BlendState glBlendState;
1729 glBlendState.blend = false;
1730 glBlendState.sourceBlendRGB = GL_ONE;
1731 glBlendState.destBlendRGB = GL_ZERO;
1732 glBlendState.sourceBlendAlpha = GL_ONE;
1733 glBlendState.destBlendAlpha = GL_ZERO;
1734 glBlendState.blendEquationRGB = GL_FUNC_ADD;
1735 glBlendState.blendEquationAlpha = GL_FUNC_ADD;
1736 glBlendState.colorMaskRed = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskRed : false;
1737 glBlendState.colorMaskGreen = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskGreen : false;
1738 glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false;
shannon.woods@transgaming.com50ea9932013-02-28 23:13:40 +00001739 glBlendState.colorMaskAlpha = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskAlpha : false;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001740 glBlendState.sampleAlphaToCoverage = false;
1741 glBlendState.dither = false;
1742
1743 static const float blendFactors[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
1744 static const UINT sampleMask = 0xFFFFFFFF;
1745
1746 ID3D11BlendState *blendState = mStateCache.getBlendState(glBlendState);
1747
1748 // Set the vertices
1749 D3D11_MAPPED_SUBRESOURCE mappedResource;
1750 result = mDeviceContext->Map(mClearVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
1751 if (FAILED(result))
1752 {
1753 ERR("Failed to map masked clear vertex buffer, HRESULT: 0x%X.", result);
1754 return;
1755 }
1756
1757 d3d11::PositionDepthColorVertex *vertices = reinterpret_cast<d3d11::PositionDepthColorVertex*>(mappedResource.pData);
1758
1759 float depthClear = gl::clamp01(clearParams.depthClearValue);
1760 d3d11::SetPositionDepthColorVertex(&vertices[0], -1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1761 d3d11::SetPositionDepthColorVertex(&vertices[1], -1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1762 d3d11::SetPositionDepthColorVertex(&vertices[2], 1.0f, 1.0f, depthClear, clearParams.colorClearValue);
1763 d3d11::SetPositionDepthColorVertex(&vertices[3], 1.0f, -1.0f, depthClear, clearParams.colorClearValue);
1764
1765 mDeviceContext->Unmap(mClearVB, 0);
1766
1767 // Apply state
1768 mDeviceContext->OMSetBlendState(blendState, blendFactors, sampleMask);
1769 mDeviceContext->OMSetDepthStencilState(dsState, stencilClear);
1770 mDeviceContext->RSSetState(mScissorEnabled ? mClearScissorRS : mClearNoScissorRS);
1771
1772 // Apply shaders
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001773 ID3D11PixelShader *pixelShader = usingExtendedDrawBuffers ? mClearMultiplePS : mClearSinglePS;
1774
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001775 mDeviceContext->IASetInputLayout(mClearIL);
1776 mDeviceContext->VSSetShader(mClearVS, NULL, 0);
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001777 mDeviceContext->PSSetShader(pixelShader, NULL, 0);
shannon.woods@transgaming.com2c53e472013-02-28 23:13:56 +00001778 mDeviceContext->GSSetShader(NULL, NULL, 0);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001779
1780 // Apply vertex buffer
1781 static UINT stride = sizeof(d3d11::PositionDepthColorVertex);
1782 static UINT startIdx = 0;
1783 mDeviceContext->IASetVertexBuffers(0, 1, &mClearVB, &stride, &startIdx);
1784 mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
1785
1786 // Draw the clear quad
1787 mDeviceContext->Draw(4, 0);
1788
1789 // Clean up
1790 markAllStateDirty();
1791}
1792
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001793void Renderer11::markAllStateDirty()
1794{
shannon.woods%transgaming.com@gtempaccount.comf4fe7102013-04-13 03:30:26 +00001795 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
1796 {
1797 mAppliedRenderTargetSerials[rtIndex] = 0;
1798 }
daniel@transgaming.com9a067372012-12-20 20:55:24 +00001799 mAppliedDepthbufferSerial = 0;
1800 mAppliedStencilbufferSerial = 0;
daniel@transgaming.com7b6b83e2012-11-28 21:00:30 +00001801 mDepthStencilInitialized = false;
1802 mRenderTargetDescInitialized = false;
1803
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001804 for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++)
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001805 {
1806 mForceSetVertexSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001807 mCurVertexTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001808 }
1809 for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++)
1810 {
1811 mForceSetPixelSamplerStates[i] = true;
daniel@transgaming.come33c8bf2013-01-11 04:11:33 +00001812 mCurPixelTextureSerials[i] = 0;
daniel@transgaming.com54de24f2013-01-11 04:07:59 +00001813 }
1814
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001815 mForceSetBlendState = true;
1816 mForceSetRasterState = true;
1817 mForceSetDepthStencilState = true;
1818 mForceSetScissor = true;
daniel@transgaming.com53670042012-11-28 20:55:51 +00001819 mForceSetViewport = true;
daniel@transgaming.come4991412012-12-20 20:55:34 +00001820
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001821 mAppliedIBSerial = 0;
shannon.woods@transgaming.coma1229a32013-02-28 23:08:40 +00001822 mAppliedStorageIBSerial = 0;
daniel@transgaming.com7fbf4862013-01-11 04:09:22 +00001823 mAppliedIBOffset = 0;
1824
daniel@transgaming.come4991412012-12-20 20:55:34 +00001825 mAppliedProgramBinarySerial = 0;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001826 memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants));
1827 memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants));
daniel@transgaming.comc43a6052012-11-28 19:41:51 +00001828}
1829
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001830void Renderer11::releaseDeviceResources()
1831{
daniel@transgaming.comf8ba1092012-11-28 19:37:53 +00001832 mStateCache.clear();
daniel@transgaming.comc5431eb2012-12-20 21:10:15 +00001833 mInputLayoutCache.clear();
1834
1835 delete mVertexDataManager;
1836 mVertexDataManager = NULL;
1837
1838 delete mIndexDataManager;
1839 mIndexDataManager = NULL;
daniel@transgaming.comc5114302012-12-20 21:11:36 +00001840
1841 delete mLineLoopIB;
1842 mLineLoopIB = NULL;
daniel@transgaming.com4fd1f982013-01-11 04:12:58 +00001843
1844 delete mTriangleFanIB;
1845 mTriangleFanIB = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001846
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001847 SafeRelease(mCopyVB);
1848 SafeRelease(mCopySampler);
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00001849
1850 SafeRelease(mCopy2DIL);
1851 SafeRelease(mCopy2DVS);
1852 SafeRelease(mCopyRGBA2DPS);
1853 SafeRelease(mCopyRGB2DPS);
1854 SafeRelease(mCopyLum2DPS);
1855 SafeRelease(mCopyLumAlpha2DPS);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00001856 SafeRelease(mCopy3DIL);
1857 SafeRelease(mCopy3DVS);
1858 SafeRelease(mCopy3DGS);
1859 SafeRelease(mCopyRGBA3DPS);
1860 SafeRelease(mCopyRGB3DPS);
1861 SafeRelease(mCopyLum3DPS);
1862 SafeRelease(mCopyLumAlpha3DPS);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00001863 mCopyResourcesInitialized = false;
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001864
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001865 SafeRelease(mClearVB);
1866 SafeRelease(mClearIL);
1867 SafeRelease(mClearVS);
shannonwoods@chromium.org98949842013-05-30 00:01:51 +00001868 SafeRelease(mClearSinglePS);
1869 SafeRelease(mClearMultiplePS);
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001870 SafeRelease(mClearScissorRS);
1871 SafeRelease(mClearNoScissorRS);
shannon.woods@transgaming.com34f507c2013-01-25 21:52:15 +00001872
1873 mClearResourcesInitialized = false;
shannon.woods@transgaming.com5fb979d2013-01-25 21:53:04 +00001874
shannonwoods@chromium.org894b3242013-05-30 00:01:44 +00001875 SafeRelease(mDriverConstantBufferVS);
1876 SafeRelease(mDriverConstantBufferPS);
1877 SafeRelease(mSyncQuery);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001878}
1879
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001880void Renderer11::notifyDeviceLost()
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001881{
1882 mDeviceLost = true;
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001883 mDisplay->notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001884}
1885
1886bool Renderer11::isDeviceLost()
1887{
1888 return mDeviceLost;
1889}
1890
1891// set notify to true to broadcast a message to all contexts of the device loss
1892bool Renderer11::testDeviceLost(bool notify)
1893{
1894 bool isLost = false;
1895
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001896 // GetRemovedReason is used to test if the device is removed
1897 HRESULT result = mDevice->GetDeviceRemovedReason();
1898 isLost = d3d11::isDeviceLostError(result);
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001899
1900 if (isLost)
1901 {
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001902 // Log error if this is a new device lost event
1903 if (mDeviceLost == false)
1904 {
1905 ERR("The D3D11 device was removed: 0x%08X", result);
1906 }
1907
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001908 // ensure we note the device loss --
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00001909 // we'll probably get this done again by notifyDeviceLost
1910 // but best to remember it!
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001911 // Note that we don't want to clear the device loss status here
1912 // -- this needs to be done by resetDevice
1913 mDeviceLost = true;
1914 if (notify)
1915 {
shannon.woods@transgaming.comeb049e22013-02-28 23:04:49 +00001916 notifyDeviceLost();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001917 }
1918 }
1919
1920 return isLost;
1921}
1922
1923bool Renderer11::testDeviceResettable()
1924{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001925 // determine if the device is resettable by creating a dummy device
1926 PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001927
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001928 if (D3D11CreateDevice == NULL)
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001929 {
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00001930 return false;
1931 }
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001932
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001933 D3D_FEATURE_LEVEL featureLevels[] =
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001934 {
1935 D3D_FEATURE_LEVEL_11_0,
1936 D3D_FEATURE_LEVEL_10_1,
1937 D3D_FEATURE_LEVEL_10_0,
1938 };
1939
1940 ID3D11Device* dummyDevice;
1941 D3D_FEATURE_LEVEL dummyFeatureLevel;
1942 ID3D11DeviceContext* dummyContext;
1943
1944 HRESULT result = D3D11CreateDevice(NULL,
1945 D3D_DRIVER_TYPE_HARDWARE,
1946 NULL,
1947 #if defined(_DEBUG)
1948 D3D11_CREATE_DEVICE_DEBUG,
1949 #else
1950 0,
1951 #endif
shannon.woods@transgaming.comd438fd42013-02-28 23:17:45 +00001952 featureLevels,
1953 ArraySize(featureLevels),
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00001954 D3D11_SDK_VERSION,
1955 &dummyDevice,
1956 &dummyFeatureLevel,
1957 &dummyContext);
1958
1959 if (!mDevice || FAILED(result))
1960 {
1961 return false;
1962 }
1963
1964 dummyContext->Release();
1965 dummyDevice->Release();
1966
1967 return true;
1968}
1969
1970void Renderer11::release()
1971{
1972 releaseDeviceResources();
1973
1974 if (mDxgiFactory)
1975 {
1976 mDxgiFactory->Release();
1977 mDxgiFactory = NULL;
1978 }
1979
1980 if (mDxgiAdapter)
1981 {
1982 mDxgiAdapter->Release();
1983 mDxgiAdapter = NULL;
1984 }
1985
1986 if (mDeviceContext)
1987 {
1988 mDeviceContext->ClearState();
1989 mDeviceContext->Flush();
1990 mDeviceContext->Release();
1991 mDeviceContext = NULL;
1992 }
1993
1994 if (mDevice)
1995 {
1996 mDevice->Release();
1997 mDevice = NULL;
1998 }
1999
2000 if (mD3d11Module)
2001 {
2002 FreeLibrary(mD3d11Module);
2003 mD3d11Module = NULL;
2004 }
2005
2006 if (mDxgiModule)
2007 {
2008 FreeLibrary(mDxgiModule);
2009 mDxgiModule = NULL;
2010 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002011}
2012
2013bool Renderer11::resetDevice()
2014{
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00002015 // recreate everything
2016 release();
2017 EGLint result = initialize();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002018
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +00002019 if (result != EGL_SUCCESS)
2020 {
2021 ERR("Could not reinitialize D3D11 device: %08X", result);
2022 return false;
2023 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002024
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002025 mDeviceLost = false;
2026
2027 return true;
2028}
2029
2030DWORD Renderer11::getAdapterVendor() const
2031{
daniel@transgaming.com1f811f52012-11-28 20:57:39 +00002032 return mAdapterDescription.VendorId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002033}
2034
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002035std::string Renderer11::getRendererDescription() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002036{
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002037 std::ostringstream rendererString;
2038
2039 rendererString << mDescription;
2040 rendererString << " Direct3D11";
2041
2042 rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel();
2043 rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel();
2044
2045 return rendererString.str();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002046}
2047
2048GUID Renderer11::getAdapterIdentifier() const
2049{
shannon.woods@transgaming.com43db7952013-02-28 23:04:28 +00002050 // Use the adapter LUID as our adapter ID
2051 // This number is local to a machine is only guaranteed to be unique between restarts
2052 META_ASSERT(sizeof(LUID) <= sizeof(GUID));
2053 GUID adapterId = {0};
2054 memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID));
2055 return adapterId;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002056}
2057
shannon.woods@transgaming.combec04bf2013-01-25 21:53:52 +00002058bool Renderer11::getBGRATextureSupport() const
2059{
2060 return mBGRATextureSupport;
2061}
2062
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002063bool Renderer11::getDXT1TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002064{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002065 return mDXT1TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002066}
2067
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002068bool Renderer11::getDXT3TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002069{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002070 return mDXT3TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002071}
2072
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002073bool Renderer11::getDXT5TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002074{
shannon.woods@transgaming.com09f326b2013-02-28 23:13:34 +00002075 return mDXT5TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002076}
2077
2078bool Renderer11::getDepthTextureSupport() const
2079{
shannon.woods@transgaming.comcf103f32013-02-28 23:18:45 +00002080 return mDepthTextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002081}
2082
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002083bool Renderer11::getFloat32TextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002084{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00002085 return mFloat32TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002086}
2087
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002088bool Renderer11::getFloat32TextureFilteringSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002089{
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002090 return mFloat32FilterSupport;
2091}
2092
2093bool Renderer11::getFloat32TextureRenderingSupport() const
2094{
2095 return mFloat32RenderSupport;
2096}
2097
2098bool Renderer11::getFloat16TextureSupport() const
2099{
shannon.woods@transgaming.com9cdced62013-02-28 23:07:31 +00002100 return mFloat16TextureSupport;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002101}
2102
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002103bool Renderer11::getFloat16TextureFilteringSupport() const
2104{
2105 return mFloat16FilterSupport;
2106}
2107
2108bool Renderer11::getFloat16TextureRenderingSupport() const
2109{
2110 return mFloat16RenderSupport;
2111}
2112
2113bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002114{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002115 return false;
2116}
2117
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002118bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002119{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002120 return false;
2121}
2122
2123bool Renderer11::getTextureFilterAnisotropySupport() const
2124{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002125 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002126}
2127
2128float Renderer11::getTextureMaxAnisotropy() const
2129{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002130 switch (mFeatureLevel)
2131 {
2132 case D3D_FEATURE_LEVEL_11_0:
2133 return D3D11_MAX_MAXANISOTROPY;
2134 case D3D_FEATURE_LEVEL_10_1:
2135 case D3D_FEATURE_LEVEL_10_0:
2136 return D3D10_MAX_MAXANISOTROPY;
2137 default: UNREACHABLE();
2138 return 0;
2139 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002140}
2141
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002142bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002143{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002144 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002145}
2146
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002147Range Renderer11::getViewportBounds() const
2148{
2149 switch (mFeatureLevel)
2150 {
2151 case D3D_FEATURE_LEVEL_11_0:
2152 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
2153 case D3D_FEATURE_LEVEL_10_1:
2154 case D3D_FEATURE_LEVEL_10_0:
2155 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
2156 default: UNREACHABLE();
2157 return Range(0, 0);
2158 }
2159}
2160
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002161unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002162{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002163 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2164 switch (mFeatureLevel)
2165 {
2166 case D3D_FEATURE_LEVEL_11_0:
2167 case D3D_FEATURE_LEVEL_10_1:
2168 case D3D_FEATURE_LEVEL_10_0:
2169 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
2170 default: UNREACHABLE();
2171 return 0;
2172 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002173}
2174
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002175unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
2176{
2177 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2178}
2179
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002180unsigned int Renderer11::getReservedVertexUniformVectors() const
2181{
2182 return 0; // Driver uniforms are stored in a separate constant buffer
2183}
2184
2185unsigned int Renderer11::getReservedFragmentUniformVectors() const
2186{
2187 return 0; // Driver uniforms are stored in a separate constant buffer
2188}
2189
2190unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002191{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002192 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2193 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2194 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002195}
2196
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002197unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002198{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002199 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2200 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2201 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002202}
2203
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002204unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002205{
2206 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00002207 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
2208 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002209 switch (mFeatureLevel)
2210 {
2211 case D3D_FEATURE_LEVEL_11_0:
2212 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2213 case D3D_FEATURE_LEVEL_10_1:
2214 case D3D_FEATURE_LEVEL_10_0:
2215 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2216 default: UNREACHABLE();
2217 return 0;
2218 }
2219}
2220
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002221unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2222{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002223 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2224 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2225
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002226 switch (mFeatureLevel)
2227 {
2228 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002229 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002230 case D3D_FEATURE_LEVEL_10_1:
2231 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002232 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002233 default: UNREACHABLE();
2234 return 0;
2235 }
2236}
2237
2238unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2239{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002240 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2241 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2242
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002243 switch (mFeatureLevel)
2244 {
2245 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002246 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002247 case D3D_FEATURE_LEVEL_10_1:
2248 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002249 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002250 default: UNREACHABLE();
2251 return 0;
2252 }
2253}
2254
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002255unsigned int Renderer11::getReservedVertexUniformBuffers() const
2256{
2257 // we reserve one buffer for the application uniforms, and one for driver uniforms
2258 return 2;
2259}
2260
2261unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2262{
2263 // we reserve one buffer for the application uniforms, and one for driver uniforms
2264 return 2;
2265}
2266
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002267unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2268{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002269 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2270 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2271
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002272 switch (mFeatureLevel)
2273 {
2274 case D3D_FEATURE_LEVEL_11_0:
2275 return D3D11_SO_BUFFER_SLOT_COUNT;
2276 case D3D_FEATURE_LEVEL_10_1:
2277 case D3D_FEATURE_LEVEL_10_0:
2278 return D3D10_SO_BUFFER_SLOT_COUNT;
2279 default: UNREACHABLE();
2280 return 0;
2281 }
2282}
2283
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002284unsigned int Renderer11::getMaxUniformBufferSize() const
2285{
2286 // Each component is a 4-element vector of 4-byte units (floats)
2287 const unsigned int bytesPerComponent = 4 * sizeof(float);
2288
2289 switch (mFeatureLevel)
2290 {
2291 case D3D_FEATURE_LEVEL_11_0:
2292 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2293 case D3D_FEATURE_LEVEL_10_1:
2294 case D3D_FEATURE_LEVEL_10_0:
2295 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2296 default: UNREACHABLE();
2297 return 0;
2298 }
2299}
2300
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002301bool Renderer11::getNonPower2TextureSupport() const
2302{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002303 switch (mFeatureLevel)
2304 {
2305 case D3D_FEATURE_LEVEL_11_0:
2306 case D3D_FEATURE_LEVEL_10_1:
2307 case D3D_FEATURE_LEVEL_10_0:
2308 return true;
2309 default: UNREACHABLE();
2310 return false;
2311 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002312}
2313
2314bool Renderer11::getOcclusionQuerySupport() const
2315{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002316 switch (mFeatureLevel)
2317 {
2318 case D3D_FEATURE_LEVEL_11_0:
2319 case D3D_FEATURE_LEVEL_10_1:
2320 case D3D_FEATURE_LEVEL_10_0:
2321 return true;
2322 default: UNREACHABLE();
2323 return false;
2324 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002325}
2326
2327bool Renderer11::getInstancingSupport() const
2328{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002329 switch (mFeatureLevel)
2330 {
2331 case D3D_FEATURE_LEVEL_11_0:
2332 case D3D_FEATURE_LEVEL_10_1:
2333 case D3D_FEATURE_LEVEL_10_0:
2334 return true;
2335 default: UNREACHABLE();
2336 return false;
2337 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002338}
2339
2340bool Renderer11::getShareHandleSupport() const
2341{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002342 // We only currently support share handles with BGRA surfaces, because
2343 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002344 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002345 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002346}
2347
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002348bool Renderer11::getDerivativeInstructionSupport() const
2349{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002350 switch (mFeatureLevel)
2351 {
2352 case D3D_FEATURE_LEVEL_11_0:
2353 case D3D_FEATURE_LEVEL_10_1:
2354 case D3D_FEATURE_LEVEL_10_0:
2355 return true;
2356 default: UNREACHABLE();
2357 return false;
2358 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002359}
2360
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002361bool Renderer11::getPostSubBufferSupport() const
2362{
2363 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2364 return false;
2365}
2366
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002367int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002368{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002369 switch (mFeatureLevel)
2370 {
2371 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002372 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002373 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2374 default: UNREACHABLE(); return 0;
2375 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002376}
2377
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002378int Renderer11::getMinorShaderModel() const
2379{
2380 switch (mFeatureLevel)
2381 {
2382 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2383 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2384 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2385 default: UNREACHABLE(); return 0;
2386 }
2387}
2388
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002389float Renderer11::getMaxPointSize() const
2390{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002391 // choose a reasonable maximum. we enforce this in the shader.
2392 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2393 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002394}
2395
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002396int Renderer11::getMaxViewportDimension() const
2397{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002398 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2399 // In our case return the maximum texture size, which is the maximum render buffer size.
2400 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2401 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2402
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002403 switch (mFeatureLevel)
2404 {
2405 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002406 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002407 case D3D_FEATURE_LEVEL_10_1:
2408 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002409 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002410 default: UNREACHABLE();
2411 return 0;
2412 }
2413}
2414
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002415int Renderer11::getMaxTextureWidth() const
2416{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002417 switch (mFeatureLevel)
2418 {
2419 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2420 case D3D_FEATURE_LEVEL_10_1:
2421 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2422 default: UNREACHABLE(); return 0;
2423 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002424}
2425
2426int Renderer11::getMaxTextureHeight() const
2427{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002428 switch (mFeatureLevel)
2429 {
2430 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2431 case D3D_FEATURE_LEVEL_10_1:
2432 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2433 default: UNREACHABLE(); return 0;
2434 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002435}
2436
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002437int Renderer11::getMaxTextureDepth() const
2438{
2439 switch (mFeatureLevel)
2440 {
2441 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2442 case D3D_FEATURE_LEVEL_10_1:
2443 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2444 default: UNREACHABLE(); return 0;
2445 }
2446}
2447
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002448int Renderer11::getMaxTextureArrayLayers() const
2449{
2450 switch (mFeatureLevel)
2451 {
2452 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2453 case D3D_FEATURE_LEVEL_10_1:
2454 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2455 default: UNREACHABLE(); return 0;
2456 }
2457}
2458
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002459bool Renderer11::get32BitIndexSupport() const
2460{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002461 switch (mFeatureLevel)
2462 {
2463 case D3D_FEATURE_LEVEL_11_0:
2464 case D3D_FEATURE_LEVEL_10_1:
2465 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2466 default: UNREACHABLE(); return false;
2467 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002468}
2469
2470int Renderer11::getMinSwapInterval() const
2471{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002472 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002473}
2474
2475int Renderer11::getMaxSwapInterval() const
2476{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002477 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002478}
2479
2480int Renderer11::getMaxSupportedSamples() const
2481{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002482 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002483}
2484
Geoff Lang0e120e32013-05-29 10:23:55 -04002485GLsizei Renderer11::getMaxSupportedFormatSamples(GLint internalFormat) const
2486{
2487 DXGI_FORMAT format = gl_d3d11::GetTexFormat(internalFormat, getCurrentClientVersion());
2488 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2489 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2490}
2491
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002492int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2493{
2494 if (requested == 0)
2495 {
2496 return 0;
2497 }
2498
2499 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2500 if (iter != mMultisampleSupportMap.end())
2501 {
2502 const MultisampleSupportInfo& info = iter->second;
2503 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2504 {
2505 if (info.qualityLevels[i] > 0)
2506 {
2507 return i + 1;
2508 }
2509 }
2510 }
2511
2512 return -1;
2513}
2514
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002515unsigned int Renderer11::getMaxRenderTargets() const
2516{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002517 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2518 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2519
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002520 switch (mFeatureLevel)
2521 {
2522 case D3D_FEATURE_LEVEL_11_0:
2523 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2524 case D3D_FEATURE_LEVEL_10_1:
2525 case D3D_FEATURE_LEVEL_10_0:
2526 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2527 default:
2528 UNREACHABLE();
2529 return 1;
2530 }
2531}
2532
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002533bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002534{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002535 if (source && dest)
2536 {
2537 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2538 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2539
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002540 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002541 return true;
2542 }
2543
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002544 return false;
2545}
2546
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002547bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002548{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002549 if (source && dest)
2550 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002551 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2552 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002553
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002554 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002555 return true;
2556 }
2557
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002558 return false;
2559}
2560
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002561bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2562{
2563 if (source && dest)
2564 {
2565 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2566 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2567
2568 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2569 return true;
2570 }
2571
2572 return false;
2573}
2574
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002575bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2576{
2577 if (source && dest)
2578 {
2579 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2580 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2581
2582 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2583 return true;
2584 }
2585
2586 return false;
2587}
2588
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002589bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002590 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002591{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002592 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002593 if (!colorbuffer)
2594 {
2595 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002596 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002597 }
2598
2599 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2600 if (!sourceRenderTarget)
2601 {
2602 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002603 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002604 }
2605
2606 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2607 if (!source)
2608 {
2609 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002610 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002611 }
2612
2613 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2614 if (!storage11)
2615 {
2616 source->Release();
2617 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002618 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002619 }
2620
2621 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2622 if (!destRenderTarget)
2623 {
2624 source->Release();
2625 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002626 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002627 }
2628
2629 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2630 if (!dest)
2631 {
2632 source->Release();
2633 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002634 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002635 }
2636
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002637 gl::Box sourceArea;
2638 sourceArea.x = sourceRect.x;
2639 sourceArea.y = sourceRect.y;
2640 sourceArea.z = 0;
2641 sourceArea.width = sourceRect.width;
2642 sourceArea.height = sourceRect.height;
2643 sourceArea.depth = 1;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002644
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002645 gl::Box destArea;
2646 destArea.x = xoffset;
2647 destArea.y = yoffset;
2648 destArea.z = 0;
2649 destArea.width = sourceRect.width;
2650 destArea.height = sourceRect.height;
2651 destArea.depth = 1;
2652
2653 bool ret = copyTexture(source, sourceArea, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1,
2654 dest, destArea, destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1, destFormat);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002655
2656 source->Release();
2657 dest->Release();
2658
2659 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002660}
2661
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002662bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002663 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002664{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002665 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002666 if (!colorbuffer)
2667 {
2668 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002669 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002670 }
2671
2672 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2673 if (!sourceRenderTarget)
2674 {
2675 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002676 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002677 }
2678
2679 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2680 if (!source)
2681 {
2682 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002683 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002684 }
2685
2686 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2687 if (!storage11)
2688 {
2689 source->Release();
2690 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002691 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002692 }
2693
2694 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(target, level));
2695 if (!destRenderTarget)
2696 {
2697 source->Release();
2698 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002699 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002700 }
2701
2702 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2703 if (!dest)
2704 {
2705 source->Release();
2706 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002707 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002708 }
2709
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002710 gl::Box sourceArea;
2711 sourceArea.x = sourceRect.x;
2712 sourceArea.y = sourceRect.y;
2713 sourceArea.z = 0;
2714 sourceArea.width = sourceRect.width;
2715 sourceArea.height = sourceRect.height;
2716 sourceArea.depth = 1;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002717
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002718 gl::Box destArea;
2719 destArea.x = xoffset;
2720 destArea.y = yoffset;
2721 destArea.z = 0;
2722 destArea.width = sourceRect.width;
2723 destArea.height = sourceRect.height;
2724 destArea.depth = 1;
2725
2726 bool ret = copyTexture(source, sourceArea, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1,
2727 dest, destArea, destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1, destFormat);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002728
2729 source->Release();
2730 dest->Release();
2731
2732 return ret;
2733}
2734
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002735bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2736 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2737{
2738 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2739 if (!colorbuffer)
2740 {
2741 ERR("Failed to retrieve the color buffer from the frame buffer.");
2742 return gl::error(GL_OUT_OF_MEMORY, false);
2743 }
2744
2745 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2746 if (!sourceRenderTarget)
2747 {
2748 ERR("Failed to retrieve the render target from the frame buffer.");
2749 return gl::error(GL_OUT_OF_MEMORY, false);
2750 }
2751
2752 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2753 if (!source)
2754 {
2755 ERR("Failed to retrieve the render target view from the render target.");
2756 return gl::error(GL_OUT_OF_MEMORY, false);
2757 }
2758
2759 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2760 if (!storage11)
2761 {
2762 source->Release();
2763 ERR("Failed to retrieve the texture storage from the destination.");
2764 return gl::error(GL_OUT_OF_MEMORY, false);
2765 }
2766
2767 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2768 if (!destRenderTarget)
2769 {
2770 source->Release();
2771 ERR("Failed to retrieve the render target from the destination storage.");
2772 return gl::error(GL_OUT_OF_MEMORY, false);
2773 }
2774
2775 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2776 if (!dest)
2777 {
2778 source->Release();
2779 ERR("Failed to retrieve the render target view from the destination render target.");
2780 return gl::error(GL_OUT_OF_MEMORY, false);
2781 }
2782
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002783 gl::Box sourceArea;
2784 sourceArea.x = sourceRect.x;
2785 sourceArea.y = sourceRect.y;
2786 sourceArea.z = 0;
2787 sourceArea.width = sourceRect.width;
2788 sourceArea.height = sourceRect.height;
2789 sourceArea.depth = 1;
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002790
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002791 gl::Box destArea;
2792 destArea.x = xoffset;
2793 destArea.y = yoffset;
2794 destArea.z = 0;
2795 destArea.width = sourceRect.width;
2796 destArea.height = sourceRect.height;
2797 destArea.depth = 1;
2798
2799 bool ret = copyTexture(source, sourceArea, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1,
2800 dest, destArea, destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1, destFormat);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002801
2802 source->Release();
2803 dest->Release();
2804
2805 return ret;
2806}
2807
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002808bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2809 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2810{
2811 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2812 if (!colorbuffer)
2813 {
2814 ERR("Failed to retrieve the color buffer from the frame buffer.");
2815 return gl::error(GL_OUT_OF_MEMORY, false);
2816 }
2817
2818 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2819 if (!sourceRenderTarget)
2820 {
2821 ERR("Failed to retrieve the render target from the frame buffer.");
2822 return gl::error(GL_OUT_OF_MEMORY, false);
2823 }
2824
2825 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2826 if (!source)
2827 {
2828 ERR("Failed to retrieve the render target view from the render target.");
2829 return gl::error(GL_OUT_OF_MEMORY, false);
2830 }
2831
2832 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2833 if (!storage11)
2834 {
2835 source->Release();
2836 ERR("Failed to retrieve the texture storage from the destination.");
2837 return gl::error(GL_OUT_OF_MEMORY, false);
2838 }
2839
2840 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2841 if (!destRenderTarget)
2842 {
2843 source->Release();
2844 ERR("Failed to retrieve the render target from the destination storage.");
2845 return gl::error(GL_OUT_OF_MEMORY, false);
2846 }
2847
2848 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2849 if (!dest)
2850 {
2851 source->Release();
2852 ERR("Failed to retrieve the render target view from the destination render target.");
2853 return gl::error(GL_OUT_OF_MEMORY, false);
2854 }
2855
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002856 gl::Box sourceArea;
2857 sourceArea.x = sourceRect.x;
2858 sourceArea.y = sourceRect.y;
2859 sourceArea.z = 0;
2860 sourceArea.width = sourceRect.width;
2861 sourceArea.height = sourceRect.height;
2862 sourceArea.depth = 1;
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002863
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002864 gl::Box destArea;
2865 destArea.x = xoffset;
2866 destArea.y = yoffset;
2867 destArea.z = 0;
2868 destArea.width = sourceRect.width;
2869 destArea.height = sourceRect.height;
2870 destArea.depth = 1;
2871
2872 bool ret = copyTexture(source, sourceArea, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1,
2873 dest, destArea, destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1, destFormat);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002874
2875 source->Release();
2876 dest->Release();
2877
2878 return ret;
2879}
2880
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002881bool Renderer11::copyTexture(ID3D11ShaderResourceView *source, const gl::Box &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight, unsigned int sourceDepth,
2882 ID3D11RenderTargetView *dest, const gl::Box &destArea, unsigned int destWidth, unsigned int destHeight, unsigned int destDepth,
2883 GLenum destFormat)
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002884{
2885 HRESULT result;
2886
2887 if (!mCopyResourcesInitialized)
2888 {
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002889 ASSERT(!mCopyVB && !mCopySampler && !mCopy2DIL && !mCopy2DVS && !mCopyRGBA2DPS && !mCopyRGB2DPS && !mCopyLum2DPS && !mCopyLumAlpha2DPS &&
2890 !mCopy3DIL && !mCopy3DVS && !mCopy3DGS && !mCopyRGBA3DPS && !mCopyRGB3DPS && !mCopyLum3DPS && !mCopyLumAlpha3DPS);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002891
2892 D3D11_BUFFER_DESC vbDesc;
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002893 vbDesc.ByteWidth = std::max(sizeof(d3d11::PositionLayerTexCoord3DVertex) * 6 * getMaxTextureDepth(),
2894 sizeof(d3d11::PositionTexCoordVertex) * 4);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002895 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
2896 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
2897 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
2898 vbDesc.MiscFlags = 0;
2899 vbDesc.StructureByteStride = 0;
2900
2901 result = mDevice->CreateBuffer(&vbDesc, NULL, &mCopyVB);
2902 ASSERT(SUCCEEDED(result));
2903 d3d11::SetDebugName(mCopyVB, "Renderer11 copy texture vertex buffer");
2904
2905 D3D11_SAMPLER_DESC samplerDesc;
2906 samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
2907 samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
2908 samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
2909 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
2910 samplerDesc.MipLODBias = 0.0f;
2911 samplerDesc.MaxAnisotropy = 0;
2912 samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
2913 samplerDesc.BorderColor[0] = 0.0f;
2914 samplerDesc.BorderColor[1] = 0.0f;
2915 samplerDesc.BorderColor[2] = 0.0f;
2916 samplerDesc.BorderColor[3] = 0.0f;
2917 samplerDesc.MinLOD = 0.0f;
2918 samplerDesc.MaxLOD = 0.0f;
2919
2920 result = mDevice->CreateSamplerState(&samplerDesc, &mCopySampler);
2921 ASSERT(SUCCEEDED(result));
2922 d3d11::SetDebugName(mCopySampler, "Renderer11 copy sampler");
2923
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002924 // Create 2D copy resources
2925 D3D11_INPUT_ELEMENT_DESC quad2DLayout[] =
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002926 {
2927 { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2928 { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2929 };
2930
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002931 result = mDevice->CreateInputLayout(quad2DLayout, ArraySize(quad2DLayout), g_VS_Passthrough2D, sizeof(g_VS_Passthrough2D), &mCopy2DIL);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002932 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002933 d3d11::SetDebugName(mCopy2DIL, "Renderer11 copy 2D texture input layout");
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002934
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002935 result = mDevice->CreateVertexShader(g_VS_Passthrough2D, sizeof(g_VS_Passthrough2D), NULL, &mCopy2DVS);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002936 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002937 d3d11::SetDebugName(mCopy2DVS, "Renderer11 copy 2D texture vertex shader");
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002938
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002939 result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA2D, sizeof(g_PS_PassthroughRGBA2D), NULL, &mCopyRGBA2DPS);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002940 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002941 d3d11::SetDebugName(mCopyRGBA2DPS, "Renderer11 copy 2D texture RGBA pixel shader");
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002942
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002943 result = mDevice->CreatePixelShader(g_PS_PassthroughRGB2D, sizeof(g_PS_PassthroughRGB2D), NULL, &mCopyRGB2DPS);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002944 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002945 d3d11::SetDebugName(mCopyRGB2DPS, "Renderer11 copy 2D texture RGB pixel shader");
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002946
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002947 result = mDevice->CreatePixelShader(g_PS_PassthroughLum2D, sizeof(g_PS_PassthroughLum2D), NULL, &mCopyLum2DPS);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002948 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002949 d3d11::SetDebugName(mCopyLum2DPS, "Renderer11 copy 2D texture luminance pixel shader");
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002950
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002951 result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha2D, sizeof(g_PS_PassthroughLumAlpha2D), NULL, &mCopyLumAlpha2DPS);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002952 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002953 d3d11::SetDebugName(mCopyLumAlpha2DPS, "Renderer11 2D copy texture luminance alpha pixel shader");
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002954
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002955 // Create 3D copy resources
2956 D3D11_INPUT_ELEMENT_DESC quad3DLayout[] =
2957 {
2958 { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2959 { "LAYER", 0, DXGI_FORMAT_R32_UINT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2960 { "TEXCOORD", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2961 };
2962
2963 result = mDevice->CreateInputLayout(quad3DLayout, ArraySize(quad3DLayout), g_VS_Passthrough3D, sizeof(g_VS_Passthrough3D), &mCopy3DIL);
2964 ASSERT(SUCCEEDED(result));
2965 d3d11::SetDebugName(mCopy3DIL, "Renderer11 copy 3D texture input layout");
2966
2967 result = mDevice->CreateVertexShader(g_VS_Passthrough3D, sizeof(g_VS_Passthrough3D), NULL, &mCopy3DVS);
2968 ASSERT(SUCCEEDED(result));
2969 d3d11::SetDebugName(mCopy3DVS, "Renderer11 copy 3D texture vertex shader");
2970
2971 result = mDevice->CreateGeometryShader(g_GS_Passthrough3D, sizeof(g_GS_Passthrough3D), NULL, &mCopy3DGS);
2972 ASSERT(SUCCEEDED(result));
2973 d3d11::SetDebugName(mCopy3DGS, "Renderer11 copy 3D texture geometry shader");
2974
2975 result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA3D, sizeof(g_PS_PassthroughRGBA3D), NULL, &mCopyRGBA3DPS);
2976 ASSERT(SUCCEEDED(result));
2977 d3d11::SetDebugName(mCopyRGBA3DPS, "Renderer11 copy 3D texture RGBA pixel shader");
2978
2979 result = mDevice->CreatePixelShader(g_PS_PassthroughRGB3D, sizeof(g_PS_PassthroughRGB3D), NULL, &mCopyRGB3DPS);
2980 ASSERT(SUCCEEDED(result));
2981 d3d11::SetDebugName(mCopyRGB3DPS, "Renderer11 copy 3D texture RGB pixel shader");
2982
2983 result = mDevice->CreatePixelShader(g_PS_PassthroughLum3D, sizeof(g_PS_PassthroughLum3D), NULL, &mCopyLum3DPS);
2984 ASSERT(SUCCEEDED(result));
2985 d3d11::SetDebugName(mCopyLum3DPS, "Renderer11 copy 3D texture luminance pixel shader");
2986
2987 result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha3D, sizeof(g_PS_PassthroughLumAlpha3D), NULL, &mCopyLumAlpha3DPS);
2988 ASSERT(SUCCEEDED(result));
2989 d3d11::SetDebugName(mCopyLumAlpha3DPS, "Renderer11 3D copy texture luminance alpha pixel shader");
2990
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002991 mCopyResourcesInitialized = true;
2992 }
2993
2994 // Verify the source and destination area sizes
2995 if (sourceArea.x < 0 || sourceArea.x + sourceArea.width > static_cast<int>(sourceWidth) ||
2996 sourceArea.y < 0 || sourceArea.y + sourceArea.height > static_cast<int>(sourceHeight) ||
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002997 sourceArea.z < 0 || sourceArea.z + sourceArea.depth > static_cast<int>(sourceDepth) ||
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002998 destArea.x < 0 || destArea.x + destArea.width > static_cast<int>(destWidth) ||
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002999 destArea.y < 0 || destArea.y + destArea.height > static_cast<int>(destHeight) ||
3000 destArea.z < 0 || destArea.z + destArea.depth > static_cast<int>(destDepth))
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003001 {
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00003002 return gl::error(GL_INVALID_VALUE, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003003 }
3004
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003005 bool use3DCopy = sourceArea.depth > 1;
3006
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003007 // Set vertices
3008 D3D11_MAPPED_SUBRESOURCE mappedResource;
3009 result = mDeviceContext->Map(mCopyVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
3010 if (FAILED(result))
3011 {
3012 ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00003013 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003014 }
3015
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003016 // Create a quad in homogeneous coordinates
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00003017 float x1 = (destArea.x / float(destWidth)) * 2.0f - 1.0f;
shannon.woods@transgaming.comcfdfd462013-02-28 23:13:01 +00003018 float y1 = ((destHeight - destArea.y - destArea.height) / float(destHeight)) * 2.0f - 1.0f;
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00003019 float x2 = ((destArea.x + destArea.width) / float(destWidth)) * 2.0f - 1.0f;
shannon.woods@transgaming.comcfdfd462013-02-28 23:13:01 +00003020 float y2 = ((destHeight - destArea.y) / float(destHeight)) * 2.0f - 1.0f;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003021
3022 float u1 = sourceArea.x / float(sourceWidth);
3023 float v1 = sourceArea.y / float(sourceHeight);
3024 float u2 = (sourceArea.x + sourceArea.width) / float(sourceWidth);
3025 float v2 = (sourceArea.y + sourceArea.height) / float(sourceHeight);
3026
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003027 UINT stride = 0;
3028 UINT startIdx = 0;
3029 UINT drawCount = 0;
3030 D3D11_PRIMITIVE_TOPOLOGY topology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
3031
3032 if (use3DCopy)
3033 {
3034 d3d11::PositionLayerTexCoord3DVertex *vertices = static_cast<d3d11::PositionLayerTexCoord3DVertex*>(mappedResource.pData);
3035
3036 for (unsigned int i = 0; i < destDepth; i++)
3037 {
3038 float readDepth = ((i * 2) + 0.5f) / (sourceDepth - 1);
3039
3040 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 0], x1, y1, i, u1, v2, readDepth);
3041 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 1], x1, y2, i, u1, v1, readDepth);
3042 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 2], x2, y1, i, u2, v2, readDepth);
3043
3044 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 3], x1, y2, i, u1, v1, readDepth);
3045 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 4], x2, y2, i, u2, v1, readDepth);
3046 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 5], x2, y1, i, u2, v2, readDepth);
3047 }
3048
3049 stride = sizeof(d3d11::PositionLayerTexCoord3DVertex);
3050 drawCount = destDepth * 6;
3051 topology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
3052 }
3053 else
3054 {
3055 d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData);
3056
3057 d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2);
3058 d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1);
3059 d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2);
3060 d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1);
3061
3062 stride = sizeof(d3d11::PositionTexCoordVertex);
3063 drawCount = 4;
3064 topology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
3065 }
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003066
3067 mDeviceContext->Unmap(mCopyVB, 0);
3068
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003069 mDeviceContext->IASetVertexBuffers(0, 1, &mCopyVB, &stride, &startIdx);
3070
3071 // Apply state
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00003072 mDeviceContext->OMSetBlendState(NULL, NULL, 0xFFFFFFF);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003073 mDeviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF);
3074 mDeviceContext->RSSetState(NULL);
3075
3076 // Apply shaders
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003077 ID3D11InputLayout *il = NULL;
3078 ID3D11VertexShader *vs = NULL;
3079 ID3D11GeometryShader *gs = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003080 ID3D11PixelShader *ps = NULL;
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003081
3082 if (use3DCopy)
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003083 {
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003084 il = mCopy3DIL;
3085 vs = mCopy3DVS;
3086 gs = mCopy3DGS;
3087
3088 switch(destFormat)
3089 {
3090 case GL_RGBA: ps = mCopyRGBA3DPS; break;
3091 case GL_RGB: ps = mCopyRGB3DPS; break;
3092 case GL_ALPHA: ps = mCopyRGBA3DPS; break;
3093 case GL_BGRA_EXT: ps = mCopyRGBA3DPS; break;
3094 case GL_LUMINANCE: ps = mCopyLum3DPS; break;
3095 case GL_LUMINANCE_ALPHA: ps = mCopyLumAlpha3DPS; break;
3096 default: UNREACHABLE(); ps = NULL; break;
3097 }
3098 }
3099 else
3100 {
3101 il = mCopy2DIL;
3102 vs = mCopy2DVS;
3103 gs = NULL;
3104
3105 switch(destFormat)
3106 {
3107 case GL_RGBA: ps = mCopyRGBA2DPS; break;
3108 case GL_RGB: ps = mCopyRGB2DPS; break;
3109 case GL_ALPHA: ps = mCopyRGBA2DPS; break;
3110 case GL_BGRA_EXT: ps = mCopyRGBA2DPS; break;
3111 case GL_LUMINANCE: ps = mCopyLum2DPS; break;
3112 case GL_LUMINANCE_ALPHA: ps = mCopyLumAlpha2DPS; break;
3113 default: UNREACHABLE(); ps = NULL; break;
3114 }
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003115 }
3116
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003117 mDeviceContext->IASetInputLayout(il);
3118 mDeviceContext->IASetPrimitiveTopology(topology);
3119 mDeviceContext->VSSetShader(vs, NULL, 0);
3120
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003121 mDeviceContext->PSSetShader(ps, NULL, 0);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003122 mDeviceContext->GSSetShader(gs, NULL, 0);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003123
3124 // Unset the currently bound shader resource to avoid conflicts
3125 static ID3D11ShaderResourceView *const nullSRV = NULL;
3126 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
3127
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00003128 // Apply render target
3129 setOneTimeRenderTarget(dest);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003130
3131 // Set the viewport
3132 D3D11_VIEWPORT viewport;
3133 viewport.TopLeftX = 0;
3134 viewport.TopLeftY = 0;
3135 viewport.Width = destWidth;
3136 viewport.Height = destHeight;
3137 viewport.MinDepth = 0.0f;
3138 viewport.MaxDepth = 1.0f;
3139 mDeviceContext->RSSetViewports(1, &viewport);
3140
3141 // Apply textures
3142 mDeviceContext->PSSetShaderResources(0, 1, &source);
3143 mDeviceContext->PSSetSamplers(0, 1, &mCopySampler);
3144
3145 // Draw the quad
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003146 mDeviceContext->Draw(drawCount, 0);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003147
3148 // Unbind textures and render targets and vertex buffer
3149 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
3150
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00003151 unapplyRenderTargets();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003152
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00003153 UINT zero = 0;
3154 ID3D11Buffer *const nullBuffer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003155 mDeviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
3156
3157 markAllStateDirty();
3158
3159 return true;
daniel@transgaming.com38380882012-11-28 19:36:39 +00003160}
3161
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00003162void Renderer11::unapplyRenderTargets()
3163{
3164 setOneTimeRenderTarget(NULL);
3165}
3166
3167void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
3168{
3169 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
3170
3171 rtvArray[0] = renderTargetView;
3172
3173 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
3174
3175 // Do not preserve the serial for this one-time-use render target
3176 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
3177 {
3178 mAppliedRenderTargetSerials[rtIndex] = 0;
3179 }
3180}
3181
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003182RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
3183{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00003184 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00003185 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00003186
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00003187 if (depth)
3188 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00003189 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00003190 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
3191 swapChain11->getDepthStencilTexture(), NULL,
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00003192 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00003193 }
3194 else
3195 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00003196 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00003197 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00003198 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00003199 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00003200 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00003201 }
3202 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003203}
3204
3205RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
3206{
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00003207 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples, depth);
3208 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003209}
3210
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003211ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00003212{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003213 ShaderExecutable11 *executable = NULL;
3214
3215 switch (type)
3216 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003217 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003218 {
3219 ID3D11VertexShader *vshader = NULL;
3220 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
3221 ASSERT(SUCCEEDED(result));
3222
3223 if (vshader)
3224 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003225 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003226 }
3227 }
3228 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003229 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003230 {
3231 ID3D11PixelShader *pshader = NULL;
3232 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
3233 ASSERT(SUCCEEDED(result));
3234
3235 if (pshader)
3236 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003237 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003238 }
3239 }
3240 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00003241 case rx::SHADER_GEOMETRY:
3242 {
3243 ID3D11GeometryShader *gshader = NULL;
3244 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
3245 ASSERT(SUCCEEDED(result));
3246
3247 if (gshader)
3248 {
3249 executable = new ShaderExecutable11(function, length, gshader);
3250 }
3251 }
3252 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003253 default:
3254 UNREACHABLE();
3255 break;
3256 }
3257
3258 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00003259}
3260
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003261ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003262{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003263 const char *profile = NULL;
3264
3265 switch (type)
3266 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003267 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003268 profile = "vs_4_0";
3269 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003270 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003271 profile = "ps_4_0";
3272 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00003273 case rx::SHADER_GEOMETRY:
3274 profile = "gs_4_0";
3275 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003276 default:
3277 UNREACHABLE();
3278 return NULL;
3279 }
3280
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00003281 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003282 if (!binary)
3283 return NULL;
3284
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003285 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003286 binary->Release();
3287
3288 return executable;
3289}
3290
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00003291VertexBuffer *Renderer11::createVertexBuffer()
3292{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00003293 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00003294}
3295
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00003296IndexBuffer *Renderer11::createIndexBuffer()
3297{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00003298 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003299}
3300
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00003301BufferStorage *Renderer11::createBufferStorage()
3302{
3303 return new BufferStorage11(this);
3304}
3305
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003306QueryImpl *Renderer11::createQuery(GLenum type)
3307{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00003308 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003309}
3310
3311FenceImpl *Renderer11::createFence()
3312{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00003313 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00003314}
3315
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003316bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003317{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003318 ASSERT(colorbuffer != NULL);
3319
3320 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
3321 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003322 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003323 *subresourceIndex = renderTarget->getSubresourceIndex();
3324
3325 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
3326 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003327 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003328 ID3D11Resource *textureResource = NULL;
3329 colorBufferRTV->GetResource(&textureResource);
3330 colorBufferRTV->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003331
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003332 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003333 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003334 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
3335 textureResource->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003336
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003337 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003338 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003339 return true;
3340 }
3341 else
3342 {
3343 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
3344 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003345 }
3346 }
3347 }
3348 }
3349
3350 return false;
3351}
3352
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003353bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003354 bool blitRenderTarget, bool blitDepthStencil)
3355{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003356 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003357 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003358 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003359
3360 if (!readBuffer)
3361 {
3362 ERR("Failed to retrieve the read buffer from the read framebuffer.");
3363 return gl::error(GL_OUT_OF_MEMORY, false);
3364 }
3365
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003366 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003367
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003368 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003369 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003370 if (drawTarget->isEnabledColorAttachment(colorAttachment))
3371 {
3372 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
3373
3374 if (!drawBuffer)
3375 {
3376 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
3377 return gl::error(GL_OUT_OF_MEMORY, false);
3378 }
3379
3380 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
3381
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003382 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003383 {
3384 return false;
3385 }
3386 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003387 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003388 }
3389
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003390 if (blitDepthStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003391 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003392 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
3393 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
3394
3395 if (!readBuffer)
3396 {
3397 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
3398 return gl::error(GL_OUT_OF_MEMORY, false);
3399 }
3400
3401 if (!drawBuffer)
3402 {
3403 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
3404 return gl::error(GL_OUT_OF_MEMORY, false);
3405 }
3406
3407 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3408 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3409
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003410 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, true))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003411 {
3412 return false;
3413 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003414 }
3415
3416 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003417}
3418
3419void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3420 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3421{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003422 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003423 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003424
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003425 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3426
3427 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003428 {
3429 gl::Rectangle area;
3430 area.x = x;
3431 area.y = y;
3432 area.width = width;
3433 area.height = height;
3434
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003435 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3436 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003437
3438 colorBufferTexture->Release();
3439 colorBufferTexture = NULL;
3440 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003441}
3442
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003443Image *Renderer11::createImage()
3444{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003445 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003446}
3447
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003448void Renderer11::generateMipmap(Image *dest, Image *src)
3449{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003450 Image11 *dest11 = Image11::makeImage11(dest);
3451 Image11 *src11 = Image11::makeImage11(src);
3452 Image11::generateMipmap(dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003453}
3454
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003455TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3456{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003457 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3458 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003459}
3460
3461TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3462{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003463 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003464}
3465
3466TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3467{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003468 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003469}
3470
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003471TextureStorage *Renderer11::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3472{
3473 return new TextureStorage11_3D(this, levels, internalformat, usage, width, height, depth);
3474}
3475
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003476TextureStorage *Renderer11::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3477{
3478 return new TextureStorage11_2DArray(this, levels, internalformat, usage, width, height, depth);
3479}
3480
shannonwoods@chromium.orgb36e29f2013-05-30 00:16:30 +00003481static inline unsigned int getFastPixelCopySize(DXGI_FORMAT sourceFormat, GLenum destFormat, GLenum destType, GLuint clientVersion)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003482{
shannonwoods@chromium.orgb36e29f2013-05-30 00:16:30 +00003483 GLint sourceInternalFormat = d3d11_gl::GetInternalFormat(sourceFormat);
3484 GLenum sourceGLFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3485 GLenum sourceGLType = gl::GetType(sourceInternalFormat, clientVersion);
3486
3487 if (sourceGLFormat == destFormat && sourceGLType == destType)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003488 {
shannonwoods@chromium.orgb36e29f2013-05-30 00:16:30 +00003489 return gl::GetPixelBytes(sourceInternalFormat, clientVersion);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003490 }
3491 else
3492 {
3493 return 0;
3494 }
3495}
3496
3497static inline void readPixelColor(const unsigned char *data, DXGI_FORMAT format, unsigned int x,
3498 unsigned int y, int inputPitch, gl::Color *outColor)
3499{
3500 switch (format)
3501 {
3502 case DXGI_FORMAT_R8G8B8A8_UNORM:
shannon.woods%transgaming.com@gtempaccount.com8dce6512013-04-13 03:42:19 +00003503 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003504 {
3505 unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
shannon.woods@transgaming.com25950ea2013-02-28 23:04:15 +00003506 outColor->red = (rgba & 0x000000FF) * (1.0f / 0x000000FF);
3507 outColor->green = (rgba & 0x0000FF00) * (1.0f / 0x0000FF00);
3508 outColor->blue = (rgba & 0x00FF0000) * (1.0f / 0x00FF0000);
3509 outColor->alpha = (rgba & 0xFF000000) * (1.0f / 0xFF000000);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003510 }
3511 break;
3512
3513 case DXGI_FORMAT_A8_UNORM:
3514 {
3515 outColor->red = 0.0f;
3516 outColor->green = 0.0f;
3517 outColor->blue = 0.0f;
3518 outColor->alpha = *(data + x + y * inputPitch) / 255.0f;
3519 }
3520 break;
3521
3522 case DXGI_FORMAT_R32G32B32A32_FLOAT:
3523 {
3524 outColor->red = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 0);
3525 outColor->green = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 1);
3526 outColor->blue = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 2);
3527 outColor->alpha = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 3);
3528 }
3529 break;
3530
3531 case DXGI_FORMAT_R32G32B32_FLOAT:
3532 {
3533 outColor->red = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 0);
3534 outColor->green = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 1);
3535 outColor->blue = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 2);
3536 outColor->alpha = 1.0f;
3537 }
3538 break;
3539
3540 case DXGI_FORMAT_R16G16B16A16_FLOAT:
3541 {
3542 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 0));
3543 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 1));
3544 outColor->blue = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 2));
3545 outColor->alpha = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 3));
3546 }
3547 break;
3548
3549 case DXGI_FORMAT_B8G8R8A8_UNORM:
3550 {
3551 unsigned int bgra = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
shannon.woods@transgaming.com25950ea2013-02-28 23:04:15 +00003552 outColor->red = (bgra & 0x00FF0000) * (1.0f / 0x00FF0000);
3553 outColor->blue = (bgra & 0x000000FF) * (1.0f / 0x000000FF);
3554 outColor->green = (bgra & 0x0000FF00) * (1.0f / 0x0000FF00);
3555 outColor->alpha = (bgra & 0xFF000000) * (1.0f / 0xFF000000);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003556 }
3557 break;
3558
3559 case DXGI_FORMAT_R8_UNORM:
3560 {
3561 outColor->red = *(data + x + y * inputPitch) / 255.0f;
3562 outColor->green = 0.0f;
3563 outColor->blue = 0.0f;
3564 outColor->alpha = 1.0f;
3565 }
3566 break;
3567
3568 case DXGI_FORMAT_R8G8_UNORM:
3569 {
3570 unsigned short rg = *reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch);
3571
3572 outColor->red = (rg & 0xFF00) * (1.0f / 0xFF00);
3573 outColor->green = (rg & 0x00FF) * (1.0f / 0x00FF);
3574 outColor->blue = 0.0f;
3575 outColor->alpha = 1.0f;
3576 }
3577 break;
3578
3579 case DXGI_FORMAT_R16_FLOAT:
3580 {
3581 outColor->red = gl::float16ToFloat32(*reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch));
3582 outColor->green = 0.0f;
3583 outColor->blue = 0.0f;
3584 outColor->alpha = 1.0f;
3585 }
3586 break;
3587
3588 case DXGI_FORMAT_R16G16_FLOAT:
3589 {
3590 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 0));
3591 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 1));
3592 outColor->blue = 0.0f;
3593 outColor->alpha = 1.0f;
3594 }
3595 break;
3596
shannon.woods%transgaming.com@gtempaccount.com8dce6512013-04-13 03:42:19 +00003597 case DXGI_FORMAT_R10G10B10A2_UNORM:
3598 {
3599 unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
3600 outColor->red = (rgba & 0x000003FF) * (1.0f / 0x000003FF);
3601 outColor->green = (rgba & 0x000FFC00) * (1.0f / 0x000FFC00);
3602 outColor->blue = (rgba & 0x3FF00000) * (1.0f / 0x3FF00000);
3603 outColor->alpha = (rgba & 0xC0000000) * (1.0f / 0xC0000000);
3604 }
3605 break;
3606
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003607 default:
3608 ERR("ReadPixelColor not implemented for DXGI format %u.", format);
3609 UNIMPLEMENTED();
3610 break;
3611 }
3612}
3613
3614static inline void writePixelColor(const gl::Color &color, GLenum format, GLenum type, unsigned int x,
3615 unsigned int y, int outputPitch, void *outData)
3616{
3617 unsigned char* byteData = reinterpret_cast<unsigned char*>(outData);
3618 unsigned short* shortData = reinterpret_cast<unsigned short*>(outData);
shannonwoods@chromium.org5d4468e2013-05-30 00:13:56 +00003619 unsigned int* intData = reinterpret_cast<unsigned int*>(outData);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003620
3621 switch (format)
3622 {
3623 case GL_RGBA:
3624 switch (type)
3625 {
3626 case GL_UNSIGNED_BYTE:
3627 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
3628 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3629 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3630 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
3631 break;
3632
shannonwoods@chromium.org5d4468e2013-05-30 00:13:56 +00003633 case GL_UNSIGNED_INT_2_10_10_10_REV:
3634 intData[x + y * outputPitch / sizeof(unsigned int)] = (static_cast<unsigned int>( 3 * color.alpha) << 30) |
3635 (static_cast<unsigned int>(1023 * color.red ) << 20) |
3636 (static_cast<unsigned int>(1023 * color.green) << 10) |
3637 (static_cast<unsigned int>(1023 * color.blue ) << 0);
3638 break;
3639
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003640 default:
3641 ERR("WritePixelColor not implemented for format GL_RGBA and type 0x%X.", type);
3642 UNIMPLEMENTED();
3643 break;
3644 }
3645 break;
3646
3647 case GL_BGRA_EXT:
3648 switch (type)
3649 {
3650 case GL_UNSIGNED_BYTE:
3651 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3652 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3653 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.red + 0.5f);
3654 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
3655 break;
3656
3657 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
3658 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
3659 // this type is packed as follows:
3660 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3661 // --------------------------------------------------------------------------------
3662 // | 4th | 3rd | 2nd | 1st component |
3663 // --------------------------------------------------------------------------------
3664 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
3665 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3666 (static_cast<unsigned short>(15 * color.alpha + 0.5f) << 12) |
3667 (static_cast<unsigned short>(15 * color.red + 0.5f) << 8) |
3668 (static_cast<unsigned short>(15 * color.green + 0.5f) << 4) |
3669 (static_cast<unsigned short>(15 * color.blue + 0.5f) << 0);
3670 break;
3671
3672 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
3673 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
3674 // this type is packed as follows:
3675 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3676 // --------------------------------------------------------------------------------
3677 // | 4th | 3rd | 2nd | 1st component |
3678 // --------------------------------------------------------------------------------
3679 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
3680 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3681 (static_cast<unsigned short>( color.alpha + 0.5f) << 15) |
3682 (static_cast<unsigned short>(31 * color.red + 0.5f) << 10) |
3683 (static_cast<unsigned short>(31 * color.green + 0.5f) << 5) |
3684 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0);
3685 break;
3686
3687 default:
3688 ERR("WritePixelColor not implemented for format GL_BGRA_EXT and type 0x%X.", type);
3689 UNIMPLEMENTED();
3690 break;
3691 }
3692 break;
3693
3694 case GL_RGB:
3695 switch (type)
3696 {
3697 case GL_UNSIGNED_SHORT_5_6_5:
3698 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3699 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0) |
3700 (static_cast<unsigned short>(63 * color.green + 0.5f) << 5) |
3701 (static_cast<unsigned short>(31 * color.red + 0.5f) << 11);
3702 break;
3703
3704 case GL_UNSIGNED_BYTE:
3705 byteData[3 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
3706 byteData[3 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3707 byteData[3 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3708 break;
3709
3710 default:
3711 ERR("WritePixelColor not implemented for format GL_RGB and type 0x%X.", type);
3712 UNIMPLEMENTED();
3713 break;
3714 }
3715 break;
3716
3717 default:
3718 ERR("WritePixelColor not implemented for format 0x%X.", format);
3719 UNIMPLEMENTED();
3720 break;
3721 }
3722}
3723
3724void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3725 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3726 GLint packAlignment, void *pixels)
3727{
3728 D3D11_TEXTURE2D_DESC textureDesc;
3729 texture->GetDesc(&textureDesc);
3730
3731 D3D11_TEXTURE2D_DESC stagingDesc;
3732 stagingDesc.Width = area.width;
3733 stagingDesc.Height = area.height;
3734 stagingDesc.MipLevels = 1;
3735 stagingDesc.ArraySize = 1;
3736 stagingDesc.Format = textureDesc.Format;
3737 stagingDesc.SampleDesc.Count = 1;
3738 stagingDesc.SampleDesc.Quality = 0;
3739 stagingDesc.Usage = D3D11_USAGE_STAGING;
3740 stagingDesc.BindFlags = 0;
3741 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3742 stagingDesc.MiscFlags = 0;
3743
3744 ID3D11Texture2D* stagingTex = NULL;
3745 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3746 if (FAILED(result))
3747 {
3748 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3749 return;
3750 }
3751
3752 ID3D11Texture2D* srcTex = NULL;
3753 if (textureDesc.SampleDesc.Count > 1)
3754 {
3755 D3D11_TEXTURE2D_DESC resolveDesc;
3756 resolveDesc.Width = textureDesc.Width;
3757 resolveDesc.Height = textureDesc.Height;
3758 resolveDesc.MipLevels = 1;
3759 resolveDesc.ArraySize = 1;
3760 resolveDesc.Format = textureDesc.Format;
3761 resolveDesc.SampleDesc.Count = 1;
3762 resolveDesc.SampleDesc.Quality = 0;
3763 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3764 resolveDesc.BindFlags = 0;
3765 resolveDesc.CPUAccessFlags = 0;
3766 resolveDesc.MiscFlags = 0;
3767
3768 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3769 if (FAILED(result))
3770 {
3771 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
3772 stagingTex->Release();
3773 return;
3774 }
3775
3776 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3777 subResource = 0;
3778 }
3779 else
3780 {
3781 srcTex = texture;
3782 srcTex->AddRef();
3783 }
3784
3785 D3D11_BOX srcBox;
3786 srcBox.left = area.x;
3787 srcBox.right = area.x + area.width;
3788 srcBox.top = area.y;
3789 srcBox.bottom = area.y + area.height;
3790 srcBox.front = 0;
3791 srcBox.back = 1;
3792
3793 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3794
3795 srcTex->Release();
3796 srcTex = NULL;
3797
3798 D3D11_MAPPED_SUBRESOURCE mapping;
3799 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3800
3801 unsigned char *source;
3802 int inputPitch;
3803 if (packReverseRowOrder)
3804 {
3805 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3806 inputPitch = -static_cast<int>(mapping.RowPitch);
3807 }
3808 else
3809 {
3810 source = static_cast<unsigned char*>(mapping.pData);
3811 inputPitch = static_cast<int>(mapping.RowPitch);
3812 }
3813
shannonwoods@chromium.orgb36e29f2013-05-30 00:16:30 +00003814 unsigned int fastPixelSize = getFastPixelCopySize(textureDesc.Format, format, type, getCurrentClientVersion());
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003815 if (fastPixelSize != 0)
3816 {
3817 unsigned char *dest = static_cast<unsigned char*>(pixels);
3818 for (int j = 0; j < area.height; j++)
3819 {
3820 memcpy(dest + j * outputPitch, source + j * inputPitch, area.width * fastPixelSize);
3821 }
3822 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003823 else if (textureDesc.Format == DXGI_FORMAT_B8G8R8A8_UNORM &&
3824 format == GL_RGBA &&
3825 type == GL_UNSIGNED_BYTE)
3826 {
3827 // Fast path for swapping red with blue
3828 unsigned char *dest = static_cast<unsigned char*>(pixels);
3829
3830 for (int j = 0; j < area.height; j++)
3831 {
3832 for (int i = 0; i < area.width; i++)
3833 {
3834 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
3835 *(unsigned int*)(dest + 4 * i + j * outputPitch) =
3836 (argb & 0xFF00FF00) | // Keep alpha and green
3837 (argb & 0x00FF0000) >> 16 | // Move red to blue
3838 (argb & 0x000000FF) << 16; // Move blue to red
3839 }
3840 }
3841 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003842 else
3843 {
3844 gl::Color pixelColor;
3845 for (int j = 0; j < area.height; j++)
3846 {
3847 for (int i = 0; i < area.width; i++)
3848 {
3849 readPixelColor(source, textureDesc.Format, i, j, inputPitch, &pixelColor);
3850 writePixelColor(pixelColor, format, type, i, j, outputPitch, pixels);
3851 }
3852 }
3853 }
3854
3855 mDeviceContext->Unmap(stagingTex, 0);
3856
3857 stagingTex->Release();
3858 stagingTex = NULL;
3859}
3860
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003861bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
3862 RenderTarget *drawRenderTarget, bool wholeBufferCopy)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003863{
3864 ASSERT(readRect.width == drawRect.width && readRect.height == drawRect.height);
3865
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003866 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3867 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003868 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003869 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003870 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003871 }
3872
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003873 ID3D11Resource *readTexture = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003874 unsigned int readSubresource = 0;
3875 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003876 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003877 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3878 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
3879 unresolvedResource->Release();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003880
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003881 if (unresolvedTexture)
3882 {
3883 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3884 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003885
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003886 unresolvedTexture->Release();
3887 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003888 }
3889 else
3890 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003891 readTexture = readRenderTarget11->getTexture();
3892 readSubresource = readRenderTarget11->getSubresourceIndex();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003893 }
3894
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003895 if (!readTexture)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003896 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003897 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003898 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003899 }
3900
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003901 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003902 if (!drawRenderTarget)
3903 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003904 readTexture->Release();
3905 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003906 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003907 }
3908
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003909 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003910 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003911
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003912 D3D11_BOX readBox;
3913 readBox.left = readRect.x;
3914 readBox.right = readRect.x + readRect.width;
3915 readBox.top = readRect.y;
3916 readBox.bottom = readRect.y + readRect.height;
3917 readBox.front = 0;
3918 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003919
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003920 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3921 // We also require complete framebuffer copies for depth-stencil blit.
3922 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
3923
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003924 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, drawRect.x, drawRect.y, 0,
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003925 readTexture, readSubresource, pSrcBox);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003926
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003927 readTexture->Release();
3928 drawTexture->Release();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003929
3930 return true;
3931}
3932
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003933ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3934{
3935 D3D11_TEXTURE2D_DESC textureDesc;
3936 source->GetDesc(&textureDesc);
3937
3938 if (textureDesc.SampleDesc.Count > 1)
3939 {
3940 D3D11_TEXTURE2D_DESC resolveDesc;
3941 resolveDesc.Width = textureDesc.Width;
3942 resolveDesc.Height = textureDesc.Height;
3943 resolveDesc.MipLevels = 1;
3944 resolveDesc.ArraySize = 1;
3945 resolveDesc.Format = textureDesc.Format;
3946 resolveDesc.SampleDesc.Count = 1;
3947 resolveDesc.SampleDesc.Quality = 0;
3948 resolveDesc.Usage = textureDesc.Usage;
3949 resolveDesc.BindFlags = textureDesc.BindFlags;
3950 resolveDesc.CPUAccessFlags = 0;
3951 resolveDesc.MiscFlags = 0;
3952
3953 ID3D11Texture2D *resolveTexture = NULL;
3954 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3955 if (FAILED(result))
3956 {
3957 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3958 return NULL;
3959 }
3960
3961 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3962 return resolveTexture;
3963 }
3964 else
3965 {
3966 source->AddRef();
3967 return source;
3968 }
3969}
3970
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003971bool Renderer11::getLUID(LUID *adapterLuid) const
3972{
3973 adapterLuid->HighPart = 0;
3974 adapterLuid->LowPart = 0;
3975
3976 if (!mDxgiAdapter)
3977 {
3978 return false;
3979 }
3980
3981 DXGI_ADAPTER_DESC adapterDesc;
3982 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3983 {
3984 return false;
3985 }
3986
3987 *adapterLuid = adapterDesc.AdapterLuid;
3988 return true;
3989}
3990
Geoff Lang61e49a52013-05-29 10:22:58 -04003991Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3992{
3993 MultisampleSupportInfo supportInfo = { 0 };
3994
3995 UINT formatSupport;
3996 HRESULT result;
3997
3998 result = mDevice->CheckFormatSupport(format, &formatSupport);
3999 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
4000 {
4001 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
4002 {
4003 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
4004 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
4005 {
4006 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
4007 }
4008 else
4009 {
4010 supportInfo.qualityLevels[i - 1] = 0;
4011 }
4012 }
4013 }
4014
4015 return supportInfo;
4016}
4017
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00004018}